0

My web.xml contains

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      resources/index.html
    </welcome-file>
  </welcome-file-list>
</web-app>

The resources/index.html references to other static resources like images, js, css etc. stored in resoruces directory by relative paths.

When I put http://localhost/MyProject/ in browser, it showed up index.html but didn't get the css and javascripts.

However, if I put http://localhost/MyProject/resources/index.html in the browser, everything shows up correctly.

So, the question is how can I let the welcome page served in the url as the path given in the <welcome-file>, e.g. /resources/index.html.

If it can't be done in the <welcome-file list>, what other configurable method should I use.

I tend not to redirect to /resources/index.html by adding another html or by doing it programmatically in a Servlet controller.

1
  • You should add more tags, like 'spring', 'java'... Jul 27, 2012 at 4:50

1 Answer 1

1

It seems you are using Spring and having problems with static content.

Try looking at this link

It explains how to proceed in this case...

Pay atention to the line:

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

It maps your resources folder (containing the css, javascript and image files) to a special handler of Spring.

Update:

In your servlet-context.xml file you can add this line

<!-- Forwards requests to the "/" resource to the "welcome" view -->
    <mvc:view-controller path="/" view-name="index"/>

<!-- Resolves view names to protected .html resources within the /WEB-INF/resources/ directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/resources/"/>
        <property name="suffix" value=".html"/>
    </bean>

that says that you don't have to use the 'index.jsp' properly. This way, you will map a view to the "/" access. Summarizing, this way the user enters in 'http://localhost/MyProject/' and sees your index.html and sees the effects of css and javascripts.

PS.: - This configurations only works on Spring 3+
- Prefer naming your files to '.jsp' and not '.html'... it is simpler to map.

2
  • Vitor, spring resources mapping is defined, but it doesn't solve the problem. The problem is <welcome-file> doesn't copy the full path resources/index.html to the url. I get a feeling I have to use redirect.
    – YukunIX
    Jul 27, 2012 at 6:31
  • added spring configs as Vitor suggested, but it still didn't get .css and .js files though it displayed index.html. I think what I need is a permanent url redirect to /resources/index.html.
    – YukunIX
    Aug 2, 2012 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.