CuriousMind
CuriousMind

Reputation: 8903

Spring MVC without annotation - HTTP 404 page not found error coming

I am trying to write a simple Spring MVC web application without using annotations. However, when I access specific URL, I am getting HTTP 404 error. The relevant code snippet is below:

web.xml changes done for front controller:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee"     xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.4">
  <display-name>POCProject</display-name>
  <servlet>
    <servlet-name>FrontController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
  <servlet-mapping>
      <servlet-name>FrontController</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

The servlet name is --> FrontController

Hence, have the following configuration file --> FrontController-servlet.xml

<beans ...>

   <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

   <bean name="/userdetails" class="com.example.controllers.UserDetailsController"/>

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

In above configuration file, the Handler mapping class is --> org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

The controller class for URL /userdetails is --> com.example.controllers.UserDetailsController

The code for this controller is below:

public class UserDetailsController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse res) throws Exception {

    ModelAndView mw = new ModelAndView("userInfo");
    return mw;
    }
}

So, for a request with /userdetails the controller should send the view userInfo.html

However when I invoke this, I am getting HTTP 404 error.

I have deployed this in Apache tomcat, and the below is the message that I get in the tomcat console.

WARNING: No mapping found for HTTP request with URI [/POCProject/WEB-INF/userInfo.html] in DispatcherServlet with name "FrontController"

Any inputs on what I am missing here?

Upvotes: 0

Views: 333

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148910

The javadoc for InternalResourceViewResolver says that it is a (emphasize mine):

Wrapper for a JSP or other resource within the same web application. Exposes model objects as request attributes and forwards the request to the specified resource URL using a RequestDispatcher.

A JSP is internally converted in a true servlet by Tomcat (or by any servlet container) that is a resource suitable for forwarding. But a simple HTML is not.

That means that Spring InternalResourceViewResolver cannot use HTML views and you must convert them to JSP files.

Upvotes: 2

Related Questions