【发布时间】:2014-05-16 05:47:49
【问题描述】:
我有一个使用 Eclipse M2E 插件在 Eclipse 中基于 Maven 构建的 Spring-Hibernate 项目。虽然我的功能运行良好,但我需要为应用程序提供一些视觉吸引力,并且我希望使用 CSS 来实现同样的效果。但是,我的 CSS 从未反映在应用程序上。我已经尝试查看有关 stackoverflow 的几个密切相关的问题,但无法正确解决。
Spring not finding resource files (css, jsp...)
External CSS does not load in web page
这是我尝试过的(对 jsp 本身进行了大量的尝试和尝试):
index.jsp
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html
> xmlns="http://www.w3.org/1999/xhtml"> <%@page contentType="text/html"
> %>
>
> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@page
> session="true"%> <html> <head> <link rel="stylesheet"
> href="${pageContext.request.contextPath}/styles.css" /> <link
> rel="stylesheet" href="styles.css" /> <link rel="stylesheet"
> href="<c:url value="style.css"/>" type="text/css" />
<!-- even tried this with some reference I got somewhere,
while this atleast gave the body the color as mentioned in CSS,
nothing else worked including the form fields-->
<style>
<%@include file="styles.css"%>
</style>
</head>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring MVC Application</display-name>
<!-- Spring MVC -->
<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>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Controller 中的请求映射:
@RequestMapping("/")
public ModelAndView welcomePage(Map<String, Object> map) {
ModelAndView model = new ModelAndView();
model.setViewName("index");
// more code
return model;
}
【问题讨论】:
-
你在清除浏览器的缓存吗?
-
您确定您的 css get 包含在部署存档中吗?听起来很愚蠢,但是您是否检查过服务器上的 css 可用。使 maven 包含要构建的所需文件可能会很棘手,具体取决于它们的路径和扩展名。您可能需要应用一些自定义过滤。
-
你的评论听起来很有趣@skegg99,你能详细说明一下吗?
-
@nhavar,是的,我之前也尝试过类似的方法,位细化解决了这个问题。谢谢:)
标签: css spring maven spring-mvc web.xml