一、SpringMVC使用
1.pom导入
spring-mvc相关依赖,将原有的spring-context覆盖,因为这里面包含了
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
导入jstl依赖
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
2.在WEB-INF下添加springmvc-servlet.xml(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" xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 通过context:component-scan元素扫描指定包下的控制器-->
<!--1) 扫描com.javaxl.zf及子子孙孙包下的控制器(扫描范围过大,耗时)-->
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.javaxl.ssm"/>
<!--2) 此标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<!--两个bean,这两个bean是spring MVC为@Controllers分发请求所必须的。并提供了数据绑定支持,-->
<!--@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--3) ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- viewClass需要在pom中引入两个包:standard.jar and jstl.jar -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--4) 单独处理图片、样式、js等资源 -->
<!--<mvc:resources location="/css/" mapping="/css/**"/>-->
<mvc:resources location="/images/" mapping="/images/**"/>
<!--<mvc:resources location="/js/" mapping="/js/**"/>-->
</beans>
3.web.xml中进行相关配置
<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_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 读取Spring上下文的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC servlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--此参数可以不配置,默认值为:/WEB-INF/springmvc-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!--web.xml 3.0的新特性,是否支持异步-->
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
SpringMVC工作流程
1 首先用户发送请求-->DispatherServlet
2 DispatcherServlet-->HandlerMapping
3 DispatcherServlet-->HandlerAdapter
4 HandlerAdapter-->处理器功能处理方法的调用
5 ModelAndView的逻辑视图名-->ViewRecolver
6 View-->渲染
7 返回控制权给DispatcherServlet,由DispatcherServlet返回呼应给用户,流程结束
二、返回值处理
体验一下基本的请求响应方式
package com.csf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
这是子控制器的模板,相关注解会在后面讲解
重点关注返回值
转发到页面:就是上面这种写法,直接返回String类型,会转发到对应页面,例如上面这个,会转发到hello.jsp
重定向到页面:"redirect:hello"
转发到子控制器:"/book/list"
重定向到子控制器:"redirect:/book/list"
三、静态资源配置
在SpringMVC中,一般的访问地址都会被识别为一个请求,所以在处理时会出错,各种静态资源访问需要额外配置
<mvc:resources location="/static/" mapping="/static/**"/>
这代表static..的地址会识别为静态资源
四、常用注解
1 @Controller:用于标识处理器类
2 @RequestMapping:请求到处理器功能方法的映射规则,可定义到类和方法
常用参数:value、method
可将@RequestMapping标签定义到类名处窄化路径
3 @RequestParam:请求参数到处理器功能处理方法的方法参数上的绑定
常用参数:value、required、defaultValue
注:required设置成false的参数类型必须是引用类型,因为基本数据类型是不能为null的
4 @ModelAttribute:请求参数到命令对象的绑定
常用参数:value
4.1 可用@ModelAttribute标注方法参数,方法参数会被添加到Model对象中(作用:向视图层传数据)
4.2 可用@ModelAttribute标注一个非请求处理方法,此方法会在每次调用请求处理方法前被调用(作用:数据初始化)
4.3 可用@ModelAttribute标注方法,方法返回值会被添加到Model对象中(作用:向视图层传数据)
但此方法视图的逻辑图就会根据请求路径解析,例如:a/test42 --> /WEB-INF/a/test42.jsp
太麻烦几乎不用,不用直接保存到Model或ModelAndView中
5 @SessionAttributes:指定ModelMap中的哪些属性需要转存到session
常用参数:value、types
注1:必须放到class类名处
6 @InitBinder(本章暂不介绍):用于将请求参数转换到命令对象属性的对应类型
7 @RequestBody(重要~~~~~):用于目前比较流行的ajax开发的数据绑定(即提交数据的类型为json格式)
注1:使用@RequestBody注解的时候,前台的Content-Type必须要改为application/json,
如果没有更改,前台会报错415(Unsupported Media Type)。
后台日志就会报错Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported。
这些错误Eclipse下Tomcat是不会显示错误信息的,只有使用了日志才会显示
$.ajax({
url : "jsontest",
type : "POST",
async : true,
contentType : "application/json",
data : JSON.stringify(json),
dataType : 'json',
success : function(data) {
if (data.userstatus === "success") {
$("#errorMsg").remove();
} else {
if ($("#errorMsg").length <= 0) {
$("form[name=loginForm]").append(errorMsg);
}
}
}
});
其它
@CookieValue cookie数据到处理器功能处理方法的方法参数上的绑定
@RequestHeader:请求头(header)数据到处理器功能处理方法的方法参数上的绑定
@RequestBody:请求的body体的绑定(通过HttpMessageConverter进行类型转换);
@ResponseBody:处理器功能处理方法的返回值作为响应体(通过HttpMessageConverter进行类型转换);
@ResponseStatus:定义处理器功能处理方法/异常处理器返回的状态码和原因;
@ExceptionHandler:注解式声明异常处理器;
@PathVariable:请求URI中的模板变量部分到处理器功能处理方法的方法参数上的绑定,