简单类型参数的绑定
package com.mr.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.mr.entity.Users;
/**
* Servlet implementation class UsersController
*/
@Controller
public class UsersController {
@RequestMapping(value = "/qw")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
List<Users> listU = UsersService();
mv.addObject("listU", listU);
mv.setViewName("usersList");
return mv;
}
//简单类型参数的绑定
//无注解参数绑定
@RequestMapping(value = "/test")
public void getParamter(Integer id) throws Exception { //参数名必须为id,即形参名与参数名必须相同
System.out.println(id);
}
//使用注解参数绑定
@RequestMapping(value = "/testParam")
// 取参数名为id的值 ,形参名可以与参数名不同
public void getTestParamter(@RequestParam(value = "id") Integer uid_1) throws Exception {
System.out.println(uid_1);
}
//使用默认值绑定
@RequestMapping(value = "/TestParam")
//取参数名为id的值, 如果没有,则形参的值默认为 1(defaultValue)
public void GetTestParamter(@RequestParam(value = "id", defaultValue = "1") Integer uid_1) throws Exception {
System.out.println(uid_1);
}
//返回User对象的列表
public List<Users> UsersService() {
List<Users> list = new ArrayList<>();
Users u = new Users();
u.setName("zhangsan");
u.setAge(30);
u.setTel("138365984");
Users u1 = new Users();
u1.setName("zhang");
u1.setAge(23);
u1.setTel("562620354");
list.add(u);
list.add(u1);
return list;
}
}
jsp文件中
<%@ page language="java" import="java.sql.*"
contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>姓名</td><td>年龄</td><td>电话</td>
</tr>
<c:forEach items="${listU}" var="list">
<tr>
<td>${list.name }</td>
<td>${list.age }</td>
<td>${list.tel }</td>
</tr>
</c:forEach>
</table>
<a href="test?id=123">无注解参数</a>
<a href="testParam?id=456">使用注解参数绑定</a>
<a href="TestParam?pid=789">默认值参数绑定</a>
</body>
</html>
包装类型参数绑定
package com.mr.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.mr.entity.Users1;
@Controller
@RequestMapping("usersController")
public class UsersController1 {
@RequestMapping("/selUser")
public ModelAndView getAllUser(Users1 users) throws Exception {
// TODO Auto-generated method stub
List<Users1> listU = null;
if (users == null || users.getName() == null || users.getName().equals("")) {
listU = UsersService(); // 如果users为空 ,则将UserService的返回值赋给 listU
} else {
listU = setUser(users); // 返回的是 查询的结果的列表
}
ModelAndView mav = new ModelAndView();
mav.addObject("listU", listU);
mav.setViewName("usersList4");
return mav;
}
// 模拟Service的内部类查询所有的方法
public List<Users1> UsersService() {
List<Users1> list = new ArrayList<>();
Users1 u = new Users1();
u.setName("Steven");
u.setAge(30);
u.setTel("138********");
Users1 u1 = new Users1();
u1.setName("MR");
u1.setAge(10);
u1.setTel("12345678");
list.add(u);
list.add(u1);
return list; // 返回的是一个列表
}
// 包装类型参数绑定
// 模拟Service的内部类模糊查询方法
public List<Users1> setUser(Users1 users) {
// 获取查询条件users对象中name的属性值
String uName = users.getName();
// 获取模拟数据库中Users表中所有数据
List<Users1> listU = UsersService(); // 获取列表
// 创建一个空对象用于存返回值用
Users1 users1 = null;
// 创建一个空集合用于存所有符合条件的Users对象
List<Users1> selU = new ArrayList<>();
// 将我们的查询条件与数据库中表中name字段所有值进行比较
for (int i = 0; i < listU.size(); i++) {
if (listU.get(i).getName().contains(uName)) {
users1 = listU.get(i);
selU.add(users1);
}
}
return selU;
}
}
jsp文件
<body>
<form action="getArrayTest2">
<table>
<tr>
<td>姓名</td>
<td>年龄</td>
<td>电话</td>
</tr>
<c:forEach items="${listU }" var="list" varStatus="status">
<tr>
<td><input name="listU[${status.index }].name" value="${list.name }"></td>
<td><input name="listU[${status.index }].age" value="${list.age }"></td>
<td><input name="listU[${status.index }].tel" value="${list.tel }"></td>
</tr>
</c:forEach>
</table><br/>
<input type="submit" value="提交测试"/>
</form>
</body>
集合类型的参数绑定
数组类型的请求参数与List类型的请求参数与ListQryModel实体类
// List类型请求参数
@RequestMapping("/getArrayTest2")
public void arrayTest(ListQryModel listQryModel) {
List<Users1> list = listQryModel.getListU();
for (int i = 0; i < list.size(); i++) {
System.out.println("list[" + i + "].name=" + list.get(i).getName());
}
}
// 数组类型的请求参数
@RequestMapping(value = "/getArrayTest")
public void arryList(int[] id) {
for (int i = 0; i < id.length; i++) {
System.out.println("id[" + i + "]" + id[i]);
}
}
}
package com.mr.entity;
import java.util.List;
public class ListQryModel {
private List<Users1> listU;
public List<Users1> getListU() {
return listU;
}
public void setListU(List<Users1> listU) {
this.listU = listU;
}
}
jsp文件
<body>
<form action="getArrayTest3">
<table>
<tr>
<td>名称</td>
<td>年龄</td>
<td>电话</td>
</tr>
<tr>
<td><input name="userMap['name']" value="LILY" /></td>
<td><input name="userMap['age']" value="18" /></td>
<td><input name="userMap['tel']" value="130********" /></td>
</tr>
</table>
<br /> <input type="submit" value="批量提交" />
</form>
</body>
Map类型的请求参数与MapQryModel实体类
//Map类型请求参数
@RequestMapping("/getArrayTest3")
public void arrayTest(MapQryModel mapQryModel) {
Map<String, Object> userMap = mapQryModel.getUserMap();
for (String key : userMap.keySet()) {
System.out.println("userMap[" + key + "]=" + userMap.get(key));
}
}
package com.mr.entity;
import java.util.Map;
public class MapQryModel {
private Map<String,Object> userMap;
public Map<String, Object> getUserMap() {
return userMap;
}
public void setUserMap(Map<String, Object> userMap) {
this.userMap = userMap;
}
}
jsp文件
<form action="getArrayTest">
<table>
<tr>
<td>选择</td>
<td>名称</td>
<td>年龄</td>
<td>电话</td>
</tr>
<c:forEach items="${listU }" var="list">
<tr>
<td><input type="checkbox" name="id" value="${list.id }"/></td>
<td>${list.name }</td>
<td>${list.age }</td>
<td>${list.tel }</td>
</tr>
</c:forEach>
</table><br/>
<input type="submit" value="批量提交"/>
</form>