spring 使用Spring,在web.xml中映射到root,找不到静态资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2129876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:58:34  来源:igfitidea点击:

Using Spring, mapping to root in web.xml, static resources aren't found

model-view-controllerspringtiles

提问by richever

What I'm trying to do is map requests to the servlet root (correct terminology?). I'm at the point where URLs are mapped to correct view but all the static content - css, javascript, images - that is part of the page cannot be found.

我想要做的是将请求映射到 servlet 根(正确的术语?)。我正处于将 URL 映射到正确视图但所有静态内容 - css、javascript、图像 - 这是页面的一部分都找不到的时候。

So in my web.xml my servlet tag looks like this

所以在我的 web.xml 我的 servlet 标签看起来像这样

<servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

My controller looks something like this:

我的控制器看起来像这样:

@RequestMapping("/shop")
public class TheShopController extends MyBaseController {

    public static String VIEW = "Tile.Shop";

    @Override
    @RequestMapping(method = RequestMethod.GET)
    protected ModelAndView processRequest(HttpServletRequest req, HttpServletResponse resp) {
        ModelAndView mav = new ModelAndView(VIEW);
        return mav;
    }

}

MyBaseController is very simple. It looks like this:

MyBaseController 非常简单。它看起来像这样:

public abstract class MyBaseController extends AbstractController {

    protected Logger log = Logger.getLogger(getClass());

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse resp) 
        throws Exception {

        ModelAndView mav = processRequest(req, resp);
        return mav;
    }

    protected abstract ModelAndView processRequest(HttpServletRequest req, HttpServletResponse resp);
}

I'm using Tiles in my view layer. My configuration is as follows:

我在我的视图层中使用了 Tiles。我的配置如下:

/WEB-INF/tiles-defs.xml

/WEB-INF/tiles-defs.xml

As I mentioned, the views are found but the static resources that are a port of the page can't be found. Here is some typical logging out put:

正如我所提到的,找到了视图,但找不到作为页面端口的静态资源。以下是一些典型的注销:

2010-01-24 17:25:01,777 DEBUG [http-8080-7] servlet.DispatcherServlet (DispatcherServlet.java:690) - DispatcherServlet with name 'springapp' processing GET request for [/springapp/static/css/account.css] 2010-01-24 17:25:01,778 WARN [http-8080-4] servlet.DispatcherServlet (DispatcherServlet.java:962) - No mapping found for HTTP request with URI [/springapp/static/css/shop.css] in DispatcherServlet with name 'springapp' 2010-01-24 17:25:01,778 DEBUG [http-8080-6] servlet.FrameworkServlet (FrameworkServlet.java:677) - Successfully completed request 2010-01-24 17:25:01,778 WARN [http-8080-5] servlet.DispatcherServlet (DispatcherServlet.java:962) - No mapping found for HTTP request with URI [/springapp/static/css/offers.css] in DispatcherServlet with name 'springapp' 2010-01-24 17:25:01,778 WARN [http-8080-3] servlet.DispatcherServlet (DispatcherServlet.java:962) - No mapping found for HTTP request with URI [/springapp/static/css/scrollable-buttons.css] in DispatcherServlet with name 'springapp'

2010-01-24 17:25:01,777 调试 [http-8080-7] servlet.DispatcherServlet (DispatcherServlet.java:690) - 名为“springapp”的 DispatcherServlet 处理 [/springapp/static/css/account.css 的 GET 请求] 2010-01-24 17:25:01,778 WARN [http-8080-4] servlet.DispatcherServlet (DispatcherServlet.java:962) - 未找到带有 URI [/springapp/static/css/shop.css] 的 HTTP 请求的映射在 DispatcherServlet 中名为“springapp” 2010-01-24 17:25:01,778 DEBUG [http-8080-6] servlet.FrameworkServlet (FrameworkServlet.java:677) - 成功完成请求 2010-01-24 17:25:01,778 WARN [http-8080-5] servlet.DispatcherServlet (DispatcherServlet.java:962) - 在 DispatcherServlet 中找不到名称为“springapp”的带有 URI [/springapp/static/css/offers.css] 的 HTTP 请求的映射 2010-01-24 17:25:01,778 WARN [http-8080-3] servlet.DispatcherServlet (DispatcherServlet.java:962) - 在名为“springapp”的 DispatcherServlet 中找不到带有 URI [/springapp/static/css/scrollable-buttons.css] 的 HTTP 请求的映射

Going to http://localhost:8080/springapp/shopworks fine but the css and images are missing.

转到 http://localhost:8080/springapp/shop工作正常,但缺少 css 和图像。

I think that using Tiles is somehow complicating things but I"m reluctant to get rid of it. I'm wondering if I need to adjust my view resolution configuration needs to be tweeked somehow? Chaining view resolvers maybe? I'm just not that experienced in doing that.

我认为使用 Tiles 在某种程度上使事情复杂化,但我不愿意摆脱它。我想知道我是否需要调整我的视图分辨率配置需要以某种方式进行调整?可能是链接视图解析器?我只是不是那样有这样做的经验。

回答by axtavt

The problem is that requests for the static content go to the dispatcherServlet, because it's mapped as <url-pattern>/</url-pattern>. It's a very common problem in applications with "RESTful" URLs (that is, without any prefix in the DispatcherServletmapping).

问题是对静态内容的请求会转到 dispatcherServlet,因为它被映射为<url-pattern>/</url-pattern>. 这是具有“RESTful” URL(即DispatcherServlet映射中没有任何前缀)的应用程序中的一个非常常见的问题。

There are several possible ways to solve this problem:

有几种可能的方法可以解决这个问题:



Since Spring 3.x the preferred way to access static resources is to use <mvc:resources>:web.xml:

既然Spring 3.x中的首选方式访问静态资源是使用<mvc:resources>web.xml

<servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping>

Spring config:

弹簧配置:

<!-- Handles GET requests for /resources/** by efficiently serving static content 
    in the ${webappRoot}/resources dir --> 
<mvc:resources mapping="/resources/**" location="/resources/" /> 

See also MVC Simplifications in Spring 3

另请参阅Spring 3 中的 MVC 简化



1. Use URL rewrite filter
See mvc-basicexample here

1. 使用 URL 重写过滤器
参见这里的mvc-basic示例

2. Set a prefix for the defaultservlet:

2. 为defaultservlet设置前缀:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
</servlet-mapping>

That is, request for /static/images/image.pngwill return the file named /images/image.pngHowever, this way is incompatible across different servlet containers (doesn't work in Jetty), see workarounds here

也就是说,请求/static/images/image.png将返回名为的文件/images/image.png但是,这种方式在不同的 servlet 容器之间不兼容(在 Jetty 中不起作用),请参阅此处的解决方法

3. Set static content extensions for the defaultservlet:

3. 为defaultservlet设置静态内容扩展:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

4. Do not use RESTful URLs, use URLs with prefix:

4. 不要使用 RESTful URL,使用带前缀的 URL:

<servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>/app</url-pattern> 
</servlet-mapping>

5. Do not use RESTful URLs, use URLs with extension:

5. 不要使用 RESTful URL,使用带扩展名的 URL:

<servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping>

回答by Solubris

Did any one consider using this:

有没有人考虑过使用这个:

<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource requests to the container's default Servlet -->
<mvc:default-servlet-handler/>

Here is the latest spring docs on it: http://static.springsource.org/spring/docs/3.1.2.RELEASE/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-default-servlet-handler

这是最新的 spring 文档:http: //static.springsource.org/spring/docs/3.1.2.RELEASE/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-default-servlet -处理程序

回答by ngeek

As an alternative to the proposed solution number (2, default servlet which behaves differently from servlet container to servlet container), I would recommend taking a look into the Resource Servlet(org.springframework.js.resource.ResourceServlet) from the Spring Webflow project.

作为提议的解决方案编号(2,默认 servlet,其行为因 servlet 容器而异于 servlet 容器)的替代方案,我建议您查看Spring Webflow 项目中的Resource Servlet( org.springframework.js.resource.ResourceServlet)。

For more details please have a look at How to handle static content in Spring MVC?

有关更多详细信息,请查看如何在 Spring MVC 中处理静态内容?

回答by Nereis

I have the same problem but instead of using spring, I do mysefl a small filter that redirect root to my start page like that:

我有同样的问题,但我没有使用 spring,而是做了一个小过滤器,将根重定向到我的起始页,如下所示:

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

    String pageName = req.getServletPath();

    if(pageName.equals("/")) {
        res.sendRedirect( req.getContextPath() + "/start" );
    } else {
        chain.doFilter(request, response);
    }

It's maybe a trick but it look like working fine with a small code. Go here for more filter information http://www.oracle.com/technetwork/java/filters-137243.html

这可能是一个技巧,但看起来用一小段代码就可以正常工作。去这里了解更多过滤器信息http://www.oracle.com/technetwork/java/filters-137243.html