spring mvc 参数传递的三种方式
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- spring mvc 注解驱动 --> <mvc:annotation-driven /> <!-- 扫描器 --> <context:component-scan base-package="com" /> <!-- 配置视图 解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀和后缀 --> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
controller
package com.stone.controller; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.stone.bean.UserInfo; /** * user/list.do user/add.do */ @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/add1.do") public String add(HttpServletRequest request) { String userNum = request.getParameter("userNum"); String passWord = request.getParameter("passWord"); System.out.println("userNum:" + userNum + " passWord:" + passWord); return "user_add1"; } @RequestMapping("/add2.do") public String add2( @RequestParam(value = "number", defaultValue = "123") String userNum, String passWord) { System.out.println("userNum:" + userNum + " passWord:" + passWord); return "user_add1"; } @RequestMapping("/add3.do") public String add3(UserInfo user) { System.out.println(user); return "user_add1"; } @RequestMapping("/add4.do") public String add4(Integer userId, Date userTime) { System.out.println(userId); System.out.println(userTime); return "user_add1"; } /** * 时间属性编辑器 */ // @InitBinder // public void initBinder(ServletRequestDataBinder bin) { // SimpleDateFormat dateFormat = new SimpleDateFormat( // "yyyy-MM-dd HH:mm:ss"); // CustomDateEditor cust = new CustomDateEditor(dateFormat, true); // bin.registerCustomEditor(Date.class, cust); // } }
jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>hello</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <h1>用户信息添加</h1> <form action="user/add1.do" method="post"> 账号:<input name="userNum" type="text" /> 密码:<input name="passWord" type="text" /> <input type="submit" /> </form> <h1>用户信息添加</h1> <form action="user/add2.do" method="post"> 账号:<input name="number" type="text" /> 密码:<input name="passWord" type="text" /> <input type="submit" /> </form> <h1>用户信息添加</h1> <form action="user/add3.do" method="post"> 账号:<input name="userId" type="text" /><br /> 姓名:<input name="userName" type="text" /> 性别:<input name="userSex" type="text" /> 兴趣爱好: <input type="checkbox" name="userxq" value="LOL"/>LOL <input type="checkbox" name="userxq" value="英雄"/>英雄 <input type="checkbox" name="userxq" value="DOTA"/>DOTA<br/> 时间:<input name="userTime" type="text" /> <input type="submit" /> </form> <h1>用户信息添加</h1> <form action="user/add4.do" method="post"> 账号:<input name="userId" type="text" /><br /> 时间:<input name="userTime" type="text" /> <input type="submit" /> </form> </body> </html>
java bean
package com.stone.bean; import java.util.Arrays; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; public class UserInfo { private Integer userId; private String userName; private String userSex; private String[] userxq; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date userTime; @Override public String toString() { return "UserInfo [userId=" + userId + ", userName=" + userName + ", userSex=" + userSex + ", userxq=" + Arrays.toString(userxq) + ", userTime=" + userTime + "]"; } public Date getUserTime() { return userTime; } public void setUserTime(Date userTime) { this.userTime = userTime; } public String[] getUserxq() { return userxq; } public void setUserxq(String[] userxq) { this.userxq = userxq; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; } }