首页 > 代码库 > Spring MVC:在jsp中引入css

Spring MVC:在jsp中引入css

  为了将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目录下成功引入了!

Spring MVC:在jsp中引入css