Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

Spring mvc issue with loading of resources (images/css)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...