1
我想将css文件包含到jsp页面中。我尝试了所有的解决方案,从stackoverflow,谷歌...但仍然没有,它不承认的CSS代码。Spring mvc资源文件不能包含到jsp页面(Tomcat v9.0,Spring Tool Suite,Maven)
我试图与这3个解决方案:
<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;
}
的mvc与Spring工具套件创建的项目。项目使用Maven,JRE_8下,Tomcat 9.0
工程在Eclipse中,但没有在Spring工具套件,有趣的..谢谢你解!!! :) – Ivan