我正在尝试将请求映射到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:
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
Use URL rewrite filter See mvc-basic example here
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
default .png .js *.css 4. Do not use RESTful URLs, use URLs with prefix:
springapp /app 5. Do not use RESTful URLs, use URLs with extension:
springapp *.do shareimprove this answerfollow edited May 23 ‘17 at 12:26
Community♦ 111 silver badge answered Jan 25 ‘10 at 2:56
axtavt 219k3636 gold badges473473 silver badges461461 bronze badges What I really want to do is use the @RequestMapping annotation with the @PathVariable annotation to achieve the use of RESTful urls. Eventually all static resources will be served by apache but that will be in a production environment. I need static resources served by tomcat when running locally, like in development. I’ve used urlrewrite filter before, and I liked it, but I’d like to find another way. – richever Jan 27 ‘10 at 4:07 I went with a combination of org.tuckey.urlrewrite filter and #4 from above. Thanks! – richever Mar 5 ‘10 at 6:46 You rock. FYI I tried #2 and it worked in my local environment, which was running under the root website in tomcat. As soon as I deployed it to a test server, under a non-root site called “test”, it stopped working. For some reason the /test/ before /static/ was throwing off the default servlet mapping. Just switched to #3 and it’s working like a charm. I’ve been struggling with this for too long. Thank you! +1 – kmehta Apr 21 ‘11 at 13:20 @axtavt : I have similar issue while accessing REST, not sure if its because of wrong mapping. Could you help me by having a look at : stackoverflow.com/questions/25285805/… Thanks! – Snehal Masne Aug 14 ‘14 at 7:04 add a comment British Columbia Investment Management Corporation (BCI) Building Meaningful Futures View all 3 job openings!
5
Did any one consider using this:
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
shareimprove this answerfollow edited Sep 4 ‘12 at 13:50 answered Sep 4 ‘12 at 13:35
Solubris 3,08822 gold badges1515 silver badges2828 bronze badges add a comment
1
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:
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
shareimprove this answerfollow edited Jul 9 ‘12 at 8:00 answered Jul 9 ‘12 at 7:25
Nereis 42111 gold badge44 silver badges1717 bronze badges add a comment
4
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.
For more details please have a look at How to handle static content in Spring MVC?
shareimprove this answerfollow edited May 23 ‘17 at 10:29
Community♦ 111 silver badge answered Apr 21 ‘10 at 12:38
ngeek 6,6021111 gold badges3333 silver badges4040 bronze badges add a comment Your Answer Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Required, but never shown
Post Your Answer By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you’re looking for? Browse other questions tagged model-view-controller spring tiles or ask your own question. The Overflow Blog The Overflow #16: How many jobs can be done at home? Socializing with co-workers while social distancing Featured on Meta Community and Moderator guidelines for escalating issues via new response… Feedback on Q2 2020 Community Roadmap Triage needs to be fixed urgently, and users need to be notified upon… Dark Mode Beta - help us root out low-contrast and un-converted bits Technical site integration observational experiment live on Stack Overflow British Columbia Investment Management Corporation (BCI) British Columbia Investment Management Corporation (BCI)
Vancouver, BC, Canada
Capital Markets500-1k people Our tech stack pythondjangojavascriptjqueryc#gitunix.netintegrationxunitgit-flowxsdmulesoftesb Meet our team Shivendra Chaudhary Shivendra Chaudhary Kevin Dowling Kevin Dowling Brian Dunn Brian Dunn View all 3 job openings! Linked 198 How to handle static content in Spring MVC? 144 Servlet for serving static content 9 Using Spring’s @RequestMapping with wildcards 2 Spring URL mapping question 2 Spring MVC 3 & Tiles 2: static resources are not displayed, even in non-tiles pages 1 Spring servlet mapping - no css or jsp! 1 Match for root url and serving of static resources 0 How to browse htm file in Spring MVC 5 How do I dispatch to WEB-INF with /* servlet-mapping? 0 Can’t find external CSS file see more linked questions… Related 2 Spring 3 MVC and Tiles 2 - can’t display static resources when controller @RequestMapping has URI template 2 Spring : Error in mapping request (No mapping found for HTTP request with URI) 1 Spring MVC mapping not working: PageNotFound 0 Spring can’t load static resources 0 Deployment failing in Spring MVC and tiles with No mapping found for HTTP request with URI 2 URL mapping is not working in web.xml in spring 1 No mapping found for HTTP request with URI [/FitnessTracker/] Hot Network Questions Draw geometric overlapping figures using latex How to change the color of specific ticks in BarLegend? Can Eduroam decrypt SSL traffic? What is N.D.E. short hand for? Which timeline is Picard set in? Why does the .z80 emulator-snapshot format have separate fields for the 8-bit refresh register and bit 7 of R? Does Minsc appear in any 5e pen & paper adventures? What does “Flowers masking kke women and men” mean? Compactify the input When not to do post-doc after Ph.D.? What happens when the Dominate Monster or Geas spells end? Integral that Mathematica can solve but Rubi can’t Asking for Fibonacci numbers, up till 50 Why was the Soviet Naval Infantry disbanded in 1947? Results that are widely accepted but no proof has appeared Counting Queen’s paths on a rectangular chessboard Counting area percentage of overlapping polygons Can you stand from prone if your speed is 5 and you have no movement remaining? Is ‘optimism’ countable? Delete set of folders contain more than one ‘-‘ in different places as a part of their name Simulating an OOP Coffee Machine Symmetric difference Via in PCB - Why not go for a solid copper filled via than a plated via? Note rejected from arXiv: what to do next? Question feed
STACK OVERFLOW Questions Jobs Developer Jobs Directory Salary Calculator Help Mobile Disable Responsiveness PRODUCTS Teams Talent Advertising Enterprise COMPANY About Press Work Here Legal Privacy Policy Contact Us STACK EXCHANGE NETWORK Technology Life / Arts Culture / Recreation Science Other Blog Facebook Twitter LinkedIn site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.4.13.36581