赞
踩
本篇将介绍SpringMVC中如何为Controller配置处理URL路径、请求参数等等。
Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请求。
在控制器的类定义及方法定义处都可标注@RequestMapping
@RequestMapping 常用属性:
它们之间是与的关系,联合使用多个条件可让请求映射更加精确化。
DispatcherServlet 截获请求后,就通过控制器上 @RequestMapping 提供的映射信息确定请求所对应的处理方法。
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/login")
public String login(@RequestParam(value="username", required=true) String username, @RequestParam(value="password", required=true)String password) {
System.out.println("login username:"+username+",password:"+password);
return "login";
}
}
http://localhost:8080/webapp/user/login?username=ricky&password=123
在处理方法入参处使用 @RequestParam 可以把请求参数传递给请求方法。
@RequestParam 包含3个属性:
@RequestMapping("/login")
public String login(@RequestParam(value="username", required=true) String username, @RequestParam(value="password", required=true)String password) {
System.out.println("login username:"+username+",password:"+password);
return "login";
}
http://localhost:8080/webapp/user/login?username=ricky&password=123
@RequestParam value属性值可以与控制器处理方法的形参名称不一致,例如:
@RequestMapping("/logout")
public String logout(@RequestParam(value="username", required=true) String name){
System.out.println("logout username:"+name);
return "logout";
}
http://localhost:8080/webapp/user/logout?username=demo
通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx”) 绑定到操作方法的入参中。
@RequestMapping("/login/{username}")
public String login(@PathVariable("username") String username) {
System.out.println("login username:"+username);
return "login";
}
http://localhost:8080/webapp/restuser/login/ricky
请求头包含了若干个Header属性,服务器可据此获知客户端的信息,通过 @RequestHeader 即可将请求头中的属性值绑定到处理方法的入参中
@RequestMapping("/header")
public String header(@RequestHeader("Accept-Encoding") String encoding, @RequestParam("name") String name){
System.out.println("header encoding:"+encoding);
return "header";
}
http://localhost:8080/webapp/param/header?name=abc
@CookieValue 可• 让处理方法入参绑定某个 Cookie 值。
@RequestMapping("/cookie")
public String cookie(@CookieValue("JSESSIONID") String sessionId, @RequestParam("age") int age){
System.out.println("cookie sessionId:"+sessionId);
return "login";
}
http://localhost:8080/webapp/param/cookie?age=21
Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值。支持级联属性。
当控制器的处理方法需要多个参数时,使用@RequestParam进行表示就显得很臃肿了,此时我们可以将请求参数映射到一个POJO对象中。
1、用户注册请求(POST表单提交)
注册页面 register_form.jsp如下:
<!DOCTYPE HTML>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="param/pojo" method="post">
username: <input type="text" name="username"/>
<br>
password: <input type="password" name="password"/>
<br>
email: <input type="text" name="email"/>
<br>
age: <input type="text" name="age"/>
<br>
province: <input type="text" name="address.province"/>
<br>
city: <input type="text" name="address.city"/>
<br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
我们将表单中的请求参数映射到一个User对象中,然后将User对象传递给处理器的处理方法。
User定义如下:
package com.ricky.codelab.webapp.ch2.model;
public class User {
private String username;
private String password;
private String email;
private int age;
private Address 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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", email=" + email + ", age=" + age
+ ", address=" + address + "]";
}
}
Address定义如下:
package com.ricky.codelab.webapp.ch2.model;
public class Address {
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 "Address [province=" + province + ", city=" + city + "]";
}
}
注册请求处理器PojoController
@RequestMapping(value="/pojo", method=RequestMethod.POST)
public String register(User user){
System.out.println("pojo user:"+user);
return "login";
}
2、GET方式提交
@RequestMapping(value="/pojo_get", method=RequestMethod.GET)
public String registerByGet(User user){
System.out.println("get pojo user:"+user);
return "login";
}
Post:http://localhost:8080/webapp/register_form.jsp
Get:http://localhost:8080/webapp/param/pojo_get?username=ricky&password=123&email=ricky_feng@163.com&age=27&address.province=HuBei&&address.city=Wuhan
SpringMVC支持将Servlet API 作为入参传递给Controller的处理方法。
可接收的类型如下:
package com.ricky.codelab.webapp.ch2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/servlet")
public class ServletAPIController {
@RequestMapping("api_test")
public String register(HttpServletRequest request, HttpServletResponse response){
System.out.println("request:"+request);
return "login";
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。