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.
只在第一個輸入框裡輸入的只是數字的話就不報錯
其他情況都不報錯
求原因
你第一個輸入框是編號,你輸入中文最後肯定會報錯,因為中文屬於字符串類型,而你的編號是整型的。