我正在尝试使用相对路径在我的 jsp 文件中加载静态资源,例如 css 文件和 javascript 文件,但似乎 servlet 映射覆盖了对它们的映射。
项目结构:
webapp/
assets/
scripts/
dashboard.js
styles/
common.css
WEB-INF/
views/
pages/
home/
body.jsp
网页.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'/>" />
我得到的错误是:
警告 org.springframework.web.servlet.PageNotFound:1108 - 在 DispatcherServlet 中找不到带有 URI [/project/assets/styles/common.css] 的 HTTP 请求的映射,名称为“mvc-dispatcher”