Lakshmi
Lakshmi

Reputation: 2294

Getting 404 error while trying to load the default page in Spring

I am trying to set up the default page of my application to index.html

My folder structure is folder structure

My dispatcher-Servlet.xml is

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:view-controller path="/" view-name="index"/>
    <!-- <context:component-scan base-package="com.programcreek.helloworld.controller" /> -->
 <mvc:annotation-driven />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.html</value>
        </property>
    </bean>
</beans>

My web.xml is

<?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/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>NewAppPoc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <!-- <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file> -->
  </welcome-file-list>
  <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml
            </param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

</web-app>

I am trying to access http://localhost:7001/NewAppPoc/ so i get 404 error. I tried searching a lot but not able to get my index.html displayed I am sure I am doing something wrong but unable to find what. Any help will be appreciated.

Upvotes: 0

Views: 1723

Answers (2)

Master Slave
Master Slave

Reputation: 28569

You're getting the issue because there is no mapping that can proocess the /WEB-INF/views/index.html request.

The fastest solution is to turn your .html to .jsp files and the suffix of your InternalResourceViewResolver also from .html to .jsp. If you need to keep your html files you should configure them as static resource using mvc:resources tag in your configuration. Something like

<mvc:resources mapping="/index.html" location="/WEB-INF/views/" /> 

a longer explanation

The dynamic resources are hanled by servlets which are mapped to URLs. Now what happens with the initial request

  1. Your initial request will be handled by the DispatcherServlet cause it is mapped with <url-pattern>/</url-pattern>
  2. The handlder method returns the view name index
  3. The view name is concatenated with the prefix and sufix becoming /WEB-INF/views/index.html and then forwarded to be handled by some other servlet
  4. This will again be captured by the DispatcherServlet
  5. No handler method will be found for the mapping /WEB-INF/views/index.html and at that point it fails.

If the view suffix was set to .jsp things would work out differently. Servlet containters have a servlet that is mapped to *.jsp registered by defualt. For example tomcat's web.xml you'll see a org.apache.jasper.servlet.JspServlet mapped to *.jsp or *.jspx, so in this case a view would be rendered

Upvotes: 2

varun
varun

Reputation: 726

All your requests are mapped to dispatcher servlet, that is why it tries to resolve a view for index.jsp using view resolver. Ideally you should put your static resources in a different folder like shown below.

<mvc:resources mapping="/resources/**" location="/resources/static/" />

Or add a controller for the home path "/" which returns 'index' then it will be resolved by view resolver correctly.

Upvotes: 0

Related Questions