在我的spring mvc配置下,没有为以下类型的URL加载资源
http://hostname:8080/AppName/services/search
当我将URL更改为以下资源正确加载时
http://hostname:8080/AppName/search
我的applicationContext.xml文件如下
<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>
我的spring-security.xml文件如下:
<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" />
控制器定义如下:
@Controller
@RequestMapping(value = "/services")
public class ServicesController {
@RequestMapping(value = "/search")
public String getSearch(ModelMap map) {
return NavigationConstants.SEARCH_RESPONSE;
}
}
以下列方式访问资源:
<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">
正在正确显示AppName / contactUs和AppName / home等请求映射,并正确加载所有css和图像。但是对于请求映射,例如AppName / services / **或AppName / industries / **,没有任何css / images加载。
答案 0 :(得分:0)
您必须使用绝对路径而不是css和图像的相对路径。 尝试使用&#34; / AppName / contactUs&#34;和&#34; / AppName / home&#34;。你会发现给出绝对路径可以解决你的问题。
答案 1 :(得分:0)
链接到images和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">
根据您的视图语言,有很好的方法可以添加/ AppName部分,就像使用Velocity #springUrl
宏一样。
答案 2 :(得分:0)
我认为你应该把${pageContext.servletContext.contextPath}
放到href和src属性
<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">