简体   繁体   English

如何在Java + Spring中正确使用@PathVariable?

[英]How do I correctly use @PathVariable in Java+Spring?

I have a controller that works fine for other request mappings, but I'm having trouble getting one to work with the @PathVariable annotation. 我有一个控制器可以很好地处理其他请求映射,但是我很难@PathVariable@PathVariable注释一起使用。

In this example (see "URI Template Patterns") it has this example: 此示例中 (请参见“ URI模板模式”),它具有以下示例:

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
    Owner owner = ownerService.findOwner(ownerId);
    model.addAttribute("owner", owner);
    return "displayOwner";
}

My code looks like this: 我的代码如下所示:

@RequestMapping( value="/ViewReport/json", method = RequestMethod.GET)
public ModelAndView TestJson(final Model model){
    model.addAttribute("fnord", "{\"hello\":\"awesome\"}"); 
    return new ModelAndView("reportViewJson");  
}    

@RequestMapping( value="/ViewReport/jayson/{foobar}", method = RequestMethod.GET)
public ModelAndView TestJayz(final Model model, @PathVariable String foobar){
    model.addAttribute("fnord", "{\"hello\":\""+foobar+"\"}");  
    return new ModelAndView("reportViewJson");  
} 

Then I have a web.xml file containing this: 然后,我有一个包含以下内容的web.xml文件:

  <servlet-mapping>
    <servlet-name>autoreport</servlet-name>
    <url-pattern>/ViewReport/jayson/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>autoreport</servlet-name>
    <url-pattern>/ViewReport/json</url-pattern>
  </servlet-mapping>

Now, when I navigate to /ViewReport/json , I get the JSON {"hello": "awesome"} as I would expect. 现在,当我导航到/ViewReport/json ,便得到了我所期望的JSON {"hello": "awesome"} However, when I navigate to /ViewReport/jayson/42 I get 但是,当我导航到/ViewReport/jayson/42我得到了

HTTP Status 404 -

type Status report

message

description The requested resource () is not available.

Apache Tomcat/7.0.29

What am I doing wrong with my usage of the PathVariable , and what do I need to do to fix it? 我对PathVariable使用有什么错,我需要怎么做才能解决?

Update 更新

When my server starts up it spits out these lines: 当我的服务器启动时,它弹出以下行:

[ INFO] 14:15(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/json.*] onto handler 'reportViewController'

[DEBUG] 14:15(AbstractBeanFactory.java:doGetBean:246)
Returning cached instance of singleton bean 'reportViewController'

[ INFO] 14:15(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/json/] onto handler 'reportViewController'

[DEBUG] 14:15(AbstractBeanFactory.java:doGetBean:246)
Returning cached instance of singleton bean 'reportViewController'

[ INFO] 14:15(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/jayson/{foobar}] onto handler 'reportViewController'

And when I make the request I get this: 当我发出请求时,我得到了:

[DEBUG] 20:45(DispatcherServlet.java:doService:823)
DispatcherServlet with name 'autoreport' processing GET request for [/ViewReport/jayson/fnord]

[ WARN] 20:45(DispatcherServlet.java:noHandlerFound:1108)
No mapping found for HTTP request with URI [/ViewReport/jayson/fnord] in DispatcherServlet with name 'autoreport'

Spring has magic; 春天有魔力; parameter order matters. 参数顺序很重要。 Try inverting the order of your handler parameters. 尝试反转处理程序参数的顺序。 Instead of this: 代替这个:

public ModelAndView TestJayz(final Model model, @PathVariable String foobar)

Try this: 尝试这个:

public ModelAndView TestJayz(@PathVariable String foobar, final Model model)

Take 2: 采取2:

Try changing the mapping in your web.xml to something more generic. 尝试将web.xml中的映射更改为更通用的映射。 For example: 例如:

<servlet-mapping>
    <servlet-name>autoreport</servlet-name>
    <url-pattern>/ViewReport/*</url-pattern>
</servlet-mapping>

I used /spring/* in my test web.xml, @RequestMapping("/blammy/{value}") in my controller, then sent the request to "{WEBROOT}/spring/blammy/testvalue" where "{WEBROOT}" was the web-root of my test app. 我在测试web.xml中使用了/spring/* ,在控制器中使用了@RequestMapping(“ / blammy / {value}”),然后将请求发送至“ {WEBROOT} / spring / blammy / testvalue”,其中“ {WEBROOT} ”是我的测试应用的网络根目录。

request mappings a relative to the dispatcher servlet mapping (by default, see alwaysUseFullPath property in DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter for more information). 请求映射相对于调度程序Servlet映射的相对关系(默认情况下,请参阅DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter alwaysUseFullPath属性以获取更多信息)。

if you request a resource /ViewReport/jayson/fnord spring mvc is looking for a handler mapped to fnord . 如果您请求资源/ViewReport/jayson/fnord spring mvc正在寻找映射到fnord的处理程序。

you would need to request /ViewReport/jayson/ViewReport/jayson/fnord 您需要请求/ViewReport/jayson/ViewReport/jayson/fnord

Have you tried @PathVariable(value="foobar") String foobar ? 您是否尝试过@PathVariable(value =“ foobar”)字符串foobar

@RequestMapping( value="/ViewReport/jayson/{foobar}", method = RequestMethod.GET)
public ModelAndView TestJayz(final Model model, @PathVariable(value="foobar") String foobar){
    model.addAttribute("fnord", "{\"hello\":\""+foobar+"\"}");  
    return new ModelAndView("reportViewJson");  
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM