一、引入依赖
二、xml配置
1.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>springMVC_demo1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置核心控制器 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置hiddenHttpMethodFilter,可以将post请求转为_method的值(delete/put) -->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置字符编码过滤器 -->
<filter>
<filter-name>encodeFiler</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>
</filter>
<filter-mapping>
<filter-name>encodeFiler</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.spring-mvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 配置扫描器 -->
<context:component-scan base-package="com.ali.clc.controller"></context:component-scan>
<!-- 开启注解驱动,支持springmvc一些更高级的功能,如映射动态请求,jsr303校验,快捷的ajax等 -->
<mvc:annotation-driven>
<!-- 设置直接响应纯文本时的编码格式 -->
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg index="0" value="utf-8"></constructor-arg>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 释放用户对静态资源的请求 -->
<mvc:default-servlet-handler/>
<!-- 配置拦截器 -->
<!-- <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/**"/>
<mvc:exclude-mapping path="/user/login.do"/>
<bean class="com.ali.clc.interceptor.TestInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors> -->
<!-- 配置文件上传解析器 -->
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
value单位:字节byte
<property name="maxUploadSize" value="999999999"></property>
</bean> -->
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
三、注解配置
1.springMVC常用注解
@Controller
@RequestMapping("user"),@RequestMapping(value = "{id}",method = RequestMethod.DELETE),可用method匹配请求
@SessionAttributes(names = {"user","name"}),加在类上
@ResponseBody,向ajax响应对象时配合Jackson.jar使用(obj==>json),在springmvc中使用以html结尾的请求,返回的数据格式都是text(文本格式的),非json格式
@RequestParam(value = "id",defaultValue = "123"),不加()时,按变量名匹配
@PathVariable("id")(restful风格常用这个注解),不加("")时,按变量名匹配;@RequestMapping("xx/{id}/{n}"),{}以外的部分参与请求路径的匹配,
@ControllerAdvice,全局异常处理,加在类上
@ExceptionHandler,局部异常处理,加在方法上
@InitBinder,预处理,如日期格式处理
@DateTimeFormat(pattern = "yyyy-MM-dd"),添加在实体类的属性上
后端验证可以使用hibernate validate注解,需要引包
四、Controller方法的返回值
1.String
经过视图解析器,如果在Controller方法的参数列表中写了Model或Map对象,model或map中的键值对将存在request中
但如果返回的String为"forward:index.jsp"(请求转发)或"redirect:index.jsp"(重定向),则绕过视图解析器
注意"forward:/index.jsp"或"redirect:/index.jsp"中的"/"代表服务器中项目根目录
2. ModelAndView
ModelAndView mv = new ModelAndView("error");
mv.addObject("errMsg",exception.getMessage());//数据存在request中
return mv;//经过视图解析器
3.方法上加了@ResponseBody注解
返回的数据不经过视图解析器,直接放入响应体中,向ajax响应对象时配合Jackson.jar使用,可以自动将Java对象转成json格式的字符串