添加参数后向前推进?

发布于 2024-12-22 06:03:53 字数 183 浏览 2 评论 0原文

有没有办法将请求转发到另一个控制器,同时向其添加一些参数数据?我尝试添加到 ModelMap,但它似乎没有出现。我正在做类似的事情:

return "forward:/my-other-controller";

我能想到的唯一其他方法是将参数放在会话上,然后将它们弹出到目标控制器中。

Is there a way to forward a request to another Controller while adding some parameter data to it? I tried adding to the ModelMap, but it doesn't seem to hang around. I am doing something like:

return "forward:/my-other-controller";

Only other way I can think of is to put the parameters on the session and then pop them off in the target controller.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

罪#恶を代价 2024-12-29 06:03:53

最简单的方法是将数据添加到请求中。由于这是转发,因此相同的请求会传递到服务器内的不同处理程序。

例如,让我们从两个控制器的简单设置开始,一个转发到另一个:

@Controller
public class TestController {
    @RequestMapping(value="/test")
    public String showTestPage() {
        return "forward:/test2";
    }
}

@Controller
public class TestController2 {
    @RequestMapping(value="/test2")
    public String showTestPage() {
        return "testPageView";
    }
}

添加数据的第一种方法是将其设置为请求的属性。新的控制器将如下所示(A):

@Controller
public class TestController {
    @RequestMapping(value="/test")
    public String showTestPage(HttpServletRequest request) {
        request.setAttribute("param1", "foo");
        request.setAttribute("param2", "bar");
        return "forward:/test2";
    }
}

@Controller
public class TestController2 {
    @RequestMapping(value="/test2")
    public String showTestPage(HttpServletRequest request) {
        String param1 = (String) request.getAttribute("param1");
        String param2 = (String) request.getAttribute("param2");
        return "testPageView";
    }
}

由于 转发前缀基本上是一个URL,你还可以有以下版本(属性更改为参数)(B ):

@Controller
public class TestController {
    @RequestMapping(value="/test")
    public String showTestPage() {
        return "forward:/test2?param1=foo¶m2=bar";
    }
}

@Controller
public class TestController2 {
    @RequestMapping(value="/test2")
    public String showTestPage(HttpServletRequest request) {
        String param1 = request.getParameter("param1");
        String param2 = request.getParameter("param2");
        return "testPageView";
    }
}

还可以进一步简化第二个控制器通过使用注释来代替:

@Controller
public class TestController2 {
    @RequestMapping(value="/test2")
    public String showTestPage(@RequestParam String param1, @RequestParam String param2) {
        return "testPageView";
    }
}

只是为了它的乐趣,并展示Spring的实际绑定行为,你甚至可以这样做(C):

@Controller
public class TestController {
    @RequestMapping(value="/test")
    public String showTestPage() {
        return "forward:/test2?param1=foo¶m2=bar";
    }
}

@Controller
public class TestController2 {
    @RequestMapping(value="/test2")
    public String showTestPage(@ModelAttribute DummyBinder params) {
        String param1 = params.getParam1();
        String param2 = params.getParam2();
        return "testPageView";
    }
}

class DummyBinder {
    private String param1;
    private String param2;

    public String getParam1() {
        return param1;
    }

    public void setParam1(String param1) {
        this.param1 = param1;
    }

    public String getParam2() {
        return param2;
    }

    public void setParam2(String param2) {
        this.param2 = param2;
    }
}

我个人会选择解决方案A对于许多参数,解决方案 B 对于一些参数。 解决方案 C 有一种“嗯...?!”的感觉。效果,所以我会避免它(它也适用于添加到 URL 的参数,所以其中一些参数或者你会得到一个混乱的 URL)。

在会话中添加数据当然也会起作用,但会不必要地延长数据的生命周期,因此最好的位置是在转换到第二个控制器期间将其添加到请求中。

The simplest way is to add the data to the request. Since this is a forward, the same request is passed around to different handlers within the server.

As example, let's start with a simple setup of two controllers, one forwarding to the other:

@Controller
public class TestController {
    @RequestMapping(value="/test")
    public String showTestPage() {
        return "forward:/test2";
    }
}

@Controller
public class TestController2 {
    @RequestMapping(value="/test2")
    public String showTestPage() {
        return "testPageView";
    }
}

First way to add the data is to set it as attributes on the request. The new controllers will look like this (A):

@Controller
public class TestController {
    @RequestMapping(value="/test")
    public String showTestPage(HttpServletRequest request) {
        request.setAttribute("param1", "foo");
        request.setAttribute("param2", "bar");
        return "forward:/test2";
    }
}

@Controller
public class TestController2 {
    @RequestMapping(value="/test2")
    public String showTestPage(HttpServletRequest request) {
        String param1 = (String) request.getAttribute("param1");
        String param2 = (String) request.getAttribute("param2");
        return "testPageView";
    }
}

Since the view name in the forward prefix is basically an URL, you can also have the following versions (attribute changed to parameter) (B):

@Controller
public class TestController {
    @RequestMapping(value="/test")
    public String showTestPage() {
        return "forward:/test2?param1=foo¶m2=bar";
    }
}

@Controller
public class TestController2 {
    @RequestMapping(value="/test2")
    public String showTestPage(HttpServletRequest request) {
        String param1 = request.getParameter("param1");
        String param2 = request.getParameter("param2");
        return "testPageView";
    }
}

You can also further simplify the second controller by using annotations instead:

@Controller
public class TestController2 {
    @RequestMapping(value="/test2")
    public String showTestPage(@RequestParam String param1, @RequestParam String param2) {
        return "testPageView";
    }
}

And just for the fun of it, and to show Spring's binding behavior in action, you could do it even like this (C):

@Controller
public class TestController {
    @RequestMapping(value="/test")
    public String showTestPage() {
        return "forward:/test2?param1=foo¶m2=bar";
    }
}

@Controller
public class TestController2 {
    @RequestMapping(value="/test2")
    public String showTestPage(@ModelAttribute DummyBinder params) {
        String param1 = params.getParam1();
        String param2 = params.getParam2();
        return "testPageView";
    }
}

class DummyBinder {
    private String param1;
    private String param2;

    public String getParam1() {
        return param1;
    }

    public void setParam1(String param1) {
        this.param1 = param1;
    }

    public String getParam2() {
        return param2;
    }

    public void setParam2(String param2) {
        this.param2 = param2;
    }
}

I would personally go with solution A for many parameters, and solution B for a few. Solution C has a sort of "huh...?!" effect so I would avoid it (also it works with parameters added to the URL so a few of those or you get a messy URL).

Adding the data in the session would also work off course, but would extend the data's life time unnecessarily, so the best place is to add it on the request during the transition to the second controller.

苏璃陌 2024-12-29 06:03:53

考虑转发就像重定向一样,但在这个意义上不需要上下文和主机(转发由服务器内部处理,而重定向指示用户的浏览器请求不同的 url)。

所以,是的,使用会话、cookie 或发布或获取参数。

Consider forward just like redirect but without the need for a context and host in this sense (forward is handled internally by the server, whereas redirect instructs the user's browser to request a different url).

So, yes, use the session, cookies or post or get parameters.

财迷小姐 2024-12-29 06:03:53

你考虑过过滤器吗?您可以使用委托过滤器代理创建一个过滤器,这将使您能够访问请求参数以及 spring beans。

Have you considered filters? You could create a filter using the delegating filter proxy which will gives you access to the request parameters as well as the spring beans .

独木成林 2024-12-29 06:03:53

我发现 这篇文章,使用 HttpRequestWrapper

第一:
定义 HttpRequestWrapper 并重写方法 getParametergetParameterMapgetParameterNamesgetParameterValues,如下所示在帖子中。

第二:
通过 RequestDispatcher 转发到您的网址,使用方法参数中的包装请求:

request.getRequestDispatcher("myForwardUrl").forward(new MyRequestWrapper(request), response);

我已经尝试过删除参数(这甚至更困难!)并且它有效!

I've found this post, that uses an HttpRequestWrapper:

First:
Define the HttpRequestWrapper and override the methods getParameter, getParameterMap, getParameterNames and getParameterValues like in the post.

Second:
Forward via RequestDispatcher to your url, using the wrapped request in the method parameters:

request.getRequestDispatcher("myForwardUrl").forward(new MyRequestWrapper(request), response);

I've tried this out for deleting of parameters (which is even more difficult!) and it works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文