SpringMVC_requestMapping
项目搭建见上篇《初识 Spring(09)---(搭建SpringMVC项目)》
1.requestMapping既可以用在方法上,也可以用在类上
文件目录:
代码:新建SpringmvcTest.java
package com.neuedu.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
/*这是类*/
public class SpringmvcTest {
/*既可以用在方法上,也可以用在类上,这是方法*/
@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="hello"> Hello Index</a>
<br></br>
<a href="test/testRequestMapping"> Hello testRequestMapping</a> //修改代码
</body>
</html>
输出:
2.method=RequestMethod.POST 限定接收用什么方式的请求
修改代码:SpringmvcTest.java
package com.neuedu.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
//@RequestMapping("/test")
/*这是类*/
public class SpringmvcTest {
@RequestMapping(value= {"/testUrl1","testUrl2"}) //新增代码(value可设置多个值)
public String testUrl() {
System.out.println("testUrl");
return "success";
}
/* method=RequestMethod.POST 限定接收用什么方式的请求*/
@RequestMapping(value="/testMethod",method=RequestMethod.POST) //新增代码
public String testMethod() {
System.out.print("testMethod...");
return "success";
}
/*既可以用在方法上,也可以用在类上,这是方法*/
@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="hello"> Hello Index</a>
<br></br>
<a href="testRequestMapping"> Hello testRequestMapping</a>
<br></br>
<a href="testMethod"> Hello testMethod</a>
<br></br>
<form action="testMethod" method="post"> //修改代码
<input type="submit" value="testMethod"> //修改代码
</form>
<br></br>
<a href="testUrl1"> Hello testUrl1</a> //修改代码
<br></br>
<a href="testUrl2"> Hello testUrl2</a> //修改代码
</body>
</html>
输出:点击 Hello testMethod 超链接
点击 testMethod 按钮
修改代码:@RequestMapping(value="/testMethod",method=RequestMethod.GET)
输出:
点击 Hello testMethod 超链接
点击 testMethod 按钮
3.@RequestMapping:用来接收前端传递过来的数据
修改代码:SpringmvcTest.java
package com.neuedu.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
//@RequestMapping("/test")
/*这是类*/
public class SpringmvcTest {
//RequestParam注解 新增代码
@RequestMapping("/testParams")
public String testParams(@RequestParam("name") String username,
@RequestParam("pwd") String password) { //新增代码
System.out.println("username:" + username);
System.out.println("password:" + password);
return "success"; //新增代码
}
@RequestMapping(value= {"/testUrl1","testUrl2"})
public String testUrl() {
System.out.println("testUrl");
return "success";
}
/* method=RequestMethod.POST 限定接收用什么方式的请求*/
@RequestMapping(value="/testMethod",method=RequestMethod.GET)
public String testMethod() {
System.out.print("testMethod...");
return "success";
}
/*既可以用在方法上,也可以用在类上,这是方法*/
@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="hello"> Hello Index</a>
<br></br>
<a href="testRequestMapping"> Hello testRequestMapping</a>
<br></br>
<a href="testMethod"> Hello testMethod</a>
<br></br>
<form action="testMethod" method="post">
<input type="submit" value="testMethod">
</form>
<br></br>
<a href="testUrl1"> Hello testUrl1</a>
<br></br>
<a href="testUrl2"> Hello testUrl2</a>
<br></br>
<a href="testParams?name=zhang&pwd=123"> Hello testParams</a> //修改代码
</body>
</html>
输出:点击 Hello testParams 控制台打印 username 和 password
补充:如果前端传来的 key 和方法中的参数名相同可以省略该注解
即 public String testParams(@RequestParam("name") String username, @RequestParam("pwd") String password)
可写为:public String testParams(String username,String password)
接收null
修改代码:SpringmvcTest.java
package com.neuedu.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
//@RequestMapping("/test")
/*这是类*/
public class SpringmvcTest {
@RequestMapping("/testParamsRequired") //修改代码
public String testParamsRequired( @RequestParam (value="username",required=true) String username,Integer age) {
System.out.println("testParamsRequired,username:" + username);
System.out.println("testParamsRequired,age:" + age);
return "success"; //修改代码
}
@RequestMapping("/testParams")
public String testParams(@RequestParam("name") String username,
@RequestParam("pwd") String password) {
System.out.println("username:" + username);
System.out.println("password:" + password);
return "success";
}
@RequestMapping(value= {"/testUrl1","testUrl2"})
public String testUrl() {
System.out.println("testUrl");
return "success";
}
/* method=RequestMethod.POST 限定接收用什么方式的请求*/
@RequestMapping(value="/testMethod",method=RequestMethod.GET)
public String testMethod() {
System.out.print("testMethod...");
return "success";
}
/*既可以用在方法上,也可以用在类上,这是方法*/
@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="hello"> Hello Index</a>
<br></br>
<a href="testRequestMapping"> Hello testRequestMapping</a>
<br></br>
<a href="testMethod"> Hello testMethod</a>
<br></br>
<form action="testMethod" method="post">
<input type="submit" value="testMethod">
</form>
<br></br>
<a href="testUrl1"> Hello testUrl1</a>
<br></br>
<a href="testUrl2"> Hello testUrl2</a>
<br></br>
<a href="testParams?name=zhang&pwd=123"> Hello testParams</a>
<br></br>
<a href="testParamsRequired?username=zhang"> Hello testParamsRequired</a> //新增代码
</body>
</html>
输出:点击 Hello testParamsRequired 控制台打印 username 和 age
接收实体类
修改代码:SpringmvcTest.java
package com.neuedu.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.neuedu.springmvc.po.User;
@Controller
//@RequestMapping("/test")
/*这是类*/
public class SpringmvcTest {
/*实体类*/
@RequestMapping("/testPojo") //新增代码
public String testPojo(User user) {
System.out.println(user); //新增代码
return "success";
}
@RequestMapping("/testParamsRequired")
public String testParamsRequired( @RequestParam (value="username",required=true) String username,Integer age) {
System.out.println("testParamsRequired,username:" + username);
System.out.println("testParamsRequired,age:" + age);
return "success";
}
@RequestMapping("/testParams")
public String testParams(@RequestParam("name") String username,
@RequestParam("pwd") String password) {
System.out.println("username:" + username);
System.out.println("password:" + password);
return "success";
}
@RequestMapping(value= {"/testUrl1","testUrl2"})
public String testUrl() {
System.out.println("testUrl");
return "success";
}
/* method=RequestMethod.POST 限定接收用什么方式的请求*/
@RequestMapping(value="/testMethod",method=RequestMethod.GET)
public String testMethod() {
System.out.print("testMethod...");
return "success";
}
/*既可以用在方法上,也可以用在类上,这是方法*/
@RequestMapping("/testRequestMapping")
public String testRequestMapping() {
System.out.println("testRequestMapping...");
return "success";
}
}
index.jsp(支持连缀写法--address.province / address.city)
<%@ 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="hello"> Hello Index</a>
<br></br>
<a href="testRequestMapping"> Hello testRequestMapping</a>
<br></br>
<a href="testMethod"> Hello testMethod</a>
<br></br>
<form action="testMethod" method="post">
<input type="submit" value="testMethod">
</form>
<br></br>
<a href="testUrl1"> Hello testUrl1</a>
<br></br>
<a href="testUrl2"> Hello testUrl2</a>
<br></br>
<a href="testParams?name=zhang&pwd=123"> Hello testParams</a>
<br></br>
<a href="testParamsRequired?username=zhang"> Hello testParamsRequired</a>
<br></br>
<form action="testPojo" method="post"> //新增代码
username: <input type="text" name="username"/><br> //新增代码
password: <input type="text" name="password"/><br> //新增代码
gender: <input type="radio" name="gengder" value="male"/>男<br><input type="radio" name="gender" value="female"/>女<br>
email: <input type="text" name="email"/><br> //新增代码
phone: <input type="text" name="phone"/><br>
province: <input type="text" name="address.province"/><br>
city: <input type="text" name="address.city"/><br> //新增代码
<input type="submit" value="testPojo"> //新增代码
</form>
</body>
</html>
新增代码:User.java
package com.neuedu.springmvc.po;
public class User {
private String username;
private String password;
private String gender;
private String email;
private String phone;
private Adderss address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Adderss getAddress() {
return address;
}
public void setAddress(Adderss address) {
this.address = address;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", gender=" + gender + ", email=" + email
+ ", phone=" + phone + ", address=" + address + "]";
}
}
新增 Address.java
package com.neuedu.springmvc.po;
public class Adderss {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Adderss [province=" + province + ", city=" + city + "]";
}
}
输出:
注意:private String username; 中的 username 必须和
(<form action="testPojo" method="post"> username: <input type="text" name="username"/><br>)中的 username 相同