根据 Struts 1 中的请求清理查询字符串

问题描述 投票:0回答:2

我正在使用 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 吗?

javascript java jsp struts struts-1
2个回答
0
投票

如果您想将页面重定向到主页,您只需返回

mapping.findForward("home")
,如下所示

public ActionForward search(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response)throws Exception{      
    request.setAttribute("RESULT","PERU");


    return mapping.findForward("home");
}

0
投票

在进行重定向之前将属性保存到会话

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.