首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

ActionForm乱码的有关问题

2012-06-26 
ActionForm乱码的问题struts1框架做的项目,在后台利用ActionForm来获取form表单的值的时候,显示的是乱码。

ActionForm乱码的问题
struts1框架做的项目,在后台利用ActionForm来获取form表单的值的时候,显示的是乱码。就是把Form表单的属性都封装在ActionForm中,比如:
public class UserForm extends ActionForm{
  private String name;
  private int id;
  ...//相应的get和set方法
}
public ActionForward selectUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
          //第一种获取表单值的方法:
          UserForm  user=(UserForm)form;
          String name= user.getName(); //用这种方法获取的值就是乱码
         
          //第二种获取表单值的方法:
          String name=request.getParameter("name");//这一步获的的是乱码,接下来转码,name 是和表单text文本框中的name属性是一致的。
          if (name!= null && !name.isEmpty()) {
try {
name= new String(name.getBytes("ISO-8859-1"), "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}//这个时候就可以了。
           //第三种方法利用js脚本传递参数到后台
           String name=request.getParameter("name");//是否是乱码有待验证
          

热点排行