2011-11-13 26 views
1

我开始学习Spring mvc。我浏览了许多关于同一问题的类似问题,但仍无法解决此错误。有人可以看看我的代码,让我知道什么是错过?Spring MVC在Dispatcher Servlet中找不到映射

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上的浏览按钮,我只是得到一个“HTTP状态404”错误。

Tomcat的说:“没有映射[/springmvc/jsp/browsefiles.htm在分发程序Servlet的名称myphotosharingapp”

能有人请帮助?

回答

1

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

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

+0

好的..谢谢.. – meenakshi

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注解。既然你是新人,我认为最好在你不知道的情况下给你这个建议。

+0

谢谢gigadot。我将学习如何使用注释.. – meenakshi

相关问题