Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring mvc where to put css/js/img files

Tags:

css

spring-mvc

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

like image 507
kskaradzinski Avatar asked Jan 05 '13 12:01

kskaradzinski


People also ask

Where does Spring MVC put CSS files?

1. Project Directory. A standard Maven folder structure, puts the static resources like js and css files into the webapp\resources folder.


2 Answers

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>
like image 169
Usha Avatar answered Oct 05 '22 01:10

Usha


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

like image 28
Hoàng Long Avatar answered Oct 05 '22 01:10

Hoàng Long