13

0 and have problem where to put public files I tried Where to place images/CSS in spring-mvc app? this soluion but its not working for me

I have tried to place my public folder every where in WEB-INF directory outside but still nothing

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <display-name>s-mvc</display-name>
  <servlet>
    <servlet-name>frontController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>frontController</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <mvc:resources mapping="/css/**" location="/css/"/>

</web-app>

frontcontroller-servlet.xml

<mvc:annotation-driven/>

    <context:component-scan base-package="pl.skowronline.controller" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

and that how I call css file <link type="text/css" href="/css/bootstrap.css" rel="stylesheet" />

Community
  • 1
  • 1
kskaradzinski
  • 4,954
  • 10
  • 48
  • 70

6 Answers6

10

I think you are getting confused with the web.xml and the application-context.xml.

The web.xml should contain the webapp xsd declarations like this, and the mapping for the Dispatcher Servlet.

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>s-mvc</display-name>
  <servlet>
    <servlet-name>frontController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>frontController</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
 </web-app>

Where as the application-context.xml contains the spring-framework xsd declarations like below

<?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:mvc="http://www.springframework.org/schema/mvc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <mvc:resources mapping="/css/**" location="/css/"/>
 <mvc:annotation-driven />
 <context:component-scan base-package="pl.skowronline.controller" />

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
 </bean>

</beans>
Usha
  • 1,458
  • 11
  • 13
  • That really helped me. An important note is I had to place the resource folders outside the WEB-INF folder. Thanks. – Marcelo Jun 29 '13 at 19:45
9

the answer from JB Nizet is correct. However, it seems the problem of your application is more than just a place to put css/js file. For Spring MVC, you must put all configuration right, or it will not work.

For simplicity, just put the css folder right in the WEB-INF folder (/WEB-INF/css), so that you can access it like this in your page:

<a href="<c:url value='/css/bootstrap.css'/>"

That link should take you directly to the css file. If it works, you can change the <a> tag into the <link> tag for CSS styling.

If it doesn't work, there are a few things to check:

1) Spring Security constraints that forbid you to access the files

2) The affect of various filters/ interceptors that can hinder your file access

3) Servlet Configuration in web.xml. Make sure that no dispatcher intercept your access to the CSS file.

Often, <mvc:resources> will do all the above things for you. But just in case it failed, you may want to have a look.

For the <mvc:resources> error:

The matching wildcard is strict, but no declaration can be found for element 'mvc:resources'.

It looks like you haven't declared the right schema yet. For example, you should add the following lines in the beginning of your file:

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

UPDATE:

As your response, the problem seems to be here:

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

You should change it to:

<servlet-mapping>
    <servlet-name>frontController</servlet-name>
    <url-pattern>/*.html</url-pattern>
  </servlet-mapping>

This will save the URL for the css/images files from conflicting with the URL mapping of controller (your servlet), and it let mvc:resources do it magic. Of course, you can use what ever extension you want for "html" part. For a beautiful URL, we may use a library like Turkey URL rewrite to solve the problem

Hoàng Long
  • 10,746
  • 20
  • 75
  • 124
  • After I add url you wrote i got 404 page with error `sty 14, 2013 11:51:03 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping found for HTTP request with URI [/s-mvc/css/bootstrap.css] in DispatcherServlet with name 'frontController' ` I have edit my web.xml and add `mvc:resources` like in first post but still this sam error – kskaradzinski Jan 14 '13 at 10:52
  • @skowron-line: it means that the mapping between URL was not made. Another thing startle me: /s-mvc/css/bootstrap.css. How can it mistakes your css link with another servlet?? – Hoàng Long Jan 15 '13 at 07:09
  • @skowron-line: ah yes, I see. You mapp ALL the request into the "/" category, which means all the requests will be processed by the frontController dispatcher servlet. You should map it to something like: frontController *.html, so that it won't conflict with the mvc:resources configuration – Hoàng Long Jan 15 '13 at 07:11
  • I try to change it like You wrote add `@RequestMapping(value="home.html")` but still I got `WARNING: No mapping found for HTTP request with URI [/s-mvc/home.html] in DispatcherServlet with name 'frontController'` – kskaradzinski Jan 15 '13 at 11:30
  • @skowron-line: do you have a logging function enabled(log4j, for example?). If you have a logging enabled, you can view at the start time that if the URL is mapped or not. You may found a tutorial here: http://www.laliluna.de/articles/posts/log4j-tutorial.html – Hoàng Long Jan 16 '13 at 03:24
  • @skowron-line: . In your case, it looks like the URL is not mapped for "/s-mvc/home.html". Would you please check if after adding mvc:resources & changing the dispatcher servlet mapping like my updated answer, can you access the css file (putting in /WEB-INF/css)? – Hoàng Long Jan 16 '13 at 03:29
  • 1
    For me, this only worked when the folder "css" was in WEB-INF's parent, for instance: `/myProject/src/main/webapp/css/main.css`. Also, servlet-mapping url-pattern of "/*.html" gave me an error that said the url-pattern was invalid, but a pattern of "/" worked fine. – ksnortum Dec 12 '13 at 23:35
  • 1
    @HoàngLong: Was unable to access the `css` dir till I had `/*.jsp` in my `web.xml`. Instead I tried @ksnortum's approach and it worked! TL;DR: Use `/` and put `resources/css` in `webapp`. – CᴴᴀZ Aug 10 '15 at 11:27
  • @ChaZ: Thanks for your comment. It really depends on your whole configuration. The mapping /*.html that I mentioned above is for the web page (html, jsp, ...), NOT for the css. As I see, you did the same for your *.jsp file, which is great :) . The main point here is avoiding the possible conflicts between mapping an URL to css files and mapping an URL to jsp/html files – Hoàng Long Aug 11 '15 at 06:09
4

This is described in the following section of the documentation. Follow the instructions given there, and then change your href: /css/bootstrap.css means: In the folder css right under the root of the server. So, unless your application is deployed as the root application, it won't work, because you need to prepend the context path of your app:

href="<c:url value='/css/bootstrap.css'/>"

And this will thus mean: in the css folder, right under the root of the webapp. If the context path of your webapp is /myFirstWebApp, the generated href will thus be

href="/myFirstWebApp/css/bootstrap.css"
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • `` add this and I got such http://pastebin.com/dx32EtQh error, And when I have changed url to `/sh/css/bootstrap.css` i got `WARNING: No mapping found for HTTP request with URI [/sh/css/bootstrap.css] in DispatcherServlet with name 'frontController'` – kskaradzinski Jan 05 '13 at 13:02
4

You can try using context path to locate resource files.

<link type="text/css" href="${pageContext.request.contextPath}/css/bootstrap.css" rel="stylesheet" />

Read this to know more.

Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50
1

I too had the same problem.Please perform below steps to get it work which worked well for me and my teammates as well.

Step1 :- Create a folder in Web-INF with name "resources"

Step2 :- Upload your bootstrap,css and js if you already do have or in case if you want to write it write inside this folder

Step3 :- Now Update your dispatcher servlet xml as below

  1. Add Below code within beans tag

    <mvc:resources mapping="/resources/**" location="/resources/" />
            <mvc:annotation-driven />
    
    1. Add below code to xsi:schemaLocation at top of this xml

      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

step 4:- now you can use css,bootstrap resouces in your code as below

<header class="custem_header_bg"><img src="resources/img/product_banner.png" class="img-responsive"> </header>
0

You can only set base on JSP as following steps. Then you don't need to set mvc:resources in web.xml. Just set base as following. Also we can use header file to set this value. Then we can include that header.jsp in to any page we are using in application. Then we dont need to set this value in every JSP.

You need to set <base/> as following...

<c:set var="req" value="${pageContext.request}" />

<c:set var="url"> ${req.requestURL} </c:set>
<c:set var="uri" value="${req.requestURI}" />

<head>
    <title></title>
    <base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/" />

Then You can Import any JS or CSS like below..

<script src="assets/js/angular/angular.js"></script>
Chinthaka Dinadasa
  • 3,332
  • 2
  • 28
  • 33