Spring MVC中每个控制器中可以定义多个请求处理方法,我们把这种请求处理方法简称为Action,每个请求处理方法可以有多个不同的参数,以及一个多种类型的返回结果。
一、Action参数类型
1.1、自动参数映射
1.1.1、基本数据类型
方法的参数可以是任意基本数据类型,如果方法参数名与http中请求的参数名称相同时会进行自动映射,视图user目录下的index.jsp与示例代码如下:
package cn.liuw.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
// 自动参数映射
@RequestMapping("/index")
public String index(Model model, int id, String name) {
model.addAttribute("message", "name=" + name + ",id=" + id);
return "user/index";
}
}
<%@ page language="java" contentType="text/html;utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>User</title>
</head>
<body>
${message}
</body>
</html>
运行结果如下:
1.1.2、自定义数据类型
除了基本数据类型,也可以自定义的数据类型,如一个自定义的POJO对象,Spring MVC会通过反射把请中的参数设置到对象中,转换类型,示例代码如下:
package cn.liuw.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.liuw.domain.User;
@Controller
@RequestMapping("/user")
public class UserController {
// 自动参数映射自定义数据类型
@RequestMapping("/idx")
public String index(Model model, User user) {
model.addAttribute("message", user);
return "user/index";
}
}
package cn.liuw.domain;
public class User {
private int id;
private String name;
public User() {
}
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行结果:
示例中使用的是的URL中的参数,其实也可以是客户端提交的任意参数,特别是表单中的数据。
1.1.3、复杂数据类型
这里指的复杂数据类型指的是一个自定义类型中还包含另外一个对象类型,如用户类型中包含产品对象:
package cn.liuw.domain;
public class User {
private int id;
private String name;
private Product product;
public User() {
}
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
class Product {
private int pid;
private String pname;
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
@Override
public String toString() {
return "Product [pid=" + pid + ", pname=" + pname + "]";
}
}
复杂数据类型映射示例:
@RequestMapping("/idx2")
public String index2(Model model, User user) {
model.addAttribute("message", user.getName()+","+user.getProduct().getPname());
return "user/index";
}
运行结果:
为了方便这里我使用的是url,这里当然可以是一个表单,如下代码所示:
<form method="post" action="/user/idx3">
username:<input name="username" /><br/>
pdctname:<input name="product.name" /><br/>
<button>提交</button>
</form>
1.1.4、数组
浏览器请求传递参数:http://localhost:8080/user/idx01?ids=1&ids=2&ids=3
第一种:
代码中用数组Integer[] ids接收,如下:
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/idx01")
public String index2(Model model, Integer[] ids) {
model.addAttribute("message", Arrays.toString(ids));
return "user/index";
}
}
第二种:
用集合List<Integer> id接收,但是List集合必须得用其他类包装起来。如下:
包装类:
public class User {
private int id;
private String name;
List<Integer> ids;
//getter and setter
}
代码类:
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/idx02")
public String index2(Model model, User user) {
model.addAttribute("message", user);
return "user/index";
}
}
以上两种浏览器url传参一样,接收不一样。