问题描述
- spring MVC jsp页面获取参数 以对象的方式
-
jsp里面的body部分<h1>用户信息添加2</h1> <form action="user/add3.do" method="post"> 编号:<input type="text" name="userId"/><br/> 姓名:<input type="text" name="userName"/><br/> 性别:<input type="text" name="userSex"/><br/> <input type="submit" value="提交"/> </form>
Controller部分
@RequestMapping("/user") public class UserController { @RequestMapping("/add3.do" ) public String add3(UserInfo user) { System.out.println(user); return "user_add3"; } }
UserInfo 类
import java.util.Arrays; public class UserInfo { private Integer userId; private String userName; private String userSex; private String[] 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; } public String[] getUserXq() { return userXq; } public void setUserXq(String[] userXq) { this.userXq = userXq; } @Override public String toString() { return "UserInfo [userId=" + userId + ", userName=" + userName + ", userSex=" + userSex + ", userXq=" + Arrays.toString(userXq) + "]"; } }
已经有utf-8 编码转换
package com.liu.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; /** * Servlet Filter implementation class UTFFilter */ @WebFilter("/*") public class UTFFilter implements Filter { /** * Default constructor. */ public UTFFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here request.setCharacterEncoding("utf-8"); // pass the request along the filter chain chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
出来的界面 是3个 输入框 如果 我只在第一个输入框里输入了汉字 就报错
The request sent by the client was syntactically incorrect.
只在第一个输入框里输入的只是数字的话就不报错
其他情况都不报错
求原因
解决方案
你第一个输入框是编号,你输入中文最后肯定会报错,因为中文属于字符串类型,而你的编号是整型的。
解决方案二:
你的UserId是整型的,输入汉字为 字符串型 肯定报错啊
还有,一般id这种东西怎么有表单填写的?人为的会有重复啊,以后数据多了,够麻烦的,没这么搞得,一般都是代码生成。或者你用整型也是数据库中自增的,反正一般
不会人为定义id这种东西。
时间: 2024-11-08 19:54:38