我已经阅读了我找到的关于这个问题的大部分相关帖子,但我找不到适合我的情况的任何有效解决方案。 基本上我的 jsp 文件似乎找不到 css 文件。
这是我的文件: jsp页面:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Search for flight</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="${pageContext.request.contextPath}/WEB-INF/pages/theme.css" rel="stylesheet" type="text/css" >
</head>
CSS 页面:
#title
{
font-family: "Times New Roman";
color: aqua;
}
mvc-dispatcher-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.mkyong.common.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
网络.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<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>
</web-app>
而且 Controller 工作正常,所以我发现发布它没有任何实际用处。
jsp 文件在浏览器中可见,但样式完全不存在。
提前致谢!
最佳答案
你需要在 dispatcher-servlet 文件中声明你的资源
<mvc:resources mapping="/resources/**" location="/resources/css/" />
任何带有url映射/resources/**的请求都会直接寻找/resources/css/
现在在 jsp 文件中你需要像这样包含你的 css 文件:
<link href="<c:url value="/resources/css/your file.css" />" rel="stylesheet">
关于java - 无法在 spring web mvc 中将 .jsp 与 .css 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27755808/