1

I'm using Tomcat 7 and want to serve a simple login screen and afterwards direct the user to a Spring MVC / EXT-JS based portal. In the web.xml I defined access to the spring area like this:

<servlet>
    <servlet-name>portal-web</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/portal-web-servlet-app-ctx.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>portal-web</servlet-name>
    <url-pattern>/portal/*</url-pattern>
</servlet-mapping>

And this part works fine (URLs to /portal/* are directed correctly). However my static login page is now not served and /index.html and /css/*.css calls return a 404 error. I tried adding the following to my web.xml:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

And that did give me access to /index.html, but the css files are still not accessible.

Here is my project layout:

-src
    -main
        +extjs
        +java
        -resources
            hibernate.cfg.xml
            log4j2.xml
        -webapp
            -css
                first.css
                second.css
            +WEB-INF
            index.html

Why isn't tomcat serving the static files under webapp/css?

Thanks!

amite
  • 140
  • 1
  • 10
  • 1
    Try placing all your static content under the resources folder. If it dosn't solve try the solution in :http://stackoverflow.com/questions/1483063/spring-mvc-3-and-handling-static-content-am-i-missing-something – BatScream Sep 17 '14 at 13:37
  • Can you show your Spring congiguration as well? – geoand Sep 17 '14 at 13:46
  • Thanks @BatScream - my resources folder was used for other stuff and since its outside the webapp dir, it does not get accessed. Following the link you posted thought I was able to fix the problem using a second resources dir. I will post what worked for me. Thanks for the help! – amite Sep 18 '14 at 11:35

1 Answers1

1

What worked for me was to give control of the static mapping to spring mvc via the spring xml file and the mvc:resources tag.

  1. In web.xml:

    <servlet>
        <servlet-name>portal-web</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/portal-web-servlet-app-ctx.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>portal-web</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
  2. In portal-web-servlet-app-ctx.xml I added the following:

    <mvc:resources mapping="/resources/**" location="/resources/" />
    
  3. My folder layout now looks like this:

    -src
        -main
            +extjs
            +java
            -resources
                hibernate.cfg.xml
                log4j2.xml
            -webapp
                -resources
                    -css
                        first.css
                        second.css
                    index.html
                +WEB-INF
    

I also recommend that anyone playing with these xmls / folders use a standalone tomcat. I was using an eclipse integrated tomcat and many times changes I made to xml files / folders weren't reflected.

amite
  • 140
  • 1
  • 10