【问题标题】:Spring MVC No mapping found in Dispatcher ServletSpring MVC 在 Dispatcher Servlet 中找不到映射
【发布时间】:2011-12-27 22:08:45
【问题描述】:

我刚刚开始学习 Spring mvc。我浏览了很多关于同一问题的类似问题,但仍然无法解决此错误。有人可以查看我的代码并让我知道缺少什么吗?

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <servlet>
    <servlet-name>myphotosharingapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>myphotosharingapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/myphotosharingapp-service.xml</param-value>
  </context-param>

  <welcome-file-list>
    <welcome-file>
      jsp/index.jsp
    </welcome-file>
  </welcome-file-list>
</web-app>

myphotosharingapp-servlet.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- mapping -->
    <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="browsefiles.htm">browseFilesController</prop>
            </props>
        </property>
    </bean>

    <!-- The view resolver -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="browseFilesController" class="springmvc.controller.BrowseFilesController">
        <property name="browseAlbumsService" ref="browseAlbumsService"></property>
        <property name="methodNameResolver">
            <bean
                class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
                <property name="mappings">
                    <props>
                        <prop key="/browsefiles.htm">browse</prop>
                    </props>
                </property>
            </bean>
        </property>
    </bean>

</beans>

myphotosharingapp-service.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="browseAlbumsService" class="springmvc.service.BrowseAlbumsService">
    </bean>
</beans>

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Share Photos</title>
</head>
<%-- <% 
        String sourcePath = ("${sourceAlbumPath}" == null)? "" : "${sourceAlbumPath}"; 
%> --%>

<body>
    <h1>
        Welcome         
    </h1>
    <form name="frmHome" method="Post" action="browsefiles.htm">
        <a href=".">View Albums</a>
        <br>
        <br>
        <input type="text" name="sourceAlbumPath" value="">
        </input>
        <button name="Browse" >
            Browse      
        </button>           
    </form>
</body>
</html>

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Share Photos</title>
</head>
<body>
    <h1>
        Welcome!!           
        <a href="jsp/home.jsp">home</a> 
    </h1>

</body>
</html>

索引页面加载正常,主页也加载正常。但是点击 home.jsp 上的 Browse 按钮​​,我只得到一个“HTTP STATUS 404”错误。

Tomcat 说“在名为 myphotosharingapp 的 Dispatcher Servlet 中没有 [/springmvc/jsp/browsefiles.htm] 的映射”

有人可以帮忙吗?

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    主页中的路径是相对的,指向/jsp/browsefiles.htm,而xml中的映射是/browsefiles.htm。

    一种解决方案是使用 JSP 中的 request.getContextPath() 或 Spring MVC 的 &lt;spring:url&gt; JSP 标签使 URL 相对于 base。

    【讨论】:

      【解决方案2】:

      您的配置是映射 /browsefiles.htm 但您尝试通过 /springmvc/jsp/browsefiles.htm 访问它

      你真的需要使用SimpleUrlHandlerMapping吗?如果没有,您应该尝试 Spring MVC 和通过注解映射。这要容易得多。您只需要两个 xml bean 来配置您的基本应用程序。这是最好的教程:

      http://blog.springsource.org/2011/01/04/green-beans-getting-started-with-spring-mvc/

      我知道这不是答案,但我认为最好学习如何使用 Spring 注释。既然你是新人,我认为最好给你这个建议,以防你不知道。

      【讨论】:

        猜你喜欢
        • 2016-12-31
        • 1970-01-01
        • 2016-03-09
        • 1970-01-01
        • 1970-01-01
        • 2016-06-30
        • 2011-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多