我正在使用 Struts 1,只想清理每个请求上参数的 URL。
在请求中,例如:
myapp.com/view.do?method=search
行动:
public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception{
request.setAttribute("RESULT","PERU");
return mapping.findForward("home");
}
转发中,URL是一样的
myapp.com/view.do?method=search
后来,我这样做:
public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception{
request.setAttribute("RESULT","PERU");
ActionForward af = new ActionForward(mapping.findForward("home"));
af.setRedirect(true);
return af;
}
我得到了网址:
myapp.com/home.jsp
,很好!但是,我失去了属性"RESULT"
。
当我使用
setRedirect()
时,Struts 1 发出新请求,我会丢失所有属性。
还有其他形式的清理
ActionForward
中的 URL 吗?
如果您想将页面重定向到主页,您只需返回
mapping.findForward("home")
,如下所示
public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception{
request.setAttribute("RESULT","PERU");
return mapping.findForward("home");
}
在进行重定向之前将属性保存到会话
public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
request.getSession().setAttribute("RESULT","PERU");
ActionForward af = new ActionForward(mapping.findForward("home"));
af.setRedirect(true);
return af;
}