我想在jsp页面中包含css文件。我尝试了所有的解决方案从stackoverflow,谷歌…但是仍然没有,它不能识别CSS代码。
我尝试了这三个解决方案:
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
<spring:url value="/resources/css/main.css" var="mainCss" />
<link type="text/css" href="${pageContext.request.contextPath}/css/main.css" rel="stylesheet" />
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.projekt.springmvc" />
<annotation-driven />
<resources mapping="/resources/**" location="/resources/css/" />
<default-servlet-handler />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
web . xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ page session="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Home</title>
<!-- First solution -->
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
<!-- Second solution -->
<spring:url value="/resources/css/main.css" var="mainCss" />
<!-- Third solution -->
<link type="text/css" href="${pageContext.request.contextPath}/css/main.css" rel="stylesheet" />
</head>
<body>
<h1>
Hello world!
</h1>
</body>
</html>
main.css
body{
padding-top:70px;
font-size:30px;
}
使用Spring Tool Suite创建的Mvc项目。项目使用Maven、JRE_8、Tomcat v9.0
试试这个:
<link href="<c:url value="/resources/main.css"/>" rel="stylesheet">
在这个配置下:
<resources mapping="/resources/**" location="/resources/css/" />
您将/resources/css
中的所有内容分配给/resources/
的spring映射。当你试图链接到/resources/style
时,你实际上是链接到/resources/style/style
。
最佳解决方案
将您的配置更改为:
<resources mapping="/css/**" location="/resources/css/" />
和使用:
<link href="<c:url value="/css/main.css"/>" rel="stylesheet">