在Spring控制器中,能否有一个基于请求参数数量的方法? [英] In a Spring controller, can I have a method called based on the number of request parameters?

查看:118
本文介绍了在Spring控制器中,能否有一个基于请求参数数量的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用Spring改造现有的Web应用程序.显然,从Spring开始比在以后添加它更容易.

I've been retrofitting an existing webapp with Spring. Clearly it's easier to start with Spring than to add it on later.

我们有可以接受多个请求参数的servlet.根据参数的数量,将采取不同的措施.例如,

We have servlets that can take multiple request parameters. Based on the number of parameters different actions will be taken. For example,

/doSomething?prod=15

显示产品15和

/doSomething?prod=15&owner=99

将产品15的所有者设置为99,并且

sets the owner of product 15 to 99 and

/doSomething?prod=15&delete=y

删除产品15.

我有一个正在工作的控制器,但是我不知道如何根据参数的数量来调用不同的方法.例如,这有效(一种简单的方法只是为了确保我掌握了基础知识):

I have a controller working but I don't know how to call different methods based on the number of parameters. For example, this works (trivial method just to ensure I have the basics working):

@RequestMapping(method=RequestMethod.GET)
public ModelAndView doIt(@RequestParam("prod") int prod, Model model)
{
  ModelAndView mav = new ModelAndView();
  mav.setViewName("jsonView");
  return mav;
}

但不是这样:

@RequestMapping(method=RequestMethod.GET)
public ModelAndView doIt(@RequestParam("prod") int prod, Model model)
{
  ModelAndView mav = new ModelAndView();
  mav.setViewName("jsonView");
  return mav;
}

@RequestMapping(method=RequestMethod.GET)
public ModelAndView doIt(@RequestParam("prod") int prod,
                         @RequestParam("owner") int owner,
                         Model model)
{
  ModelAndView mav = new ModelAndView();
  mav.setViewName("jsonView");
  return mav;
}

这将引发IllegalStateException,为HTTP路径'/mytest'映射的歧义处理程序方法",然后继续显示状态:

That throws an IllegalStateException, "Ambiguous handler methods mapped for HTTP path '/mytest'" and goes on to state:

如果您打算使用相同的路径 用多种方法,然后将它们分解 进入专门的处理程序类 该路径映射到类型 水平!

If you intend to handle the same path in multiple methods, then factor them out into a dedicated handler class with that path mapped at the type level!

我不明白那条消息告诉我要做的事情.

I don't understand what that message is telling me to do.

如果我将方法设置为接受比传入的参数更多或不同的参数,则会收到客户端发送的请求在语法上不正确()".

If I set up the method to accept more or different parameters than are passed in I get "The request sent by the client was syntactically incorrect()".

保罗

推荐答案

RequestMapping注释告诉Spring哪些URL请求映射到您的控制器.您可以将值放在方法级别或类级别.

The RequestMapping annotation tells Spring which URL requests to map to your controller. You can put the value either at the method level or at the class level.

在您的示例中,两个请求映射之间没有什么不同.不过,您可以执行以下操作:

In your example, nothing differs between the two request mappings. You can do this, though:

@RequestMapping(value="/bar/example.htm", method={RequestMethod.GET}, params={"prod", "owner"})
public String doIt( @RequestParam("prod") int prod,
                    @RequestParam("owner") int owner) {
    LOGGER.trace("in doIt(int,int)");
    return "foo/bar";
}
@RequestMapping(value="/bar/example.htm", method={RequestMethod.GET}, params={"prod"})
public String doIt( @RequestParam("prod") int prod) {
    LOGGER.trace("in doIt(int)");
    return "foo/bar";
}

如果需要,您甚至可以在他们之间共享模型:

You can even share a model between them if you want:

@Model
public static Map<String,Object> model() {
  LOGGER.trace("in model()");
  Map<String,Object> model = new HashMap<>();
  model.put("hello", "hello world");
  return model;
}

这篇关于在Spring控制器中,能否有一个基于请求参数数量的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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