路线映射不起作用 [英] Route mapping won't work
问题描述
我是Java Web应用程序的新手.使用Spring MVC框架在Internet上按照以下教程创建了简单的HelloWorld程序.但是,我找不到路径TestWebApp/helloWorld.htm
无法正常工作的原因(始终出现 404错误).
I am new in the Java web applications. Have created simple HelloWorld program, following tutorials on the internet with Spring MVC framework. But, I can't find the reason why the path TestWebApp/helloWorld.htm
won't work (404 error all the time).
如果我查看 GlassFish 4.1 服务器日志,则会显示以下内容:
If I look in the GlassFish 4.1 server log, it shows this:
Info: Mapped URL path [/helloworld] onto handler 'helloWorldController'
Info: Mapped URL path [/helloworld/*] onto handler 'helloWorldController'
HelloWorldController.java
@Controller
public class HelloWorldController {
@RequestMapping(value = "/helloWorld", method = RequestMethod.GET)
public ModelAndView helloWorld(){
String message = "Hello Spring MVC!";
return new ModelAndView("helloWorld", "message", message);
}
}
helloWorld.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
${message}
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<context:component-scan base-package="testapp"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
项目结构:
我发现了很多与此类似的问题,但是似乎没有答案可以解决这里的问题.
I have found a lot of similar questions to this, but it appears that no answer from there solves the issue here.
推荐答案
您缺少
<mvc:annotation-driven />
servlet配置中的
元素.元素将为控制器类中定义的所有 @RequestMapping 带注释的方法生成处理程序.
element inside your servlet configuration. The element will generate the handlers for all the @RequestMapping annotated methods defined in the controller classes.
还必须包括适当的名称空间,相关摘录
The proper namespace must be included as well, a relevent excerpt
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
这篇关于路线映射不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!