栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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());@Overrideprotected 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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/404946.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号