1

I would like to collect all css/js resources in a controller.

This would result in one HTTP Request for each resource.

Example:

package my.package;

// [...imports...]

@Controller
@RequestMapping( "/res" )
public class ResourcesController
{
  @RequestMapping( value = "/style.css", headers = "content-type=text/css" )
  // [...] collect all css files from /WEB-INF/css/**

  @RequestMapping( value = "/scripts.js", headers = "content-type=text/javascript" )
  // [...] collect all js files from /WEB-INF/js/**
}

I already have an DispatcherServlet which uses Apache Tiles, so I guess i need to make a new servlet?!

<servlet>
  <servlet-name>resources</servlet-name>
  <servlet-class>?org.springframework.web.servlet.ResourceServlet?</servlet-class>
  <load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>resources</servlet-name>
  <url-pattern>/res/*.css</url-pattern>
  <url-pattern>/res/*.js</url-pattern>
</servlet-mapping>

Is class org.springframework.web.servlet.ResourceServlet correct?

What I have to put in my resources-servlet.xml then? This?

<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

  <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver" />

  <context:component-scan base-package="my.package" />

</beans>

And how my mapping methods should look like in the controller? Any example code would be very useful. Can't find anything on the internet...

3
  • Not entirely sure why you would want to do this. And given the fact that you can't find any resources about it suggests that few people do. Commented Feb 3, 2012 at 13:30
  • I have absolutely no idea about the Spring part, but I can at least tell that your URL pattern is wrong. The * can only be the first of the last character. E.g. /res/* or *.js. Commented Feb 3, 2012 at 13:45
  • developer.yahoo.com/performance/rules.html Commented Feb 3, 2012 at 13:46

1 Answer 1

1

I already have an DispatcherServlet which uses Apache Tiles, so I guess I need to make a new servlet?!

No -- you should have only the DispatcherServlet. -- Every Spring Controller is handled by this servlet.


But in general it looks strange what you do.

for example

   <mvc:resources location="/, classpath:/META-INF/web-resources/"
    mapping="/resources/**" />
  • But maybe you try something more complex like Jawr?
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.