开发环境:idea 2018.3;springMVC 4.3.21.RELEASE
web.xml
<servlet>
<servlet-name>SpringMVCservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springMVC.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
<async-supported>trueasync-supported>
servlet>
<servlet-mapping>
<servlet-name>SpringMVCservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
springMVC.xml
<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
http://www.springframework.org/schema/beans/spring-beans-4.3.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-4.3.xsd">
<context:component-scan base-package="cn.spring.controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
@RequestMapping注解用于映射url到控制器类或一个特定的处理程序方法。可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径,被称作窄化请求映射。
该注解共有8个属性,注解源码如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
@AliasFor("path")
String[] value() default {};
@AliasFor("value")
String[] path() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
属性 | 注解意义 |
---|---|
name | 指定映射的名称 |
value/path | 互为别名,指定请求的路径映射,指定的地址可以是uri模板 |
method | 指定请求的method类型, GET、POST、PUT、DELETE等 |
params | 指定request中必须包含某些参数值是,才让该方法处理 |
headers | 指定request中必须包含某些指定的header值,才能让该方法处理请求 |
consumes | 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html |
produces | 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回 |
注:属性值除了name都是数组类型,也就是可以同时制定多个值,每个值之间是"或"的关系
该属性是使用最频繁,最重要的一个属性,如果只指定该属性时可以把value/path省去。
@RequestMapping("/login")
public String login() {
return "login";
}
@Controller
@RequestMapping(value = "/studentServlet")
public class StudentServlet {
@RequestMapping
public String login() {
return "login";
}
访问login的路径为:http://localhost:8080/……/studentServlet,一般可用于设置项目的起始页。
使用路径变量的好处:使路径变得更加简洁;获得参数更加方便,框架会自动进行类型转换。
第一种:可以指定为含有某变量的一类值
@RequestMapping("/login/{username}")
public String login(@PathVariable String username,Model model){
model.addAttribute("message", username);
return "login";
}
第二种:可以指定为含正则表达式的一类值
@RequestMapping(value="/action4/{id:\\d{6}}-{name:[a-zA-Z]{3:12}}")
public String login(@PathVariable int id,@PathVariable String name,Model model){
model.addAttribute("message", "id:"+id+" name:"+name);
return "login";
}
ANT通配符
通配符 | 说明 |
---|---|
? | 匹配任何单个字符 |
* | 匹配0个或者任意个字符 |
** | 匹配0个或者更多的目录 |
//Ant风格路径模式
@RequestMapping(value = "/login/*.do")
public String login(Model model){
model.addAttribute("message","Ant风格路径模式");
return "login";
}
所有的请求默认都会是 HTTP GET 类型的。
为了能降一个请求映射到一个特定的 HTTP 方法,需要在 @RequestMapping 中使用 method 来声明 HTTP 请求所使用的方法类型。
@Controller
@RequestMapping("/login")
public class IndexController {
@RequestMapping(method = RequestMethod.GET)
String get() {
return "Hello from get";
}
@RequestMapping(method = RequestMethod.DELETE)
String delete() {
return "Hello from delete";
}
@RequestMapping(method = RequestMethod.POST)
String post() {
return "Hello from post";
}
@RequestMapping(method = RequestMethod.PUT)
String put() {
return "Hello from put";
}
@RequestMapping(method = RequestMethod.PATCH)
String patch() {
return "Hello from patch";
}
}
@GetMapping等价于@RequestMapping(method = RequestMethod.GET)
@PostMapping等价于@RequestMapping(method = RequestMethod.POST)
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html,收窄请求范围,如果用户发送的请求内容类型不匹配则方法不会响应请求。
// 请求内容类型必须为text/html,注意浏览器默认没有指定Content-type
@RequestMapping(value = "/login",consumes="text/html")
public String login(Model model) {
model.addAttribute("message", "请求的提交内容类型(Content-Type)是text/html");
return "login";
}
produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回,方法才处理客户端的请求否则会报406错误,常用设置如下:
//客户端接收json且编码为utf-8,多数浏览器Accept设置的为*/*,接收任意类型
@RequestMapping(value = "/login",produces="application/json; charset=UTF-8")
public String login(Model model) {
model.addAttribute("message", "客户端可以接收的类型是application/json; charset=UTF-8");
return "login";
}
params:可以限制客户端发送到服务器的请求参数为某些特定值或不为某些值。
//请求的参数必须包含id=215与name不等于abc
@RequestMapping(value = "/login",params={"id","name!=abc"})
public String login(Model model) {
model.addAttribute("message", "请求的参数必须包含id与name不等于abc");
return "login";
}
headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。
//请求头部信息中必须包含Host=localhost:8088
@RequestMapping(value = "/login",headers="Host=localhost:8080")
public String login(Model model) {
model.addAttribute("message", "请求头部信息中必须包含Host=localhost:8080");
return "login";
}
转下片博文:springMVC参数绑定