zoukankan      html  css  js  c++  java
  • 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目录下成功引入了!

  • 相关阅读:
    springboot实现redis的分布式锁
    剑指offer--二维数组中查找
    剑指offer--二维数组中查找
    对JDK动态代理的模拟实现
    Spring(4)AOP
    设计模式之单例模式(Java)
    【Java并发系列】--Java内存模型
    maven 解决jar包冲突及简单使用
    基于注解的SpringAOP源码解析(三)
    Java代码中可以优化性能的小细节
  • 原文地址:https://www.cnblogs.com/billyu/p/5832805.html
Copyright © 2011-2022 走看看