【问题标题】:Route mapping won't work路线图不起作用
【发布时间】:2015-04-09 06:25:58
【问题描述】:

我是 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>

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>

项目结构:

我发现了很多与此类似的问题,但似乎没有任何答案可以解决这里的问题。

【问题讨论】:

  • 您似乎缺少注释驱动的元素,例如在这里查看stackoverflow.com/questions/28364163/…,只有关于缺失元素的部分适用于您的案例
  • @Master Slave 谢谢。这解决了问题。
  • @MasterSlave 你能把它作为完整的帖子,所以我可以接受它作为答案吗?
  • 做到了,标记为重复也是一种选择,但另一个问题有一个额外的问题

标签: java spring jsp spring-mvc


【解决方案1】:

你错过了

<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"

【讨论】:

  • 另外,需要添加这行。 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
  • 是的,在答案中添加并简要说明
【解决方案2】:

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

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

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

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

【讨论】:

  • 这绝对是必需的,因为这是您正在访问的 url。你的 webapp 的上下文路径是什么,你用来访问页面的精确 url 是什么。
猜你喜欢
  • 1970-01-01
  • 2017-10-02
  • 2013-05-03
  • 2014-09-08
  • 2015-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多