Announcement Announcement Module
Collapse
No announcement yet.
Spring webapp can't locate CSS file or jquery file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring webapp can't locate CSS file or jquery file

    I've written a webapp that uses a css stylesheet and jquery. I've gotten both of them loaded and working outside of spring to make sure that the layouts are correct and functions work.

    However, when loading them in spring, I get 404 when the app attempts to resolve the urls used to pull them in. I'm using Firebug to view the CSS, and this is where the 404 comes from...the app is working fine other than that.

    I've checked a lot of different things, and it all looks the same as what I've done before but I just can't figure out why Spring won't load these files. I was hoping someone might be able to give me some pointers.

    The style folder is located at WEB-INF/style
    The jsps are located at WEB-INF/jsp/<various folders>/*.jsp

    My jsp code in question:

    Code:
    <html>
    <head>
    	<link rel="stylesheet" href="style/stylesheet.css" type="text/css">
    	<script src="style/jquery.js" type="text/javascript" charset="utf-8"></script>
     	<script type="text/javascript"> 
    	 	$(document).ready(function() {
     			$("iframe").load(function() { this.style.height = this.contentWindow.document.body.offsetHeight + 'px'; });	
     		});
     	</script>                 
    </head>
    I've also tried the src and href as "/style/<filename>" but that didn't work either.

    The only difference between this app and others I've done is that I'm using a SimpleUrlHandlerMapping....I'll go ahead and include relevant code for that:

    Code:
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="WEB-INF/jsp/" />
    		<property name="suffix" value=".jsp" />
    </bean>
    
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    	<property name="mappings">
    		<props>
    			<prop key="/indexatl.htm">AtlysCommController</prop>
    			<prop key="/login.htm">LoginController</prop>
    			<prop key="/atlyscomm.htm">AtlysCommController</prop>
    		</props>
    	</property>
    </bean>
    
    <bean id="viewMapController" class="com.cellularsouth.model.UrlHandler">
    	<property name="viewMap">
    		<map>
    			<entry key="indexatl.htm" value="atlcomm/index" />
    		</map>
    	</property>
    	<property name="defaultView" value="index" />
    </bean>
    I was thinking that maybe the SimpleUrlHandlerMapping was the issue but it should only apply to files that are located in WEB-INF/jsp, right?

    I'm so confused....any help would be greatly appreciated.

    Thanks,

    Brian French

  • #2
    Hi,

    I suspect in your web.xml your servlet-mapping url-pattern is /* or / meaning that anything that comes in, including css, js files they are sent to spring's dispatcher to take care of. This is probably the reason why you can't locate your js, css files.

    You can put your css files in your webapp's dir and it should be fine.

    Thusjanthan Kubendranathan.

    Comment


    • #3
      Unfortunately I wish that were the case but my servlet mapping in web.xml is set to *.htm (and always has been).

      This is what I find so confusing....I was under the impression that the servlet would only intercept urls ending with *.htm and allow me to link in files that didn't fall under that heading, so that I could do includes and things like that.

      But, I wasn't able to include jsp fragments either until I added the SimpleUrlHandlerMapping in my servlet.xml.

      Again, I'm confused...I'll include my web.xml and my entire servlet.xml for you to look over and see if you notice anything incorrect.

      And thank you very much for the response...I just hope somebody can see what I'm doing wrong

      Brian French

      WEB.XML:

      Code:
      <?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">
      
      	<servlet>
      		<servlet-name>CommMaint</servlet-name>
      		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      		<load-on-startup>1</load-on-startup>
      	</servlet>
      
      	<servlet-mapping>
      		<servlet-name>CommMaint</servlet-name>
      		<url-pattern>*.htm</url-pattern>
      	</servlet-mapping>
      
      	<listener>
      		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      	</listener>
      
      	<welcome-file-list>
      		<welcome-file>index.jsp</welcome-file>
      	</welcome-file-list>
      
      	<context-param>
      		<param-name>contextConfigLocation</param-name>
      		<param-value>/WEB-INF/CommMaint-servlet.xml</param-value>
      	</context-param>
      
      	<jsp-config>
      		<taglib>
      			<taglib-uri>/spring</taglib-uri>
      			<taglib-location>WEB-INF/tld/spring-form.tld</taglib-location>
      		</taglib>
      	</jsp-config>
      </web-app>
      *-SERVLET.XML:

      Code:
      <?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:jee="http://www.springframework.org/schema/jee"
      	xsi:schemaLocation="http://www.springframework.org/schema/beans
      		http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      		http://www.springframework.org/schema/jee
      		http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
      
      	<bean id="propertyConfigurer"
      		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      		<property name="locations"
      			value="file:C:/bea9.2/user_projects/domains/corpdomain/configuration/properties/newcommmaint.properties" />
      	</bean>
      	
      	<bean id="viewResolver"
      		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      		<property name="prefix" value="WEB-INF/jsp/" />
      		<property name="suffix" value=".jsp" />
      	</bean>
      
      <!--  CONTROLLER CONFIG -->
      	<bean name="/login.htm" id="LoginController"
      		class="com.xxx.controller.LoginController">
      		<property name="commandName" value="user" />
      		<property name="commandClass" value="com.xxx.model.User" />
      		<property name="formView" value="/comm_menu/welcome" />
      		<property name="successView" value="/comm_menu/index" />
      		<property name="userDao" ref="UserDAO" />
      	</bean>
      
      	<bean name="/atlyscomm.htm" id="AtlysCommController"
      		class="com.xxx.controller.AtlysCommController">
      		<property name="commandName" value="choice" />
      		<property name="commandClass" value="com.cellularsouth.model.atlcomm.AtlChoice" />
      		<property name="formView" value="/atlcomm/index" />
      		<property name="successView" value="/atlcomm/display"/>
      		<property name="commMaintDAO" ref="CommMaintDAO" />
      	</bean>
      	
      	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      		<property name="mappings">
      			<props>
      				<prop key="/indexatl.htm">AtlysCommController</prop>
      				<prop key="/login.htm">LoginController</prop>
      				<prop key="/atlyscomm.htm">AtlysCommController</prop>
      			</props>
      		</property>
      	</bean>
      
      	<bean id="viewMapController" class="com.xxx.model.UrlHandler">
      		<property name="viewMap">
      			<map>
      				<entry key="indexatl.htm" value="atlcomm/index" />
      			</map>
      		</property>
      		<property name="defaultView" value="index" />
      	</bean>
      
      <!-- DB AND FILE CONFIG -->
      	<bean id="CommProp" class="com.xxx.CommProp">
      		<property name="fileName" value="${commprop}" />
      	</bean>
      
      	<bean id="CommMaintDAO" class="com.xxx.jdbc.CommMaintDAO">
      		<property name="jdbcTemplate" ref="jdbcTemplate" />
      	</bean>
      
      	<jee:jndi-lookup id="dataSource" jndi-name="${db.source}" />
      
      	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      		<constructor-arg ref="dataSource" />
      	</bean>
      	
      	<bean id="UserDAO" class="com.xxx.jdbc.UserDAO">
      		<property name="jdbcTemplate" ref="jdbcTemplate" />
      	</bean>
      </beans>

      Comment


      • #4
        Anyone have any ideas? Even using a ResourceBundleMessageSource and loading the url using spring:theme isn't working...it resolved to whatever URI I choose and then gives me a 404 on the css import.

        Comment

        Working...
        X