Java Spring MVC在Dispatcher Servlet中未找到映射

Java Spring MVC在Dispatcher Servlet中未找到映射,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正开始学习春季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

我正开始学习春季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
org.springframework.web.servlet.DispatcherServlet
1.
myphotosharingapp
*.htm
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/myphotosharingapp-service.xml
jsp/index.jsp
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>

浏览文件控制器
/WEB-INF/jsp/
.jsp
浏览
myphotosharingapp-service.xml:
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按钮,我只得到一个“HTTPStatus404”错误

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


有人能帮忙吗?

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


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

您的配置是映射/browsefiles.htm,但您试图通过/springmvc/JSP/browsefiles.htm访问它

您真的需要使用
SimpleUrlHandlerMapping
?如果没有,您应该尝试SpringMVC和MappingbyAnnotation。这要容易得多。您只需要两个XMLBean来配置您的基本应用程序。以下是最佳教程:

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