在 Spring Controller 中收集 CSS/JS 资源

发布于 2025-01-02 08:47:23 字数 1965 浏览 0 评论 0 原文

我想收集控制器中的所有 css/js 资源。

这将导致每个资源产生一个 HTTP 请求。

示例:

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/**
}

我已经有一个使用 Apache Tiles 的 DispatcherServlet,所以我想我需要创建一个新的 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>

org.springframework.web.servlet.ResourceServlet 类正确吗?

那么我必须在 resources-servlet.xml 中放入什么?这?

<?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>

我的映射方法在控制器中应该是什么样子? 任何示例代码都会非常有用。在互联网上找不到任何东西...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月染过的梦 2025-01-09 08:47:23

我已经有一个使用 Apache Tiles 的 DispatcherServlet,所以我想我需要创建一个新的 servlet?!

不——您应该只有 DispatcherServlet。 -- 每个 Spring 控制器都由该 servlet 处理。


但总的来说,你所做的事情看起来很奇怪。

例如

   <mvc:resources location="/, classpath:/META-INF/web-resources/"
    mapping="/resources/**" />
  • 但也许你尝试一些更复杂的东西,比如 Jawr

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?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文