-1

Under my spring mvc configuration, the resources are not loading for the following type of URLs

http://hostname:8080/AppName/services/search

When I change the URL to following the resources get loaded properly

http://hostname:8080/AppName/search

My applicationContext.xml file is as follows

<context:annotation-config/>
<context:component-scan base-package="com.inw.cns" />

<context:spring-configured/>

<jpa:repositories base-package="com.inw.cns.repository"/>


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

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- setting maximum upload size -->
    <property name="maxUploadSize" value="10000000000" />

</bean>

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:default-servlet-handler />
<mvc:annotation-driven />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://hostname:3306/DB"/>
    <property name="username" value="root"/>
    <property name="password" value="root123"/>
</bean>

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true"/>
    <property name="database" value="MYSQL"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
   <!--  spring based scanning for entity classes -->
    <property name="packagesToScan" value="com.inw.cns.domain"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>

<tx:annotation-driven mode="aspectj"
    transaction-manager="transactionManager" /> 

<!-- Tiles configuration -->

<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles/tiles-definitions.xml</value>
        </list>
    </property>
</bean>

<bean id="userRegisterationValidator" class="com.inw.cns.validator.UserRegisterationValidator" />
<bean id="basicProfileValidator" class="com.inw.cns.validator.BasicProfileValidator" />
<bean id="loginValidator" class="com.inw.cns.validator.LoginValidator" />

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

My spring-security.xml file is as follows:

<http pattern="/resources/**" security="none" />

<http authentication-manager-ref="userAuthManager">
    <intercept-url pattern="/" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/home" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/signUp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/contactUs" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/industries/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/services/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />

    <intercept-url pattern="/**" access="ROLE_USER" />

    <!-- <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />   -->
    <form-login login-page='/' authentication-failure-url="/" />
    <logout invalidate-session="true" logout-success-url="/" logout-url="/logout" />
    <session-management invalid-session-url="/">
        <concurrency-control max-sessions="1"
            expired-url="/" />
    </session-management>
</http>

<beans:bean id="userAuthManager" class="com.inw.cns.security.UserAuthManager">
</beans:bean>

<beans:bean id="passwordEncoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />

The controller is defined as follows:

@Controller
@RequestMapping(value = "/services")
public class ServicesController {

    @RequestMapping(value = "/search")
    public String getSearch(ModelMap map) {
        return NavigationConstants.SEARCH_RESPONSE;
    }
}

The resources are being accessed in the following way:

<link rel="stylesheet" type="text/css" href="resources/css/style.css" />
<script type="text/javascript" src="resources/js/custom.js" ></script>
<img src="resources/images/banner.jpg">

The request mappings such as AppName/contactUs and AppName/home are being displayed fine with all the css and images loading properly. But for the request mappings such as AppName/services/** or AppName/industries/** none of the css/images load.

2
  • The two later mappings are one level deeper, have you considered that? Commented Oct 9, 2014 at 19:00
  • Would it make a difference if the mappings are one level deeper? The pages themselves get rendered despite of them being a level deeper. The controller mapping has been added to the question. Probably there is something in the way the resources are being configured in the applicationContext.xml/spring-security.xml which prevents the loading for one level deeper. Commented Oct 10, 2014 at 7:49

3 Answers 3

0

I think you should put ${pageContext.servletContext.contextPath} to href and src attribute

<link rel="stylesheet" type="text/css" href="${pageContext.servletContext.contextPath}/resources/css/style.css" />
<script type="text/javascript" src="${pageContext.servletContext.contextPath}/resources/js/custom.js" ></script>
<img src="${pageContext.servletContext.contextPath}/resources/images/banner.jpg">
Sign up to request clarification or add additional context in comments.

3 Comments

Tried the solution. Now when I clicked on '/AppName/services/search' the resources are loading properly. But another issue has come up: URL for other pages get automatically prefixed with '/services' [the last page which was navigated]. So if I try to access a link coded as href="industries/fmcg" in my jsp, then the link is shown as '/AppName/services/industries/fmcg'. I think somehow the context for previous page is getting prefixed to other links.
Try put ${pageContext.setvletContext.contextPath} to all page
Solution worked fine. Still wonder why the issues comes up for pages which are one level deeper? Doing the changes to all links in the application is cumbersome. Also it increases the effort of changing the pages I receive from my designer.
0

You have to use absolute path instead of a relative path for css and images. Try to work with something such as "/AppName/contactUs" and "/AppName/home". You'll see that giving an absolute path fixes your problem.

Comments

0

Use the entire path when you link to images and css:

<link rel="stylesheet" type="text/css" href="/AppName/resources/css/style.css" />
<script type="text/javascript" src="/AppName/resources/js/custom.js" ></script>
<img src="/AppName/resources/images/banner.jpg">

Depending on your view language, there are nice ways to add the /AppName part, like with Velocity the #springUrl macro.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.