Spring 3.0.6 MVC @PathVariable和@RequestParam在JSP视图中为空/空 [英] Spring 3.0.6 MVC @PathVariable and @RequestParam blank/empty in JSP view
问题描述
我一直在尝试建立一个非常简单的控制器/视图,但无法使其正常工作.在我的web.xml
中,我定义了一个名为servlet-context.xml
的<servlet>
,它可以正常运行.在servlet-context.xml
中,我设置了:
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:
<?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";
}
在我的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>
当我向http://localhost:8080/application/student/xyz123/?studentid=456
发出请求时,得到了我期望的视图,但是所有变量均为空白或空:
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:
<p>This is the page where you would edit the stuff for .</p>
<p>The URL parameter <code>studentid</code> is set to .</p>
我怀疑web.xml
或servlet-context.xml
的设置方式存在问题,但我在任何地方都找不到罪魁祸首.据我所知,任何日志中都没有显示任何内容.
I suspect it's a problem with the way my web.xml
or servlet-context.xml
are set up, but I can't find the culprit anywhere. There's nothing showing up in any logs as far as I can see.
更新:我基于
...对我来说很好.我不明白为什么这个例子行得通,而我的行不通.是因为它们 ...which works fine for me. I can't understand why this example works but mine doesn't. Is it because they're doing something different with
啊!终于找到答案了. The 如@duffymo和@JB Nizet所指出的那样,对于早于3.1的Spring版本,要使用 As pointed out by @duffymo and @JB Nizet, adding to the Model with Ted Young pointed me in the right direction with Spring: Expose @PathVariables To The Model. 这篇关于Spring 3.0.6 MVC @PathVariable和@RequestParam在JSP视图中为空/空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!servlet-context.xml
?<annotation-driven conversion-service="conversionService">
<argument-resolvers>
<beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
</argument-resolvers>
</annotation-driven>
推荐答案
spring-mvc-showcase
使用的是Spring 3.1,根据spring-mvc-showcase
is using Spring 3.1, which will automatically expose @PathVariable
s to the model, according to SPR-7543.model.put()
添加到模型中.model.put()
is the thing to do for versions of Spring earlier than 3.1.