2012-07-02 30 views
0

我使用Maven原型Spring MVC的地圖參數.JSP,而不是@RequestMapping

mvn archetype:generate -D groupId=test.tool -D artifactId=test-D version=0.1-SNAPSHOT -D archetypeArtifactId=spring-mvc-jpa-archetype -D archetypeGroupId=org.fluttercode.knappsack 

我一直在試圖讀取從URL的一些參數創建了一個示例彈簧MVC應用程序:

package test.tool.controller; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 


@Controller 
@RequestMapping(value = "/testReadRest", method = RequestMethod.GET) 
public class TestReadRestController { 

private static final Logger logger = LoggerFactory 
     .getLogger(TestReadRestController.class); 


// No Params 
@RequestMapping(method = RequestMethod.GET) 
public void getMainModel(Model model) { 
    model.addAttribute("varFromClient","[NOT SET]"); 
    model.addAttribute("tokenFromClient","[NOT SET]"); 
    return; 
} 

// http://localhost:8080/test/testReadRest/varPost 
// read "varPost" 
@RequestMapping(value = "/{varPost}", method = RequestMethod.GET) 
public void getVarFromURI(@PathVariable("varPost") String theVar, Model model) { 
    model.addAttribute("varFromClient",theVar); 
    model.addAttribute("tokenFromClient","[NOT SET]"); 
    return; 
    } 
} 

而是試圖 http://localhost:8080/test/testReadRest/varPost

,而不是當參數不識字,我得到一個錯誤:

Problem accessing /soctoolset/WEB-INF/views/testReadRest/varPost.jsp. Reason: NOT_FOUND 

有什麼建議嗎?

[編輯]

這裏有豆

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <!-- Imports user-defined @Controller beans that process client requests --> 
    <beans:import resource="controllers.xml" /> 

</beans:beans> 

和controller.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 

    <!-- Scans within the base package of the application for @Components to 
     configure as beans --> 
    <context:component-scan base-package="test.tool" /> 

    <tx:annotation-driven /> 
    <mvc:annotation-driven /> 

    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <bean id="validator" 
     class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> 

</beans> 
+0

請求實際上是否在您的處理程序方法中結束?我認爲除了「/ testReadRest」之外,類型級配置可以防止處理請求。嘗試將其設置爲「@RequestMapping(value =」/ testReadRest/**「)'如果這是一個實際問題。如果你的映射工作,請忽略我。 – Wolfram

回答

4

我建立與您使用的原型的新項目。

您從@Controller方法返回void,所以這個section from the docs適用:

void if the method handles the response itself (by writing the response content directly, declaring an argument of type ServletResponse/HttpServletResponse for that purpose) or if the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature).

因此,the view is resolved based on the path,你的情況「varPost」或任何你把在路徑的末尾。擴展名「的.jsp」追加由於此默認配置:

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

您可以添加丟失的文件,但我想這不是你想要做什麼。相反,您可以更改該處理方法的簽名,並明確設置視圖,就像這樣:

@RequestMapping(value = "/{varPost}", method = RequestMethod.GET) 
public String getVarFromURI(@PathVariable("varPost") String theVar, 
     Model model) { 
    model.addAttribute("varFromClient", theVar); 
    model.addAttribute("tokenFromClient", "[NOT SET]"); 
    return "someview"; 
} 

這將導致以下合理的錯誤:

The requested resource (/testapp/WEB-INF/views/someview.jsp) is not available.

現在,你只需要創建一個視圖。然後,您可以使用路徑變量而不干擾視圖分辨率。

如果您不想呈現JSP,您可以先閱讀有關"Supported method return types"的文檔。如果您想完全自行處理響應,則可以再次返回void,並返回add the request and response as arguments

+0

非常感謝你4我的問題困擾。你的參考資料對像我這樣的春天新人來說是最有用的。 TY非常非常! –

+0

只是測試它。像天堂一樣工作。感覺很開心:-D –