Reputation: 2294
I am trying to set up the default page of my application to index.html
My folder structure is
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
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
<url-pattern>/</url-pattern>
/WEB-INF/views/index.html
and then forwarded to be handled by some other servlet/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
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