1.@RequestMapping注解放置在什么位置
①放置在类上面(通常是Controller类)
②放置在方法上面(Controller类中某个具体方法)
2.如何发送请求
①发送时候直接写 类名.do
在类上面写注解RequestMapping(path/value = “类名.do”)
在类中只有一个方法 方法上面添加注解RequestMapping
TestController
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(path="TestController.do")
public class TestController{
@RequestMapping
public String testOne(){
return "welcome.jsp";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<a href="TestController.do">测试一</a>
</body>
</html>
②发送请求时候 类名字.do?method=方法名
类上面写注解(类名.do)
方法上面写注解(params={“”})
(请求结构就比较麻烦 类名.do?method=方法名)
(类的注解也很麻烦 类上一个 方法上一个 方法的这个注解还有params参数)
TestController
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(path="TestController.do")
public class TestController{
@RequestMapping(params = {"method=testOne"})
public String testOne(){
return "welcome.jsp";
}
@RequestMapping
public String testTwo(){
return "welcome.jsp";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="TestController.do?method=testOne">测试一</a>
</body>
</html>
③发送请求的时候 xxx.do (xxx通常是一个方法名)
类上面就不用写注解啦
类中方法的上面写注解 注解中写与请求对应的那个 xxx.do
TestController
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController{
@RequestMapping(path = "testOne.do")
public String testOne(){
System.out.println("123");
return "welcome.jsp";
}
public String testTwo(){
return "welcome.jsp";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="testOne.do">测试一</a>
</body>
</html>
2.@RequestMapping注解中的”方法”
path={“”,””}
(写请求的名字)
value={“”,””}
(写请求的名字)
params={“”,””}
(写携带的参数名,可以加上值,但这没什么意义;若接收到的请求中参数不是严格一一对应,则无法找到方法)
method={RequestMethod.GET}
(例如左侧写法,要求必须是get请求)
headers={“Accept-Language”,””}