概述
好久没有写ssh框架了,一上来就遇到乱码,废话不多说,直接进入主题。
http://localhost:8880/shopssh/user_registPage.action这条链接传递username的参数
后台接收的是乱码,原因就是就是在Action中得到传递过来的参数后再将字符编码修改为你设置的字符编码,如“UTF-8”。
因为Servlet默认的字符编码为"ISO-8859-1",前台传递过来的请求如果没有提交而是直接传递给Action
Servlet 不会将默认的字符编码转化为你自己设置的字符编码。所以如下
String username=request.getParameter("username");
String usernames= new String(username.getBytes("ISO-8859-1"), "UTF-8");
就解决问题
顺便说一下接收get请求的两种方式
1.System.out.println(new String(user.getUsername().getBytes("ISO-8859-1"), "UTF-8"));
User user=new User();
public User getModel() {
return user;
}
使用驱动模型
2. HttpServletRequest request=ServletActionContext.getRequest();
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String username=request.getParameter("username");
String usernames= new String(username.getBytes("ISO-8859-1"), "UTF-8");
System.out.println(usernames);
最后
以上就是眼睛大人生为你收集整理的Struts2接受的参数乱码的全部内容,希望文章能够帮你解决Struts2接受的参数乱码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复