当前位置: 首页 > 面试题库 >

使用Spring映射到web.xml中的根,找不到静态资源

潘楚
2023-03-14
问题内容

我正在尝试将请求映射到servlet根(正确的术语?)。我正在将URL映射到正确的视图,但是找不到页面的一部分的所有静态内容-CSS,JavaScript,图像。

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

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

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

@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非常简单。看起来像这样:

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);
}

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

/WEB-INF/tiles-defs.xml

正如我提到的,可以找到视图,但是找不到作为页面端口的静态资源。这是一些典型的注销日志

2019-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'


问题答案:

Stack Overflow
Products
Search…
Log in Sign up
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.


Home
PUBLIC
Stack Overflow
Tags
Users
Jobs
TEAMS
What’s this?
Free 30 Day Trial
Using Spring, mapping to root in web.xml, static resources aren’t found
Ask Question
Asked 10 years, 2 months ago
Active 7 years, 7 months ago
Viewed 58k times
British Columbia Investment Management Corporation (BCI)
Building Meaningful Futures
View all 3 job openings!

29

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

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


springapp
/

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:

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:

/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’

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

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.

model-view-controller spring tiles
shareimprove this questionfollow
edited Aug 4 ‘11 at 21:16

GEOCHET
20.1k1515 gold badges6868 silver badges9696 bronze badges
asked Jan 25 ‘10 at 2:25

richever
1,01333 gold badges1919 silver badges2828 bronze badges
add a comment
4 Answers
Active
Oldest
Votes

61

The problem is that requests for the static content go to the dispatcherServlet, because it’s mapped as / . It’s a very common problem in applications with “RESTful” URLs (that is, without any prefix in the DispatcherServlet mapping).

There are several possible ways to solve this problem:

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


springapp
/

Spring config:


See also MVC Simplifications in Spring 3

  1. Use URL rewrite filter
    See mvc-basic example here

  2. Set a prefix for the default servlet:


default
/static/*

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

  1. Set static content extensions for the default servlet:


default
.png



 类似资料:
  • 主要内容:WebJars 映射,默认静态资源映射在 Web 应用中会涉及到大量的静态资源,例如 JS、CSS 和 HTML 等。我们知道,Spring MVC 导入静态资源文件时,需要配置静态资源的映射;但在 SpringBoot 中则不再需要进行此项配置,因为 SpringBoot 已经默认完成了这一工作。 Spring Boot 默认为我们提供了 3 种静态资源映射规则: WebJars 映射 默认资源映射 静态首页(欢迎页)映射 WebJ

  • 我有以下servlet映射- 我的图像链接在html是"/图像/文件夹/ImageName.jpg"-这些图像给我一个404,而如果更改链接到"/图像/ImageName.jpg"和移动图像直接下的图像文件夹,它得到我的图像。 我是否需要以任何方式修改我的servlet映射以考虑层次结构?

  • 我在将css、js、图像映射到我的JSP时遇到了一个问题:我的项目如下所示: src/main/webapp/WEB-INF/jsp/index.jsp

  • 我使用Spring Boot1.3.3我的应用程序资源存在于src/main/Resources/静态下例如:src/main/Resources/静态/index.html我试图映射我的静态资源与前缀像/*/资源/** 以便它匹配网址像 /main/resource/**和 /app/resource/** 当我尝试使用以下代码时 当我请求域:8080/app/resource/index时,它

  • 我在Spring Boot MVC应用程序中使用了Thymeleaf模板引擎,但在加载styless时遇到了困难。链接位于文件(位于参考资料/模板/片段下),并在登录中引用。html(在参考资料/模板下)。样式表位于resources/static/css目录中。 登录。html 基于这个问题和其他来源,应用程序似乎应该能够使用指定的链接加载样式表。然而,情况并非如此。具体地说,如果我查看浏览器的

  • 我试图将tomcat 8 embedded用于我的应用程序,但我得到了以下错误: 2017年11月21日11:37:18 AM org.apache.catalina.core.StandardContext setPath 警告:上下文路径必须是空字符串或以“/”开头而不以“/”结尾。路径[/]不符合这些条件,已更改为[]使用basedir:c:\users\hzammel\downloads\