路线映射不起作用

时间:2015-02-09 13:36:49

标签: java spring jsp spring-mvc

我是Java Web应用程序的新手。创建了简单的HelloWorld程序,遵循使用Spring MVC框架在互联网上的教程。但是,我无法找到路径TestWebApp/helloWorld.htm无法正常工作的原因( 404错误)。

如果我查看 GlassFish 4.1 服务器日志,则会显示以下内容:

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>

调度-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>

项目结构:
enter image description here

我发现了很多类似的问题,但似乎没有答案解决了这里的问题。

2 个答案:

答案 0 :(得分:0)

您的控制器定义将请求映射为

@RequestMapping(value = "/helloWorld", method = RequestMethod.GET)

但是您访问网络应用的方式应该是

@RequestMapping(value = "/helloWorld.htm", method = RequestMethod.GET)

答案 1 :(得分:0)

你错过了

<mvc:annotation-driven />
servlet配置中的

元素。该元素将为控制器类中定义的所有 @RequestMapping 带注释的方法生成处理程序。

必须包含正确的命名空间,相关的摘录

 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"