為了將css引入jsp中,今天可真是踩了好多坑,最后在stackoverflow上找到了解決方法,不多說貼出代碼。
在web.xml中添加以下代碼:
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping>
spring-servlet.xml配置如下:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd "> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <mvc:resources mapping="/resources/**" location="/resources/css/" cache-period="31556926"/> <!-- 掃描web包,應用Spring的注解 --> <context:component-scan base-package="main.com.winjay.blog.web"/> <!-- 配置視圖解析器,將ModelAndView及字符串解析為具體的頁面 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/" p:suffix=".jsp" /> </beans>
到這里還並沒有完成,我開始把包含css的floder放在WEB-INF目錄下,發現還是不能引入css,后來我把那個floder放在WebRoot目錄下成功引入了!