特点
- 是构建Web应用程序的全功能MVC模块。
- 提供了一个DispatcherServlet作用前端控制器来分派请求,同时提供灵活的配置处理程序映射、视图解析、语言环境和主题解析,并支持文件上传。
- 还包含了多种视图技术,例如JSP、Velocity、Tiles、iText和POI等。
- 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
- 核心思想:业务数据抽取和业务数据呈现分离。
- 流程
基本配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.hjg.springmvc">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
文件上传
- mvc的xml中配置CommonsMultipartResovler。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="resolveLazily" value="true"/>
</bean>
- Controller需要使用MultipartFile类接收文件,包括上传文件和接收上传文件的操作。
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String showUploadPage() {
return "file";
}
@RequestMapping(value = "/doUpload", method = RequestMethod.POST)
public String doUploadFile(@RequestParam("file") MultipartFile file) throws IOException {
if (!file.isEmpty()) {
FileUtils.copyInputStreamToFile(file.getInputStream(),
new File("C:\\temp\\", System.currentTimeMillis() + file.getOriginalFilename()));
}
return "success";
}
<div align="center">
<h1>上传附件</h1>
<form method="post" action="/doUpload" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</div>
处理JSON
- mvc的xml中配置ContentNegotiatingViewResolver,可以用同样的数据呈现不同的view。
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1"/>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
<entry key="htm" value="text/html"/>
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true"/>
</bean>
- Controller中使用@ResponseBody返回json。
@RequestMapping(value = "viewInJson/{courseId}", method = RequestMethod.GET)
public @ResponseBody
Course getCourseInJson(@PathVariable Integer courseId) {
return courseService.getCoursebyId(courseId);
}
过滤器和拦截器
过滤器
- web.xml中配置CharacterEncodingFilter,将请求字符转为utf-8.
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
拦截器
- 拦截器统一拦截从浏览器发往控制器( Controller ) 的请求来完成功能的增强。
使用
- 编写拦截器类,实现HandlerInterceptor接口。
public class TestIntercepter1 implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
request.setCharacterEncoding("utf8");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
modelAndView.addObject("msg", "msg from intercepter");
modelAndView.setViewName("success3");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
}
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/viewAll"/>
<bean class="com.hjg.springmvc.intercepter.TestIntercepter1"/>
</mvc:interceptor>
</mvc:interceptors>
多个拦截器
- 拦截器的顺序和mvc的xml中配置顺序一致。
- 拦截器的执行顺序,类似栈。
两者区别
- 过滤器依赖于 servlet容器,基于回调函数,过滤范围大。
- 拦截器依赖于框架容器,基于反射机制,只过滤请求。
请求处理
参考