Spring MVC在Dispatcher Servlet中找不到映射

时间:2011-11-13 04:46:42

标签: java spring spring-mvc

我正在开始学习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说“Dispatcher Servlet中没有[/springmvc/jsp/browsefiles.htm]的映射,名称为myphotosharingapp”

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

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

解决方案是使用JSP中的request.getContextPath()或Spring MVC的<spring:url> JSP标记使URL相对于base。

答案 1 :(得分:1)

您的配置是映射/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注释会更好。既然你是新人,我认为最好在你不知道的情况下给你这个建议。