Ta *_*Sas 0 java url spring spring-mvc maven
我已经使用 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
Run Code Online (Sandbox Code Playgroud)
我一直在尝试从 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;
}
}
Run Code Online (Sandbox Code Playgroud)
但尝试时未读取参数
http://localhost:8080/test/testReadRest/varPost
相反,我收到一个错误:
Problem accessing /soctoolset/WEB-INF/views/testReadRest/varPost.jsp. Reason: NOT_FOUND
Run Code Online (Sandbox Code Playgroud)
有什么建议?
[编辑]
这里是豆子
<?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>
Run Code Online (Sandbox Code Playgroud)
和控制器.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>
Run Code Online (Sandbox Code Playgroud)
我用你使用的原型建立了一个新项目。
您正在void从该@Controller方法返回,因此文档中的这一部分适用:
void如果方法本身处理响应(通过直接编写响应内容,为此目的声明 ServletResponse / HttpServletResponse 类型的参数),或者如果视图名称应该通过RequestToViewNameTranslator隐式确定(不在处理程序中声明响应参数)方法签名)。
因此,视图是根据 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>
Run Code Online (Sandbox Code Playgroud)
您可以添加丢失的文件,但我想这不是您想要做的。相反,您可以更改处理程序方法的签名并显式设置视图,如下所示:
@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";
}
Run Code Online (Sandbox Code Playgroud)
这会导致以下合理的错误:
请求的资源 (/testapp/WEB-INF/views/someview.jsp) 不可用。
现在,您只需要创建该视图。然后您可以使用路径变量而不会干扰视图分辨率。
如果您不想呈现 JSP,您可以先阅读有关“支持的方法返回类型”的文档。如果你想完全自己处理响应,你可以void再次返回并将请求和响应添加为参数。
| 归档时间: |
|
| 查看次数: |
2426 次 |
| 最近记录: |