JavaScript with Spring MVC doesn't work

jantobola picture jantobola · Apr 26, 2012 · Viewed 15.4k times · Source

I downloaded login template which uses css and JavaScript. When I start it in default html format it displays ok, but when I put the same code to my Spring MVC 3 app and change format to jsp, javascript part is disabled. I can access external js files through my web browser, so I don't know where may be the problem.

This is the right look: right

but this is what i get: wrong

<script language="javascript" type="text/javascript" src="js/niceforms.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="css/niceforms-default.css" />

I only changed the location of external js and css resources but these files are accessable as I said.

This is my structure:

enter image description here

In my web.xml I have these lines:

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml,
        /WEB-INF/spring/security-context.xml,
    </param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.txt</url-pattern>
</servlet-mapping>

I also use Apache tiles2

<tiles-definitions>
<definition name="base.definition" template="/WEB-INF/views/layout/layout.jsp">
    <put-attribute name="body" value="" />
</definition>

<definition name="contact" extends="base.definition">
    <put-attribute name="body" value="/WEB-INF/views/contact.jsp" />
</definition>

<definition name="login" template="/WEB-INF/views/login/login.jsp">
</definition>

Thanks.

Edit:

I know where is the problem now, but still don't know how to solve it... Problem is in paths in external css files. I have something like this: body {background-image:url('images/bg.jpg');} but I get not found error in firebug because it tries to find the resource in localhost:8080/sheedo/css/images/bg.jpg and when I set absolute path of resource like body {background-image:url('/images/bg.jpg');} I get the same error too but it tries to find it in localhost:8080/images/bg.jpg. When I use ./images/bg.jpg it's ok, but does exist any other solution instead of rewrite all paths in css files? I mean something like <mvc:resources ... > is it possible ?

Answer

Raghuveer picture Raghuveer · Apr 27, 2012
<script type="text/javascript" src="${pageContext.request.contextPath}/js/niceforms.js"></script>
<link href="${pageContext.request.contextPath}/css/niceforms-default.css" rel="stylesheet" />

Use the above code.. You need to specify it dynamically, not statically..