春天向前添加参数? [英] Spring forward with added parameters?

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

问题描述

有一种方法可以向另一个控制器转发请求,同时向其添加一些参数数据?我试图添加到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天全站免登陆