MVC如何接收表单bean 呢?
之前项目中MVC框架一直用struts2,所以我也就按照struts2 的思维来思考
页面loginInput.jsp:
- <?xml version="1.0" encoding="UTF-8" ?>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Insert title here</title>
- </head>
- <body>
- <center>
- <font color="red" >${message }</font>
-
- <form action="<%=path %>/user/loginVerify">
- <table>
-
- <tr>
- <td>身份证:</td>
- <td> <input type="text" name="user.identity" /> </td>
- </tr>
- <tr>
- <td>用户编号:</td>
- <td><input type="text" name="userstudentID" /> </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" value="login"/>
- </td>
- </tr>
- </table>
- </form>
-
- </center>
-
- </body>
- </html>
控制器LoginController 中登录的方法:
-
-
-
-
-
-
-
-
-
- @RequestMapping(value = "/loginVerify")
- public String login(User user, HttpSession session,
- Map<String, Object> map,Model model) throws UnsupportedEncodingException,
- Exception {
- User user2 = null;
- if (user.getIdentity() == null) {
- map.put("message", "请输入身份证");
- return "loginInput";
- }
- map.put("identity", user.getIdentity());
- model.addAttribute("identity", user.getIdentity());
- System.out.println("identity:"+session.getAttribute("identity"));
- user2 = this.userDao.getByIdentityAndStudentID(new User(user.getIdentity(),
- user.getStudentID()));
- System.out.println("user2:" + user2);
- if (user2 != null) {
- return "welcome";
- } else {
- map.put("message", "身份证和用户编号有误,请重新登录");
- return "loginInput";
- }
-
- }
我认为页面表单中name为user.identity 和user.studentID的元素会自动注入到上述方法的变量User user 中,结果没有!!!?
实体类User:
- package com.springmvc.entity;
-
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
-
-
-
-
-
-
- @Entity
- public class User {
- private int id;
-
-
-
- private String identity;
-
-
-
- private String studentID;
- private String username;
-
- public User() {
- super();
- }
-
- public User(String identity, String studentID) {
- super();
- this.identity = identity;
- this.studentID = studentID;
- }
-
- @Id
- @GeneratedValue
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getIdentity() {
- return identity;
- }
-
- public void setIdentity(String identity) {
- this.identity = identity;
- }
-
- public String getStudentID() {
- return studentID;
- }
- public void setStudentID(String studentID) {
- this.studentID = studentID;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- }
原来,spring MVC 跟struts2的注入方式不一样!!
后来我把页面中的name属性改为identity 和studentID 就好了:
<tr>
<td>身份证:</td>
<td> <input type="text" name="identity" /> </td>
</tr>
<tr>
<td>用户编号:</td>
<td><input type="text" name="studentID" /> </td>
</tr>
这就是spring MVC与struts2 ioc不同的地方!