spring MVC如何接收表单bean 呢?

之前项目中MVC框架一直用struts2,所以我也就按照struts2 的思维来思考

页面loginInput.jsp:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"
  3. pageEncoding="UTF-8"%>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme() + "://"
  7. + request.getServerName() + ":" + request.getServerPort()
  8. + path + "/";
  9. %>
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  14. <title>Insert title here</title>
  15. </head>
  16. <body>
  17. <center>
  18. <font color="red" >${message }</font>
  19. <form action="<%=path %>/user/loginVerify">
  20. <table>
  21. <tr>
  22. <td>身份证:</td>
  23. <td> <input type="text" name="user.identity"  /> </td>
  24. </tr>
  25. <tr>
  26. <td>用户编号:</td>
  27. <td><input type="text" name="user.studentID"  /> </td>
  28. </tr>
  29. <tr>
  30. <td colspan="2">
  31. <input type="submit"  value="login"/>
  32. </td>
  33. </tr>
  34. </table>
  35. </form>
  36. </center>
  37. </body>
  38. </html>
<?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>
	&lt;form action="&lt;%=path %&gt;/user/loginVerify"&gt;
&lt;table&gt; &lt;tr&gt;
&lt;td&gt;身份证:&lt;/td&gt;
&lt;td&gt; &lt;input type="text" name="user.identity" /&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;用户编号:&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="user.studentID" /&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="2"&gt;
&lt;input type="submit" value="login"/&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt; &lt;/center&gt;

</body>

</html>

控制器LoginController 中登录的方法:

  1. /***
  2. * 校验登录用户
  3. *
  4. * @param session
  5. * @param user
  6. * @return
  7. * @throws UnsupportedEncodingException
  8. * @throws Exception
  9. */
  10. @RequestMapping(value = "/loginVerify")
  11. public String login(User user, HttpSession session,
  12. Map<String, Object> map,Model model) throws UnsupportedEncodingException,
  13. Exception {
  14. User user2 = null;
  15. if (user.getIdentity() == null) {
  16. map.put("message", "请输入身份证");
  17. return "loginInput";
  18. }
  19. map.put("identity", user.getIdentity());
  20. model.addAttribute("identity", user.getIdentity());
  21. System.out.println("identity:"+session.getAttribute("identity"));
  22. user2 = this.userDao.getByIdentityAndStudentID(new User(user.getIdentity(),
  23. user.getStudentID()));
  24. System.out.println("user2:" + user2);
  25. if (user2 != null) {
  26. return "welcome";
  27. } else {
  28. map.put("message", "身份证和用户编号有误,请重新登录");
  29. return "loginInput";
  30. }
  31. }
/***
* 校验登录用户
*
* @param session
* @param user
* @return
* @throws UnsupportedEncodingException
* @throws Exception
*/
@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";
}
}</pre>

我认为页面表单中name为user.identity 和user.studentID的元素会自动注入到上述方法的变量User user 中,结果没有!!!?

实体类User:

  1. package com.springmvc.entity;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.Id;
  5. /***
  6. * father class
  7. * @author huangwei
  8. *
  9. */
  10. @Entity
  11. public  class User {
  12. private int id;
  13. /**
  14. * 身份证
  15. */
  16. private String identity;
  17. /***
  18. * 用户编号
  19. */
  20. private String studentID;
  21. private String username;
  22. public User() {
  23. super();
  24. }
  25. public User(String identity, String studentID) {
  26. super();
  27. this.identity = identity;
  28. this.studentID = studentID;
  29. }
  30. @Id
  31. @GeneratedValue
  32. public int getId() {
  33. return id;
  34. }
  35. public void setId(int id) {
  36. this.id = id;
  37. }
  38. public String getIdentity() {
  39. return identity;
  40. }
  41. public void setIdentity(String identity) {
  42. this.identity = identity;
  43. }
  44. public String getStudentID() {
  45. return studentID;
  46. }
  47. public void setStudentID(String studentID) {
  48. this.studentID = studentID;
  49. }
  50. public String getUsername() {
  51. return username;
  52. }
  53. public void setUsername(String username) {
  54. this.username = username;
  55. }
  56. }
package com.springmvc.entity;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id; /***
  • father class
  • @author huangwei
/

@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不同的地方!

spring mvc 接收表单 bean的更多相关文章

  1. Spring MVC与表单日期提交的问题

    Spring MVC与表单日期提交的问题 spring mvc 本身并不提供日期类型的解析器,需要手工绑定, 否则会出现非法参数异常. org.springframework.beans.BeanIn ...

  2. spring mvc form表单提交乱码

    spring mvc form表单submit直接提交出现乱码.导致乱码一般是服务器端和页面之间编码不一致造成的.根据这一思路可以依次可以有以下方案. 1.jsp页面设置编码 <%@ page ...

  3. spring:设置映射访问路径 或 xml配置访问路径 (spring mvc form表单)

    项目hello, 在src/main/java下面建一个目录: charpter2 一.xml配置访问路径 web.xml <web-app> <display-name>Ar ...

  4. Spring MVC 验证表单

      在实际工作中,得到数据后的第一步就是检验数据的正确性,如果存在录入上的问题,一般会通过注解校验,发现错误后返回给用户,但是对于一些逻辑上的错误,比如购买金额=购买数量×单价,这样的规则就很难使用注 ...

  5. Spring MVC 3 表单中文提交post请求和get请求乱码问题的解决方法

    在spring mvc 3.0 框架中,通过JSP页面.HTML页面以POST方式提交表单时,表单的参数传递到对应的servlet后会出现中文显示乱码的问题.解决办法可采用spring自带的过滤技术, ...

  6. spring mvc 提交表单的例子

    1. 构建MAVEN项目,然后转换成web格式,结构图如下: 2. 通过@RequestMapping来进行配置,当输入URL时,会以此找到对应方法执行,首先调用setupForm方法,该方法主要是生 ...

  7. 使用Spring MVC 的表单控制器SimpleFormController

    以注册过程为例,我们可能会选择继承AbstractController来实现表单的显示,继承AbstractCommandController来实现表单的处理 ,这样是可行的,但必须要维护两个控制器 ...

  8. spring mvc防止表单重复提交的代码片段

    1.定义一个token接口 package com.bigbigrain.token; import java.lang.annotation.Documented; import java.lang ...

  9. spring mvc 提交表单汉字乱码

    修改web.xml添加如下信息 <filter> <filter-name>characterEncodingFilter</filter-name> <fi ...

随机推荐

  1. 关于layui部分表单不显示的问题(Select, checkBox)

    原因: 没有使用JS进行初始化 官方说明: https://www.layui.com/doc/base/faq.html layui.use('form', function(){ var form ...

  2. bzoj 3209 花神的数论题——二进制下的数位dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3209 可以枚举 “1的个数是...的数有多少个” ,然后就是用组合数算在多少位里选几个1. ...

  3. Hdu 2513 区间DP

    Cake slicing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. 【JZOJ3215】【SDOI2013】费用流

    ╰( ̄▽ ̄)╭ 对于一张给定的 运输网络 ,Alice 先确定一个最大流 ,如果有多种解, Alice 可以任选一种: 之后 Bob在每条边上分配单位花费 (单位花费必须是非负实数), 要求所有边的单 ...

  5. TypeScript类型检查机制

    类型推断 指不需要指定变量的类型,TS编译器可以根据某些规则自动推断出类型. 什么时候会有类型推断? 声明变量时没有指定类型 函数默认参数 函数返回值 ...... let a; // 这时自动推断为 ...

  6. 软工作业———Alpha版本第二周小结

    姓名 学号 周前计划安排 每周实际工作记录 自我打分 zxl 061425 1.进行任务分配2.实现扫码和生成二维码功能 1.对主要任务进行了划分,但还为进行给模块间的联系2.完成了扫码签到功能 90 ...

  7. 使用 store 来优化 React 组件

    在使用 React 编写组件的时候,我们常常会碰到两个不同的组件之间需要共享状态情况,而通常的做法就是提升状态到父组件.但是这样做会有一个问题,就是尽管只有两个组件需要这个状态,但是因为把状态提到了父 ...

  8. 获得CSM(Certified Scrum Master)-价值驱动交付。

    2019年越来越多的企业开始实行敏捷转型,紧随时代潮流,学习最先进的科学管理方法,找到正确的人(团队),为企业交付高价值的产品服务. 导师Ethan ,培训的课程让人收益匪浅,活到老学到老,丰富的知识 ...

  9. 【水滴石穿】ReactNativeDemo

    这个博主他的功底算是特别棒的了,能够把一些基础的例子,通过精巧的方式布局在一个小的demo里面 很值得我学习 放上博主的链接:https://github.com/jianiuqi/ReactNati ...

  10. 只在需要的时候 Polyfill 你的 JavaScript 代码

    本文转载自 Pascal Klau,他是一名来自德国南部的实习生,他讨厌不必要的 HTTP 请求,也不爱吃西兰花.Pascal 将说明使用 polyfill 服务的一种方式,在这种方式下你可能可以完全 ...