使用添加的参数向前推进? [英] Spring forward with added parameters?

查看:21
本文介绍了使用添加的参数向前推进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

最简单的方法是将数据添加到请求中. 由于这是一个转发,相同的请求被传递到不同的服务器中的处理程序.

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

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

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

由于视图名称在 转发前缀基本上是一个网址,你也可以有以下版本(属性改为参数)(B):

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&param2=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";
    }
}

为了好玩,为了展示 Spring 的绑定行为,您甚至可以这样做 (C):

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&param2=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).

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.

这篇关于使用添加的参数向前推进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆