簡體   English   中英

Spring MVC將參數映射到.jsp而不是@RequestMapping

[英]Spring MVC maps parameter to .jsp instead of @RequestMapping

我已經使用Maven原型創建了一個示例spring-mvc應用程序

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中讀取一些參數:

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>

我用您使用的原型建立了一個新項目。

您從@Controller方法返回void ,因此適用於本節的內容

void ,如果方法處理反應本身(通過直接寫入的響應內容,聲明類型的ServletResponse / HttpServletResponse的用於該目的的一個參數),或者如果視圖名稱應該通過一個被隱式地確定RequestToViewNameTranslator (未聲明的響應參數處理程序方法簽名)。

因此, 根據路徑 (在您的情況下為“ 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";
}

這將導致以下合理錯誤:

所請求的資源(/testapp/WEB-INF/views/someview.jsp)不可用。

現在,您只需要創建該視圖。 然后,您可以使用path變量,而不會影響視圖分辨率。

如果您不想呈現JSP,則可以先閱讀有關“支持的方法返回類型”的文檔。 如果您想完全自己處理響應,則可以再次返回void 並將請求和響應添加為arguments

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM