2011-11-27 75 views
9

我一直在試圖獲得令人難以置信的簡單控制器/視圖設置,並且無法使其工作。在我的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.xmlservlet-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 differentservlet-context.xml

<annotation-driven conversion-service="conversionService"> 
    <argument-resolvers> 
     <beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/> 
    </argument-resolvers> 
</annotation-driven> 

回答

7

啊哈!最後想出來。

spring-mvc-showcase正在使用Spring 3.1,根據SPR-7543,它會自動將@PathVariables曝光到模型中。

正如@duffymo和@JB Nizet所指出的那樣,使用model.put()添加到模型中對於早於3.1的Spring版本來說是一件好事。

泰德揚指出我正確的方向與Spring: Expose @PathVariables To The Model

+1

你應該接受你自己的答案,因爲它是正確的。 –

15

創建模型地圖和這些參數的名稱/值對添加到它:

@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"; 
} 
+0

非常感謝f或答案!我已經添加了一個來自Spring開發人員的例子 - 如果你能解釋他們爲什麼工作,我會很感激。 – alexmuller

4

@PathVariable意味着該註解的方法參數應該被調用的URL的路徑中提取出來。 @RequestParam表示必須從請求參數中提取註釋的方法參數。這些註釋都不會導致將註釋參數放入請求,會話或應用程序範圍中。

${username}表示「在響應中寫入用戶名屬性的值(在頁面,請求,會話或應用程序範圍中找到)」。由於您沒有在任何這些範圍中包含任何用戶名屬性,因此它不會寫任何內容。

如果該方法返回一個ModelAndView對象,並且該模型包含一個username屬性和一個studentid屬性,則該代碼將起作用。

+0

非常感謝您的回答!我已經添加了一個來自Spring開發人員的例子 - 如果你能解釋他們爲什麼工作,我會很感激。 – alexmuller

+0

也許我錯了,然後,Spring會自動將路徑變量添加到模型中。參考文檔說,只有在編譯過程中啓用了cebugging的情況下,這些路徑變量才起作用。請參閱http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-ann-requestmapping-uri-templates。試試@PathVariable(「username」)和@PathVariable(「studentid」) –

1

假設以下鏈接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); 

}