SpringMVCTest.java
package com.wxh.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
private static final String SUCCESS = "success";
/**
* 了解:可以使用params和 headers来更加精确的映射请求,params 和 headers支持简单的表达式
* @return
*/
@RequestMapping(value="testParamsAndHeaders",
params={"username","age!=10"}, headers={"Accept-Language=en-US,zh;q=0.8"})
public String testParamsAndHeaders(){
System.out.println("testParamsAndHeaders");
return SUCCESS;
}
/**
* 常用:使用method 属性来指定请求方式
* @return
*/
@RequestMapping(value="/testMethod",method=RequestMethod.POST)
public String testMethod(){
System.err.println("testMethod");
return SUCCESS;
}
/**
* 1.@RequestMapping 除了修饰方法,还可来修饰类
* 2.
* 1).类定义处:提供初步的请求映射信息,相对于web应用的根目录
* 2).方法处:提供进一步的细分映射信息。
* 相对于类定义处的url。若类定义处未标注@RequestMapping。则方法处标记的url相对于web应用的根目录。
* @return
*/
@RequestMapping("/testRequestMapping")
public String testRequestMapping(){
System.out.println("testRequestMapping");
return SUCCESS;
}
}
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="springmvc/testParamsAndHeaders?username=wxh&age=10">Test TestParamsAndHeaders</a>
<br><br>
<!--post请求-->
<form action="springmvc/testMethod" method="post">
<input type="submit" value="submit">
</form>
<!-- get请求 -->
<br><br>
<a href="springmvc/testMethod">Test Method</a>
<br><br>
<a href="springmvc/testRequestMapping">Test RequestMapping</a>
<br><br>
<a href="helloworld">Hello world</a>
</body>
</html>
web.xml 、springDispatcherServlet-servlet.xml、success.jsp参见博客:
https://blog.csdn.net/hoho_12/article/details/79779624