meenakshi
meenakshi

Reputation: 13

Spring MVC No mapping found in Dispatcher Servlet

I am jus starting to learn Spring mvc. I browsed through a lot of similar questions about the same problem, but am still not able to solve this error. Can someone please look into my code and let me know what am missing?

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>

The index page loads fine and the Home page too. But on click of the Browse button on the home.jsp, i just get an "HTTP STATUS 404" error.

Tomcat says "No mapping for [/springmvc/jsp/browsefiles.htm] in Dispatcher Servlet with the name myphotosharingapp"

Can someone please help??

Upvotes: 1

Views: 4248

Answers (2)

gigadot
gigadot

Reputation: 8969

Your configuration is to map /browsefiles.htm but you are try to access it through /springmvc/jsp/browsefiles.htm

Do you really need to use SimpleUrlHandlerMapping? If not, you should try Spring MVC and mapping by annotation. It is a lot easier. You only need two xml beans to configure you basic application. Here is the best tutorial:

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

I understand that this is not the answer but I think it is better to learn how to use Spring annotation. Since you are new, I think it's best to give you this advice in case you don't know.

Upvotes: 1

madth3
madth3

Reputation: 7344

The path in the home page is relative and points to /jsp/browsefiles.htm while the mapping in the xml is for /browsefiles.htm.

A solution would be to make the URL relative to base using the request.getContextPath() in the JSP or the <spring:url> JSP tag of Spring MVC.

Upvotes: 1

Related Questions