Spring MVC 子映射 @RequestMapping 抛出 404
Spring MVC sub mapping @RequestMapping throws 404
请大方告诉我们为什么我会遇到这个愚蠢的问题
没有映射一切正常!但我有一个简单的问题。我已经设置了一个父映射@“/hello”并子映射了一个方法@“/showForm”,Dispatcher 设置为“/”但我得到 404(参见代码图 3)。我找不到与我的 相似 问题的答案。
查看下面的代码和我的设置的标签
控制器图 1
@Controller
@RequestMapping("/hello")
public class HelloWorldController {
@RequestMapping("/showForm")
public String showForm() {
return "hello-world";
}
web.xml 图2
<servlet>
<servlet-name>yktech</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>yktech</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Tomcat 错误图3
Type Status Report
Message /yktech/hello/WEB-INF/view/hello-world.jsp
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
编辑:忘记了 BEAN 图。 4
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
编辑:结构树(我知道很乱)
enter image description here
编辑 4 , index.jsp
<html>
<body>
<h2>Welcome to my homepage
</h2>
<a href="hello/showForm">Show form</a>
<a href="Student/showForm">Show STUDENT form</a>
</body>
</html>
编辑 5 pom.xml
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yktech</groupId>
<artifactId>yktech</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>yktech</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>yktech</finalName>
</build>
</project>
编辑 6,服务器日志,映射 URL
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/yktech-servlet.xml]
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processForm] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processForm.*] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processForm/] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processFormVersionTwo] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processFormVersionTwo.*] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processFormVersionTwo/] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/showForm] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/showForm.*] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
registerHandler
INFO: Mapped URL path [/hello/showForm/] onto handler 'helloWorldController'
... CARRIES ON SHOWS NO ERRORS....
试试这个:
/hello
的请求将由 whatever()
处理,而 /hello/showForm
的请求将由 showForm()
处理
@Controller
@RequestMapping("/hello")
public class HelloWorldController {
@RequestMapping("/")
public String whatever() {
return "whatever";
}
@RequestMapping("/showForm")
public String showForm() {
return "hello-world";
}
问题出在视图解析器 bean 中,
前缀在开头没有“/”导致 Spring 从根而不是从 /WEB-INF/view
调用任何父级映射请求
请大方告诉我们为什么我会遇到这个愚蠢的问题
没有映射一切正常!但我有一个简单的问题。我已经设置了一个父映射@“/hello”并子映射了一个方法@“/showForm”,Dispatcher 设置为“/”但我得到 404(参见代码图 3)。我找不到与我的 相似 问题的答案。
查看下面的代码和我的设置的标签
控制器图 1
@Controller
@RequestMapping("/hello")
public class HelloWorldController {
@RequestMapping("/showForm")
public String showForm() {
return "hello-world";
}
web.xml 图2
<servlet>
<servlet-name>yktech</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>yktech</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Tomcat 错误图3
Type Status Report
Message /yktech/hello/WEB-INF/view/hello-world.jsp
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
编辑:忘记了 BEAN 图。 4
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
编辑:结构树(我知道很乱)
enter image description here
编辑 4 , index.jsp
<html>
<body>
<h2>Welcome to my homepage
</h2>
<a href="hello/showForm">Show form</a>
<a href="Student/showForm">Show STUDENT form</a>
</body>
</html>
编辑 5 pom.xml
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yktech</groupId>
<artifactId>yktech</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>yktech</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>yktech</finalName>
</build>
</project>
编辑 6,服务器日志,映射 URL
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/yktech-servlet.xml]
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processForm] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processForm.*] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processForm/] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processFormVersionTwo] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processFormVersionTwo.*] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processFormVersionTwo/] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/showForm] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/showForm.*] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
registerHandler
INFO: Mapped URL path [/hello/showForm/] onto handler 'helloWorldController'
... CARRIES ON SHOWS NO ERRORS....
试试这个:
/hello
的请求将由 whatever()
处理,而 /hello/showForm
的请求将由 showForm()
@Controller
@RequestMapping("/hello")
public class HelloWorldController {
@RequestMapping("/")
public String whatever() {
return "whatever";
}
@RequestMapping("/showForm")
public String showForm() {
return "hello-world";
}
问题出在视图解析器 bean 中, 前缀在开头没有“/”导致 Spring 从根而不是从 /WEB-INF/view
调用任何父级映射请求