1、使用传统HttpServletRequest、HttpServletResponse对象
可以在业务控制方法中书写传统web参数,这种方式并不提倡,因为耦合了Servlet的API。SpringMVC与Struts2一样,也尽量与Servlet API解耦。
@Controller @RequestMapping(value="/user") public class UserAction { @RequestMapping(value="/add") public String add(HttpServletRequest request, HttpServletResponse response) throws Exception{ Integer id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); Double sal = Double.parseDouble(request.getParameter("sal")); System.out.println(id + " - " + name + " - " + sal); return "/success.jsp"; } }
在浏览器地址栏输入:
http://127.0.0.1:8080/springmvc02/user/add.action?id=1&name=Tomcat&sal=123.45
控制台输出:
1 - Tomcat - 123.45
2、在方法中添加多个参数接收各个表单数据
在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型
准备JSP页面
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta name="content-type" content="text/html; charset=UTF-8"> <title>Index Page</title> </head> <body> <form action="${pageContext.request.contextPath}/user/add.action" method="post"> <table> <tr> <td>ID:</td> <td><input type="text" name="id"/></td> </tr> <tr> <td>姓名:</td> <td><input type="text" name="name"/></td> </tr> <tr> <td>薪水:</td> <td><input type="text" name="sal"/></td> </tr> <tr> <td>入职时间:</td> <td><input type="text" name="hiredate"/></td> </tr> <tr> <td> <input type="submit" value="提交"/> </td> </tr> </table> </form> </body> </html>
UserAction.java
@Controller @RequestMapping(value="/user") public class UserAction { @RequestMapping(value="/add") public String add(Model model, int id/*数值·整型*/,String name/*字符串*/,double sal/*数值·双精度*/,Date hiredate) throws Exception{ System.out.println(id + " - " + name + " - " + sal + " - " + hiredate); model.addAttribute("message", "添加成功!"); return "/success.jsp"; } }
此时提交数据,会出现错误:
仔细看一下错误:无法将java.lang.String转换成java.util.Date。
使用@InitBind来解决字符串转日期类型,添加如下方法(方法名可以任意):
@InitBinder private void xxxx(ServletRequestDataBinder binder){ binder.registerCustomEditor( Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); }
完整的UserAction.java
package com.rk.action; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping(value="/user") public class UserAction { @InitBinder private void xxxx(ServletRequestDataBinder binder){ binder.registerCustomEditor( Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); } @RequestMapping(value="/add") public String add(Model model, int id/*数值·整型*/,String name/*字符串*/,double sal/*数值·双精度*/,Date hiredate) throws Exception{ System.out.println(id + " - " + name + " - " + sal + " - " + hiredate); model.addAttribute("message", "添加成功!"); return "/success.jsp"; } }
控制台输出:
1 - 小明 - 123.45 - Sun Oct 10 00:00:00 CST 2010
3、在方法中添加一个JavaBean参数接收各个表单数据
UserAction.java
package com.rk.action; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import com.rk.entity.User; @Controller @RequestMapping(value="/user") public class UserAction { @InitBinder private void initBinder(ServletRequestDataBinder binder){ binder.registerCustomEditor( Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); } @RequestMapping(value="/add") public String add(Model model, User user) throws Exception{ model.addAttribute("message", "添加成功!"); model.addAttribute("user", user); return "/success.jsp"; } }
User.java
package com.rk.entity; import java.util.Date; public class User { private Integer id; private String name; private Double sal; private Date hiredate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", sal=" + sal + ", hiredate=" + hiredate + "]"; } }
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta name="content-type" content="text/html; charset=UTF-8"> <title>Success Page</title> </head> <body> ${message }<br> <table border="1" style="border-collapse: collapse;text-align: center;"> <tr> <th width="200">属性</th> <th width="300">值</th> </tr> <tr> <td>ID</td> <td>${user.id }</td> </tr> <tr> <td>姓名:</td> <td>${user.name }</td> </tr> <tr> <td>薪水</td> <td>${user.sal }</td> </tr> <tr> <td>入职时间</td> <td>${user.hiredate}</td> </tr> </table> </body> </html>
演示效果
4、对User和Admin再进行一次封装
假如有两个JavaBean:User.java和Admin.java,它们拥有相同的属性:id/name/sal/hiredate。
如果在UserAction.java的add方法中如下:
public String add(User user,Admin admin);
那么,当浏览器表单提交数据时,会填充哪些一个JavaBean对象呢?
经过测试,发现user和admin对象都会被填充上。
User [id=3, name=小刚, sal=555.555, hiredate=Fri Dec 12 00:00:00 CST 2014] Admin [id=3, name=小刚, sal=555.555, hiredate=Fri Dec 12 00:00:00 CST 2014]
为了区分User和Admin的表单数据,需要在服务器和浏览器进行区分。
Bean.java
package com.rk.entity; public class Bean { private User user; private Admin admin; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Admin getAdmin() { return admin; } public void setAdmin(Admin admin) { this.admin = admin; } }
UserAction.java
package com.rk.action; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import com.rk.entity.Admin; import com.rk.entity.Bean; import com.rk.entity.User; @Controller @RequestMapping(value="/user") public class UserAction { @InitBinder private void initBinder(ServletRequestDataBinder binder){ binder.registerCustomEditor( Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); } @RequestMapping(value="/add") public String add(Model model, Bean bean) throws Exception{ model.addAttribute("message", "添加成功!"); System.out.println(bean.getAdmin()); System.out.println(bean.getUser()); return "/success.jsp"; } }
index.jsp 页面上进行区分
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta name="content-type" content="text/html; charset=UTF-8"> <title>Index Page</title> </head> <body> <form action="${pageContext.request.contextPath}/user/add.action" method="post"> <table> <tr> <td>用户ID:</td> <td><input type="text" name="user.id"/></td> </tr> <tr> <td>用户姓名:</td> <td><input type="text" name="user.name"/></td> </tr> <tr> <td>用户薪水:</td> <td><input type="text" name="user.sal"/></td> </tr> <tr> <td>用户入职时间:</td> <td><input type="text" name="user.hiredate"/></td> </tr> <tr> <td> <input type="submit" value="提交"/> </td> </tr> </table> <br/><br/> <table> <tr> <td>管理员ID:</td> <td><input type="text" name="admin.id"/></td> </tr> <tr> <td>管理员姓名:</td> <td><input type="text" name="admin.name"/></td> </tr> <tr> <td>管理员薪水:</td> <td><input type="text" name="admin.sal"/></td> </tr> <tr> <td>管理员入职时间:</td> <td><input type="text" name="admin.hiredate"/></td> </tr> <tr> <td> <input type="submit" value="提交"/> </td> </tr> </table> </form> </body> </html>
5、接收数组参数
批量删除
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta name="content-type" content="text/html; charset=UTF-8"> <title>Index Page</title> </head> <body> <form action="${pageContext.request.contextPath}/user/add.action" method="post"> <table> <tr><td> <input type="checkbox" name="ids" value="1"/>第一项 </td></tr> <tr><td> <input type="checkbox" name="ids" value="2"/>第二项 </td></tr> <tr><td> <input type="checkbox" name="ids" value="3"/>第三项 </td></tr> <tr><td> <input type="checkbox" name="ids" value="4"/>第四项 </td></tr> <tr><td> <input type="submit" value="删除"/> </td></tr> </table> </form> </body> </html>
UserAction.java
package com.rk.action; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping(value="/user") public class UserAction { @InitBinder private void initBinder(ServletRequestDataBinder binder){ binder.registerCustomEditor( Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); } @RequestMapping(value="/add") public String add(Model model, int[] ids) throws Exception{ String str = "需要批量删除的是:"; for(int id : ids){ str += id+" "; } model.addAttribute("message", "批量删除成功!" + str); return "/success.jsp"; } }
6、接收List<JavaBean>参数
批量注册用户
UserAction.java
@Controller @RequestMapping(value="/user") publicclass UserAction { @RequestMapping(value="/addAll") public String addAll(Bean bean,Model model) throws Exception{ for(User user :bean.getUserList()){ System.out.println(user.getName()+":"+user.getGender()); } model.addAttribute("message","批量增加用户成功"); return"/success.jsp"; } }
Bean.java
publicclass Bean { private List<User> userList = new ArrayList<User>(); public Bean(){} public List<User> getUserList() { returnuserList; } publicvoid setUserList(List<User> userList) { this.userList = userList; } }
index.jsp
<form action="${pageContext.request.contextPath}/user/addAll.action"method="POST"> 姓名:<input type="text"name="userList[0].name"value="小明"/> 性别:<input type="text"name="userList[0].gender"value="男"/> <hr/> 姓名:<input type="text"name="userList[1].name"value="小刚"/> 性别:<input type="text"name="userList[1].gender"value="男"/> <hr/> 姓名:<input type="text"name="userList[2].name"value="小红"/> 性别:<input type="text"name="userList[2].gender"value="女"/> <hr/> <input type="submit" value="批量注册"/> </form>