我正在尝试使用相对路径在我的jsp文件中加载静态资源,如css文件和javascript文件,但似乎servlet映射正在覆盖映射到它们。
项目结构:
webapp/
assets/
scripts/
dashboard.js
styles/
common.css
WEB-INF/
views/
pages/
home/
body.jsp
的web.xml:
...
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...
MVC-调度-servlet.xml中:
<import resource="applicationContext.xml" />
<context:component-scan base-package="com.nickythorne.controller" />
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" >
<property name="definitions" value="/WEB-INF/tiles-definitions.xml" />
</bean>
<mvc:resources mapping="/assets/**" location="/assets/" />
我的JSP文件,我正在尝试加载样式表:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}" />
<link rel="stylesheet" type="text/css" href="<c:url value='/assets/styles/common.css'/>" />
我得到的错误是:
WARN org.springframework.web.servlet.PageNotFound:1108 - 在名为'mvc-dispatcher'的DispatcherServlet中找不到带有URI [/project/assets/styles/common.css]的HTTP请求的映射
答案 0 :(得分:0)
更好的方法是:
<c:set var="root" value="${pageContext.request.contextPath}"/>
<link rel="stylesheet" type="text/css" href="${root}/assets/styles/common.css"/>
无论端口,协议,域名等如何, ${root}
都将被解析为您的相对上下文路径。