2 yue254647202 yue254647202 于 2014.10.26 15:37 提问

请问一下ActionForm的保存转发的原理
public class RegistAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    UserForm userForm = (UserForm) form;
    String  username = userForm.getUser().getUsername();
    //解决用户名乱码
    byte[] b = username.getBytes("iso-8859-1");//先回去使用iso的错误字符串
    username = new String(b,"UTF-8");//(iso)回退,使用utf-8重编
    userForm.getUser().setUsername(username);

    String[] like = userForm.getUser().getLike();
    for(int i=0;i<like.length;i++){
        byte[] like1 = like[i].getBytes("iso-8859-1");//先回去使用iso的错误字符串
        like[i] = new String(like1,"UTF-8");//(iso)回退,使用utf-8重编
    }
    userForm.getUser().setLike(like);
    ActionMessages errors = new ActionMessages();
    if(userForm.getUser().getUsername().trim().equals("aaa")){
        errors.add("errname",new ActionMessage("err.repeated","用户"));
        //this.saveErrors(request, errors);
        //return mapping.getInputForward();
    }

    if(!this.isTokenValid(request)){//加入Token无效,例如Session属性中没有Token
        //添加错误信息
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("表单重复提交",false));

    }
    if(!errors.isEmpty()){
        this.saveErrors(request, errors);
        //跳转到错误页面
        return mapping.getInputForward();
    }
    //如果Token有效即Session域中有Token则移除Session域中的Token,然后转到成功页面
    this.resetToken(request);
    return mapping.findForward("success");
}

}

在Action的execute方法中,对Form进行了转型为UserForm,然后对UserForm进行的编码转换是怎么影响到session域中的Form的呢?

1个回答

edwin110611
edwin110611   2014.10.27 16:07

你把 struts的配置 和 jsp 都贴下吧

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!