RequestMapping注解的几个属性和作用位置
1.项目目录
2.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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVC02</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.springMVC-servlet.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"
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.0.xsd">
<!-- 扫描组件 -->
<context:component-scan base-package="com.atguigu.controller"></context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="test">测试springMVC</a>
<br>
<form action="test" method="post">
<input type="submit" value="测试POST">
</form>
</body>
</html>
5.success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>成功</h1>
</body>
</html>
6.TestController.java
package com.atguigu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
//@RequestMapping("/mvc")
public class TestController {
/**
* @RequestMapping:设置请求映射,把请求和控制层中的方法设置映射关系
* 当请求路径和@RequestMapping的value属性一致时,则该注解所标注的方法即为处理请求的方法
*
* @RequestMapping可以加在类上,也可以加在方法上
* 若类和方法上都加有,应该一层一层的访问,西安访问类,再访问类中的方法
*
* method:用来设置请求方式,只有客户端发送请求的方式和method的值一致,才能处理请求
* 请求方式:GET查询 POST添加 PUT修改 DELETE删除
* params:用来设置客户端传到服务器的数据,支持表达式
* username !username username=admin username!=admin
* headers:用来设置请求头信息,所发送的请求的请求头信息一定要个headers属性中所设置的一致
*/
// @RequestMapping(value="/test", method=RequestMethod.POST)
// public String testPOST(){
// System.out.println("SUCCESS:POST");
// return "success";
// }
// @RequestMapping(value="/test", method=RequestMethod.GET)
// public String testGET(){
// System.out.println("SUCCESS:GET");
// return "success";
// }
// @RequestMapping(value="/test", params={"username", "!age", "sex!=1"})
// public String testParams(){
// System.out.println("SUCCESS");
// return "success";
// }
@RequestMapping(value="/test",
headers={"Accept-Encoding=gzip, deflate"})
public String testHeaders(){
System.out.println("SUCCESS");
return "success";
}
/**
* springMVC支持Ant方式的请求路径
* 在Ant中,有3个匹配符
* *:任意字符
* ?:任意一个字符
* **:任意多层目录
*/
//访问路径举例:localhost:8080/SpringMVC02/abc/ant11/a/b/testAnt
@RequestMapping(value="/*/ant??/**/testAnt")
public String testAnt(){
System.out.println("SUCCESS:testAnt");
return "success";
}
/**
* 以前:localhost:8080/SpringMVC02/testREST?id=1001&username=admin
* 现在:localhost:8080/SpringMVC02/testREST/1001/admin
*/
@RequestMapping("/testREST/{id}/{username}")//占位符方式的路径
public String testREST(@PathVariable("id")Integer id, @PathVariable("username")String username){
System.out.println("id:"+id+",username="+username);
return "success";
}
}