spring mvc 接收表单 bean
spring 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="user.studentID" /> </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" value="login"/>
- </td>
- </tr>
- </table>
- </form>
- </center>
- </body>
- </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><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="user.studentID" /> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="login"/>
</td>
</tr>
</table>
</form> </center>
</body>
</html>
控制器LoginController 中登录的方法:
- /***
- * 校验登录用户
- *
- * @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";
- }
- }
/***
* 校验登录用户
*
* @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:
- 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;
- }
- }
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的更多相关文章
- Spring MVC与表单日期提交的问题
Spring MVC与表单日期提交的问题 spring mvc 本身并不提供日期类型的解析器,需要手工绑定, 否则会出现非法参数异常. org.springframework.beans.BeanIn ...
- spring mvc form表单提交乱码
spring mvc form表单submit直接提交出现乱码.导致乱码一般是服务器端和页面之间编码不一致造成的.根据这一思路可以依次可以有以下方案. 1.jsp页面设置编码 <%@ page ...
- spring:设置映射访问路径 或 xml配置访问路径 (spring mvc form表单)
项目hello, 在src/main/java下面建一个目录: charpter2 一.xml配置访问路径 web.xml <web-app> <display-name>Ar ...
- Spring MVC 验证表单
在实际工作中,得到数据后的第一步就是检验数据的正确性,如果存在录入上的问题,一般会通过注解校验,发现错误后返回给用户,但是对于一些逻辑上的错误,比如购买金额=购买数量×单价,这样的规则就很难使用注 ...
- Spring MVC 3 表单中文提交post请求和get请求乱码问题的解决方法
在spring mvc 3.0 框架中,通过JSP页面.HTML页面以POST方式提交表单时,表单的参数传递到对应的servlet后会出现中文显示乱码的问题.解决办法可采用spring自带的过滤技术, ...
- spring mvc 提交表单的例子
1. 构建MAVEN项目,然后转换成web格式,结构图如下: 2. 通过@RequestMapping来进行配置,当输入URL时,会以此找到对应方法执行,首先调用setupForm方法,该方法主要是生 ...
- 使用Spring MVC 的表单控制器SimpleFormController
以注册过程为例,我们可能会选择继承AbstractController来实现表单的显示,继承AbstractCommandController来实现表单的处理 ,这样是可行的,但必须要维护两个控制器 ...
- spring mvc防止表单重复提交的代码片段
1.定义一个token接口 package com.bigbigrain.token; import java.lang.annotation.Documented; import java.lang ...
- spring mvc 提交表单汉字乱码
修改web.xml添加如下信息 <filter> <filter-name>characterEncodingFilter</filter-name> <fi ...
随机推荐
- Ionic.Zip
1.Ionic.zIP 实现文件压缩和解压 2.压缩: /// <summary> /// 压缩文件 /// </summary> / ...
- 全球城市群Megalopolis
Megalopolis From Wikipedia, the free encyclopedia (Redirected from Megalopolis (city type)) &quo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- ecshop二次开发之百度地图
案例效果展示: 代码实现: 1.在ecshop后台找到文章管理->文章分类->添加文章分类,添加一个顶级分类,叫做"合作单位",并且让其显示在导航栏.如下图: 1.在e ...
- parkingLot
一个支付宝停车支付生活号前端页面 //index.html //自定义键盘 <!DOCTYPE html> <html> <head> <meta chars ...
- s3c6410时钟初始化
今天自己写bootloader做时钟初始化时遇到的问题,特记录下来.为了方便理解,我大部分都有截图, 在此我先说明下,图均来自数据手冊.也希望看了本篇文章的同志多多參看数据手冊才干理解的更加透 ...
- MyEclipse代码提示功能和自动提示功能
1.菜单window->Preferences->Java->Editor->Content Assist->Enable auto activation 选项要打上勾 ...
- python 利用抛出异常并处理的优点
- SQL语法之DDL和DML
SQL语法之DDL和DML DDL数据库定义语言 create 创建 alter 修改 drop 删除 drop和delete的区别 truncate DML 数据操作语言 insert ...
- 不撞南墙不回头———深度优先搜索(DFS)Oil Deposits
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...