java 在struts的action方法中设置url参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3188789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:44:39  来源:igfitidea点击:

setting url parameter in an action method in struts

javaurl-routingstruts-1

提问by CoolBeans

I want to add a URL parameter before forwarding to another action from an action method. Although HttpServletRequest has a getParameter() method, it has no setParameter() method. I know setAttribute() is there but I need the it to be part of the URL (like ?something=something&some2=some2). I know I can do it using filters but that's an overkill for this task.

我想在从操作方法转发到另一个操作之前添加一个 URL 参数。虽然 HttpServletRequest 有一个 getParameter() 方法,但它没有 setParameter() 方法。我知道 setAttribute() 在那里,但我需要它成为 URL 的一部分(比如?something=something&some2=some2)。我知道我可以使用过滤器来做到这一点,但这对这项任务来说太过分了。

Basically we have an externally managed filter which will change something on a page when that parameter is set. Let's say the color of the page will be passed as part of the URL parameter. When that parameter is present a servlet filter picks it up and changes the color of that page. I know it's a very odd way of doing but that's how they have it set up here.

基本上我们有一个外部管理的过滤器,它会在设置该参数时更改页面上的某些内容。假设页面的颜色将作为 URL 参数的一部分传递。当该参数出现时,servlet 过滤器会选取它并更改该页面的颜色。我知道这是一种非常奇怪的做法,但这就是他们在这里设置的方式。

I know how to make it work using java script based form submit by adding the URL parameter to the action url (ie. "/someAction.do?color=red"). But for some of the actions it actually does a action.forward("action_name") in the code. For those I was wondering what to do.

我知道如何通过将 URL 参数添加到操作 url(即“/someAction.do?color=red”)来使用基于 java 脚本的表单提交使其工作。但是对于某些操作,它实际上在代码中执行了 action.forward("action_name")。对于那些我想知道该怎么做的人。

Does anyone know how to do that in struts 1.2?

有谁知道如何在 struts 1.2 中做到这一点?

回答by laz

The short answer is that it isn't possible. Request parameters are supposed to be from the HTTP request. You can fake adding them using a combination of a ServletFilterand an HttpServletRequestWrapperbut that is outside of Struts. Depending upon what you are trying to accomplish there may be a better solution. Want to describe that a bit more?

简短的回答是,这是不可能的。请求参数应该来自 HTTP 请求。您可以使用 aServletFilter和 an的组合来伪造添加它们,HttpServletRequestWrapper但这是在 Struts 之外。根据您要完成的任务,可能有更好的解决方案。想要多描述一点吗?

Update

更新

With the additional detail you've added, I think you can try this to see if it meets your needs:

有了你添加的额外细节,我想你可以试试这个,看看它是否满足你的需求:

import org.apache.struts.action.ActionRedirect;
...
ActionForward forward = action.forward("action_name");
ActionRedirect redirect = new ActionRedirect(forward);
redirect.addParameter("color", "red");
return redirect;

回答by davibq

I know this is an old and accepted post, but If you can't upgrade your struts (like me) then this might be useful http://www.coderanch.com/t/45890/Struts/Adding-parameters-struts-action

我知道这是一个旧的和被接受的帖子,但是如果你不能升级你的 struts(像我一样)那么这可能会有用http://www.coderanch.com/t/45890/Struts/Adding-parameters-struts-行动

import org.apache.struts.action.ActionForward;
public class ParameterizedForward extends ActionForward
{
    public ParameterizedForward(ActionForward forward)
    {
        super(forward.getPath(), forward.getRedirect());
    }
    public void addParameter(String key, String value)
    {
        StringBuffer sb = new StringBuffer(getPath());
        if (key == null || key.length() < 1)
            return;
        if (getPath().indexOf('?') == -1)
            sb.append('?');
        else
            sb.append('&');
        sb.append(key + "=" + value);
        setPath(sb.toString());
    }
}

ParameterizedForward fwd = new ParameterizedForward(mapping.findForward("success"));
fwd.addParameter("name","jason");
fwd.addParameter("userLevel", "god");
return fwd;