Fork me on GitHub

Spring-MVC

//第一步,在web.xml文件中注册
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<display-name>Archetype Created Web Application</display-name>

<!--绑定Spring-Servlet类-->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--
绑定Spring配置文件
-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value>
</init-param>
<!--
设置启动级别,一代表和服务器一起启动
-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--
只会匹配所有的请求
匹配所有的请求,包括jsp页面
-->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!--中文乱码-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

 

 

注解开发:
SpringMVC.xml 更换为下面
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--注解的支持-->
<context:component-scan base-package="com.jiang.servlet" />
<!--过滤静态资源-->
<mvc:default-servlet-handler />
<!--原本的处理器和解析器-->
<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- <property name="prefix" value="/WEB-INF/jsp/"/>-->
<!-- <property name="suffix" value=".jsp"/>-->
</bean>

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/html/*"/>
<bean class="com.jiang.Fitel.MyFitel"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>




常用注解:
@Controller 代表这个类为servlet类
@RequestMapping("/hello") 注册url
@ResponseBody 不再是注册url,直接返回一个字符串
RestFul风格(简洁,安全,高效) @GetMapping("/add") 规定必须用某种方式提交
@PathVariable 用来将参数使用RestFul风格

 

posted @ 2024-03-25 21:08  一名狗书匠&  阅读(1)  评论(0编辑  收藏  举报

asd