SpringMVC
1.介绍
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的Spring MVC框架或集成其他MVC开发框架
2.入门
(1)新建maven的web项目
(2)在pom.xml中配置所需的maven依赖及Tomcat插件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.2.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>springmvc001</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
(3)配置web.xml文件
① 配置前端控制器
<!--前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
② 配置前端控制初始化参数,加载spring的配置文件
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
(4)配置bean.xml
① 开启注解扫包
<context:component-scan base-package="com"></context:component-scan>
② 配置视图解析器的bean
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
③ 开启SpringMVC的注解
mvc:annotation-driven</mvc:annotation-driven>
(5)新建Controller类
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("HelloController.hello");
return "success";
}
}
3.组件
(1)前端控制器:DispatcherServlet
(2)处理器映射器:HandllerMapping
(3)处理器:Handler
(4)处理器适配器:HandlerAdpater
(5)视图解析器:ViewResolver
(6)视图:View
4.原理:
5.细节问题
(1)支持的数据类型:基本数据类型与String,POJO,Collection或Array
(2)类型转换器
① 内置类型转换器:org.springframework.core.convert.support 包下
② 自定义类型转换器:SpringMVC对日期类型的转换
参考https://www.cnblogs.com/lcngu/p/5785805.html
(3)参数乱码问题
① get方式:对tomcat7-maven-plugin配置UTF-8
② post方式:在web.xml中配置字符过滤器
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(4)ServletAPI作为参数
① HttpServletRequest
② HttpServletResponse
③ HttpSession
(5)常用注解
① @RequestMapping
② @RequestParam
③ @RequestBody:post方法
④ @PathVariable
1)REST设计风格的URL
a.GET获取资源
b.POST新建资源
c.PUT更新资源
d.DELETE删除资源
2)配置 HiddentHttpMethodFilter 过滤器
a.使用post请求
b.提供_method参数
<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>
⑤ RequestHeader
⑥ @CookieValue
⑦ @SessionAttribute
⑧ @ModelAttribute
(6)响应结果
① ServletAPI源生实现:跳转,重定向
② return “forward:/WEB-INF/pages/hello.jsp”
③ return “redirect:hello”
④ return ModelAndView
1)addObject()
2)setViewName;
⑤ @RequestBody
1)@RequestBody
2)json
<script type="text/javascript">
function sbmt(){
var json = {name:"zhangsi",age:18,id:1003};
$.ajax(
{
type:"post",//发送的方式
url:"/json/test02",//发送到的 地址
data:JSON.stringify(json),//json 格式的 数据内容
contentType:"application/json;charset=utf-8",//发送的数据类型 及编码
dataType:"json",//接收的数据类型
success:function(msg){//回调
console.dir(msg.name);
}
}
);
}
</script>
@Controller
@RequestMapping("/json")
public class JsonController {
@RequestMapping(value = "/test01",method = RequestMethod.POST)
@ResponseBody
public Map<String,String> test01(@RequestBody String param){
Map<String,String> map = new HashMap<>();
System.out.println("param = " + param);
map.put("name","zx");
map.put("age","11");
return map;
}
@RequestMapping("/test02")
@ResponseBody
public User2 test02(@RequestBody User2 user){
System.out.println("user = " + user);
return user;
}
}
3)jackson的maven依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
(7)文件上传:导入io,fileupload对应的maven依赖
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
① 本地上传
1)MultipartFile对象:transferTo()
2)配置文件解析器的bean
id是固定的
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
</bean>
(8)异常处理
① 自定义异常类
② 异常处理器
1)实现 HandlerExceptionResolver
2)配置异常处理器的bean
(9)拦截器
① 与过滤器的区别
② 自定义拦截器:实现HandlerInterceptor接口
③ 在配置文件中配置拦截器:mvc:interceptors