david25632145
david25632145

Reputation: 3

Adding css and js to Spring project

I know that this question was answered many times on stackoverflow but I can't make it work following the steps on the answers.

First of all what I want is to add some static files (css js img) to my project to access them from html tags.

My project structure is :

https://i.sstatic.net/5bfVu.jpg

My project works just fine before adding the css but I read in the other posts that I shoud add this line to my dispatcher file :

<mvc:resources mapping="/**" location="/jsp/website/" />

But when I add it I can no more deploy and get this error :

GlassFish Server, deploy, null, false

Note :

I changer some thing in the configuration files to remove the .htm extension from the url.

My dispatcher :

<mvc:resources mapping="/**" location="/" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="indexContr"/>

My web.xml file:

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>/</welcome-file>
</welcome-file-list>

Thanks for your help.

Upvotes: 0

Views: 92

Answers (1)

AntonZ
AntonZ

Reputation: 136

  1. You have mapped as resources all namespace that is available for dispatcher servlet. Reduce it from /** to /jsp/website/**

    <mvc:resources mapping="/resources/**" location="/jsp/website/" />

  2. I would recommend you not to keep static resource under /jsp/, here should be jsp's =) Use "static" or "resources" in path

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

Upvotes: 2

Related Questions