如何在Spring MVC中的相同@RequestMapping中映射参数的不同值? [英] How do I map different values for a parameter in the same @RequestMapping in Spring MVC?

查看:1032
本文介绍了如何在Spring MVC中的相同@RequestMapping中映射参数的不同值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:

  @RequestMapping(params =action = nuovoprodotto)
public ModelAndView nuovoProdotto(
@RequestParam(value =page,required = false,defaultValue = - 1)int page,
@RequestParam(value =action)字符串操作,
@ModelAttribute Prodotto prod,HttpSession session)
抛出异常{

是否可以将此请求映射到两个或三个动作参数值?



我尝试了很多方法,比如

  @RequestMapping(params =action = nuovoprodotto,action = salvaprodotto)

  @RequestMapping(params =action = nuovoprodotto | salvaprodotto)

但它们不起作用......如果我不能解决什么问题,那么除了为每个参数值组合编写处理程序外?

尝试这个:

  @RequestMapping(params = {action = nuovoprodotto,action = salvaprodotto})

params 属性实际上是 String [] 类型,但是注释允许你写一个 String 来代替单个元素 String [] ,所以这两个是等价的:

  @RequestMapping(params = {action = nuovoprodotto})

  @RequestMapping(params =action = nuovoprodotto)

参考:








更新:我的不好,就像你一样可以阅读高级@RequestMapping选项,使用和组合多个参数not or,所以它不能像上面指定的那样工作。



所以我要说你要做的是创建一个具有几乎相同签名的别名方法:

  @RequestMapping(params =action = nuovoprodotto)
public ModelAndView nuovoProdotto(
@RequestParam(value) =page,required = false,defaultValue = - 1)int page,
@RequestParam(value =action)字符串操作,
@ModelAttribute Prodotto prod,HttpSession session)
抛出异常{
//这里的一些东西
}

@RequestMapping(params =action = salvaprodotto)
public ModelAndView salvaProdotto(
@ RequestParam(value =page,required = false,defaultValue = - 1)int page,
@RequestParam(value =action)String actio n,
@ModelAttribute Prodotto prod,HttpSession session)
throws Exception {

return nuovoProdotto(page,action,prod,session);
}


Suppose I have:

@RequestMapping(params = "action=nuovoprodotto")    
    public ModelAndView nuovoProdotto(
            @RequestParam(value = "page", required = false, defaultValue = "-1") int page,
            @RequestParam(value = "action") String action,
            @ModelAttribute Prodotto prod, HttpSession session)
            throws Exception {

is it possible to map this request to like two or three values of "action" parameter?

I tried many ways like

@RequestMapping(params = "action=nuovoprodotto, action=salvaprodotto")  

or

@RequestMapping(params = "action=nuovoprodotto|salvaprodotto")  

but they don't work... If I can't what are the solutions, besided writing an handler for every single parameter value combination?

解决方案

Try this:

@RequestMapping(params = {"action=nuovoprodotto","action=salvaprodotto"})

The params attribute is actually of type String[], but annotations let you write a String in place of a single-element String[], so these two are equivalent:

@RequestMapping(params = {"action=nuovoprodotto"})

and

@RequestMapping(params = "action=nuovoprodotto")

Reference:


Update: my bad, as you can read in the section Advanced @RequestMapping options, multiple params are combined using and, not or, so it can't work as specified above.

So I'd say what you have to do is create an alias method with almost the same signature:

@RequestMapping(params = "action=nuovoprodotto")    
public ModelAndView nuovoProdotto(
        @RequestParam(value = "page", required = false, defaultValue = "-1") int page,
        @RequestParam(value = "action") String action,
        @ModelAttribute Prodotto prod, HttpSession session)
        throws Exception {
        // some stuff here
}

@RequestMapping(params = "action=salvaprodotto")    
public ModelAndView salvaProdotto(
        @RequestParam(value = "page", required = false, defaultValue = "-1") int page,
        @RequestParam(value = "action") String action,
        @ModelAttribute Prodotto prod, HttpSession session)
        throws Exception {

        return nuovoProdotto(page, action, prod, session);
}

这篇关于如何在Spring MVC中的相同@RequestMapping中映射参数的不同值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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