JSP 视图中的 Spring 3.0.6 MVC @PathVariable 和 @RequestParam 空白/空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8286874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 3.0.6 MVC @PathVariable and @RequestParam blank/empty in JSP view
提问by alexmuller
I have been trying to get an incredibly simple controller/view set up, and just can't make it work. In my web.xml, I've defined a <servlet>called servlet-context.xml, which is running ok. In servlet-context.xml, I've set:
我一直在尝试设置一个非常简单的控制器/视图,但无法使其正常工作。在我的 中web.xml,我定义了一个<servlet>调用的servlet-context.xml,它运行正常。在servlet-context.xml,我已经设置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
<...other stuff in here... />
<mvc:annotation-driven />
among other things. My understanding is this is all that's needed to use @annotations.
除其他事项外。我的理解是这就是使用@注释所需的全部内容。
In my controller, I have:
在我的控制器中,我有:
@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)
public String adminStudent(@PathVariable String username, @RequestParam String studentid) {
return "student";
}
And in my student.jspview, I have:
在我student.jsp看来,我有:
<p>This is the page where you would edit the stuff for ${username}.</p>
<p>The URL parameter <code>studentid</code> is set to ${studentid}.</p>
When I make a request to http://localhost:8080/application/student/xyz123/?studentid=456, I get the view I expect, but all the variables are blank or null:
当我向 发出请求时http://localhost:8080/application/student/xyz123/?studentid=456,我得到了我期望的视图,但所有变量都为空或空:
<p>This is the page where you would edit the stuff for .</p>
<p>The URL parameter <code>studentid</code> is set to .</p>
I suspect it's a problem with the way my web.xmlor servlet-context.xmlare set up, but I can't find the culprit anywhere. There's nothing showing up in any logs as far as I can see.
我怀疑这是我的web.xml或servlet-context.xml设置方式的问题,但我在任何地方都找不到罪魁祸首。据我所知,任何日志中都没有显示任何内容。
Update: I was basing my code off this part of the spring-mvc-showcase:
更新:我的代码基于spring-mvc-showcase 的这一部分:
@RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)
public String pathVars(@PathVariable String foo, @PathVariable String fruit) {
// No need to add @PathVariables "foo" and "fruit" to the model
// They will be merged in the model before rendering
return "views/html";
}
...which works fine for me. I can't understand why this example works but mine doesn't. Is it because they're doing something differentwith servlet-context.xml?
...这对我来说很好用。我不明白为什么这个例子有效,但我的不行。是不是因为他们正在做一些不同的用servlet-context.xml?
<annotation-driven conversion-service="conversionService">
<argument-resolvers>
<beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
</argument-resolvers>
</annotation-driven>
采纳答案by alexmuller
Aha! Figured it out, finally.
啊哈!想通了,终于。
The spring-mvc-showcaseis using Spring 3.1, which will automatically expose @PathVariables to the model, according to SPR-7543.
的spring-mvc-showcase是使用Spring 3.1,这将自动地暴露@PathVariables到模型中,根据SPR-7543。
As pointed out by @duffymo and @JB Nizet, adding to the Model with model.put()is the thing to do for versions of Spring earlier than 3.1.
正如@duffymo 和@JB Nizet 所指出的,model.put()对于 3.1 之前的 Spring 版本,添加到 Model with是要做的事情。
Ted Young pointed me in the right direction with Spring: Expose @PathVariables To The Model.
Ted Young 用Spring 为我指明了正确的方向:将 @PathVariables 暴露给模型。
回答by duffymo
Create a Model map and add those parameter name/value pairs to it:
创建模型映射并将这些参数名称/值对添加到其中:
@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)
public String adminStudent(@PathVariable String username, @RequestParam String studentid, Model model) {
model.put("username", username);
model.put("studentid", studentid);
return "student";
}
回答by JB Nizet
@PathVariablemeans that the annotated method argument should be extracted from the path of the invoked URL. @RequestParammeans that the annotated method argument must be extracted from the request parameters. None of these annotations cause the annotated arguments to be put in the request, session or application scope.
@PathVariable意味着应从调用的 URL 的路径中提取带注释的方法参数。@RequestParam意味着必须从请求参数中提取带注释的方法参数。这些注释都不会导致将带注释的参数放入请求、会话或应用程序范围。
${username}means "write the value of the username attribute (found in page, or request, or session, or application scope) in the response". Since you didn't include any username attribute in any of those scopes, it doesn't write anything.
${username}表示“在响应中写入用户名属性的值(在页面、请求、会话或应用程序范围中找到)”。由于您没有在任何这些范围内包含任何用户名属性,因此它不会写入任何内容。
The code would work if the method returned a ModelAndView object, and the model contained a usernameattribute and a studentidattribute.
如果该方法返回一个 ModelAndView 对象,并且该模型包含一个username属性和一个studentid属性,则该代码将起作用。
回答by Sudhakar
@PathVariableis to obtain some placeholder from the uri (Spring call it an URI Template) — see Spring Reference Chapter 16.3.2.2 URI Template Patterns@RequestParamis to obtain an parameter — see Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with @RequestParam
@PathVariable是从 uri 中获取一些占位符(Spring 称其为 URI 模板)——参见Spring 参考章节 16.3.2.2 URI 模板模式@RequestParam是获取一个参数——参见Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with @RequestParam
Assume this Url http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013(to get the invoices for user 1234 for today)
假设这个 URL http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013(获取今天用户 1234 的发票)
@RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
public List<Invoice> listUsersInvoices(
@PathVariable("userId") int user,
@RequestParam(value = "date", required = false) Date dateOrNull) {
model.put("userId", user);
model.put("date", dateOrNull);
}

