在 Spring 控制器中,我可以根据请求参数的数量调用一个方法吗?
我一直在用 Spring 改造现有的网络应用程序。显然,从 Spring 开始比稍后添加要容易得多。
我们有可以接受多个请求参数的 servlet。根据参数的数量,将采取不同的操作。例如,
/doSomething?prod=15
显示产品 15 的信息,并将
/doSomething?prod=15&owner=99
产品 15 的所有者设置为 99 并
/doSomething?prod=15&delete=y
删除产品 15。
我有一个控制器正在工作,但我不知道如何根据参数的数量调用不同的方法。例如,这是可行的(简单的方法只是为了确保我有基本的工作):
@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'映射的不明确的处理程序方法”并继续声明:
如果您打算处理相同的路径 用多种方法,然后将它们因式分解 进入专用处理程序类 该路径映射到类型 级别!
我不明白该消息告诉我要做什么。
如果我将方法设置为接受比传入的参数更多或不同的参数,我会收到“客户端发送的请求在语法上不正确()”。
保罗
I've been retrofitting an existing webapp with Spring. Clearly it's easier to start with Spring than to add it on later.
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
displays the information for product 15 and
/doSomething?prod=15&owner=99
sets the owner of product 15 to 99 and
/doSomething?prod=15&delete=y
deletes product 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;
}
but not this:
@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;
}
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()".
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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:
You can even share a model between them if you want:
您的请求映射需要映射实际的 URL 而不仅仅是 HTTP 方法。您还可以在每个方法的基础上放置所需的参数,如下所示......
Your request mapping needs to map an actual URL rather then just the HTTP method. You can also put required params on a per method basis like so...