我一直在試圖獲得令人難以置信的簡單控制器/視圖設置,並且無法使其工作。在我的web.xml
中,我定義了一個名爲servlet-context.xml
的<servlet>
,它運行正常。在servlet-context.xml
,我已經設置:Spring 3.0.6 MVC @PathVariable和@RequestParam在JSP視圖中爲空/空
<?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 />
等等。我的理解是這是使用@
註釋所需的全部內容。
在我的控制,我有:
@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
的請求,我得到我預期的看法,但所有的變量是空白或空值:
<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
設置,但我無法找到任何地方的罪魁禍首。據我所知,沒有任何日誌顯示在任何日誌中。
更新:我是立足我的代碼關閉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";
}
...這對我來說工作正常。我不明白爲什麼這個例子有效,但我的不是。是因爲他們的doing something different和servlet-context.xml
?
<annotation-driven conversion-service="conversionService">
<argument-resolvers>
<beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
</argument-resolvers>
</annotation-driven>
你應該接受你自己的答案,因爲它是正確的。 –