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.
Use the entire path when you link to images and css:
Depending on your view language, there are nice ways to add the /AppName part, like with Velocity the
#springUrl
macro.I think you should put
${pageContext.servletContext.contextPath}
to href and src attributeYou 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.