【问题标题】:Clean up querystring on request in Struts在 Struts 中根据请求清理查询字符串
【发布时间】:2014-10-21 22:43:14
【问题描述】:

我使用的是 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?

【问题讨论】:

  • 查看 url 重写,例如使用 Tuckey Rewrite 过滤器。我认为基本上你是在向后看问题。
  • 我不想实现 Tuckey,只想在 ActionForward 中进行更改。
  • 你正在重定向,它告诉浏览器发出一个新请求,所以请求中的任何内容都会丢失。
  • @YanOner,就像我说的,你是在向后看。您应该做的是:不要使用 oldschool 查询字符串并尝试将其“清理”成漂亮的 url,而是使用漂亮的 url 并重写以转发到后端的 oldschool 查询字符串。

标签: javascript java jsp struts struts-1


【解决方案1】:

如果您想将页面重定向到主页,您只需返回 mapping.findForward("home"),如下所示

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


    return mapping.findForward("home");
}

【讨论】:

  • 但是,这并没有清理 URL 的 QueryString。
【解决方案2】:

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

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;
}

【讨论】:

  • 这个的问题,其他的方法都要清理session中的属性,很麻烦。我想,我会在加载 jsp 时修改 URL。
  • 其他方法应该使用自己的请求属性,直到他们想要重定向请求。
猜你喜欢
  • 2010-11-17
  • 2020-01-29
  • 2013-04-06
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
相关资源
最近更新 更多