Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: No handler found for correctly resolved JSP

I have written a simple Spring MVC application which has a Controller method mapped to a URL:

@RequestMapping(method = RequestMethod.GET, value = "/person_list")
public ModelAndView getPersonList()
{
    // get the list of all persons from the database and set this as the only member of our model map
    List<Person> personList = personDao.list();
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("person_list", personList);

    // pass it on as a model and view, view should resolve to "list_persons.jsp"
    return new ModelAndView("list_persons", "model", model);
}

When the URL is requested via a browser I get a 404 page, and in the Tomcat log I see a message telling me that the correctly resolved JSP view file, /WEB-INF/jsp/list_persons.jsp, is not found:

org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/my_app/WEB-INF/jsp/list_persons.jsp] in DispatcherServlet with name 'dispatcher'

In my Spring application context configuration I have used a pretty standard view resolver set up:

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

If I go into the Tomcat webapps directory where the application is deployed I can see the JSP file is where it's supposed to be, $TOMCAT_HOME/webapps/my_app/WEB-INF/jsp/list_persons.jsp

Maybe the file is being found but there is another handler mapping step that supposed to happen that I've skipped? My assumption is that once this JSP page is referenced as the view in the response then the JSP should render as HTML in the browser. I can get expected results from other views which are classes extending AbstractView, but plain JSPs give no joy.

Can anyone suggest why this isn't working as expected? Thanks in advance for any assistance/insight.

UPDATE: This was resolved by changing the servlet mapping in the web.xml from /* to /, i.e from this:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

to this:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

I hope this will be helpful to prevent someone else from wasting hours on this like I did...

like image 416
James Adams Avatar asked Mar 25 '14 07:03

James Adams


2 Answers

It seems to me like you have a url-pattern of /* for your DispatcherServlet. You have to know that a typical servlet container will have a Servlet implementation for handling JSPs. This JspServlet is typically mapped with extension mapping, like *.jsp. The path mapping pattern /* is matched before extension mapping. Therefore, your DispatcherServlet will be chosen to handle the internal forward to

/my_app/WEB-INF/jsp/list_persons.jsp

but it won't have an appropriate handler.

By changing the DispatcherServlet mapping to /, it becomes the default servlet which is matched last, so the Servlet container will have already chosen the JspServlet to handle the RequestDispatcher forward.

like image 100
Sotirios Delimanolis Avatar answered Oct 21 '22 01:10

Sotirios Delimanolis


<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

This forward all requests to spring dispatcher

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>.htm</url-pattern>
</servlet-mapping>

Yes as you said the url-pattern will send the request with patterns here to the spring dispatcher servlet. if specify something like .htm , only those requests will be handled by dispatcher as the web.xml loads first

like image 39
Santhosh Avatar answered Oct 21 '22 00:10

Santhosh