我使用 Maven 原型(prototype)创建了一个示例 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
有什么建议吗?
[编辑]
这是 bean
<?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>
最佳答案
我使用您使用的原型(prototype)建立了一个新项目。
您将从 @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>
您可以添加丢失的文件,但我想这不是您想要做的。相反,您可以更改处理程序方法的签名并显式设置 View ,如下所示:
@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.
现在,您只需创建该 View 。然后,您可以使用路径变量,而不会干扰 View 分辨率。
如果您不想呈现 JSP,您可以首先阅读有关 "Supported method return types" 的文档。 。如果您想完全自己处理响应,可以再次返回 void
并 add the request and response as arguments .
关于java - Spring MVC 将参数映射到 .jsp 而不是 @RequestMapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11297252/