Github代码地址

SpringMVC02静态资源的访问

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    
 <link rel="stylesheet" type="text/css" href="statics/css/style.css">
  <script type="text/javascript" src="statics/js/jquery-1.8.3.js"></script>
  <script type="text/javascript">
  
     $(function(){
         alert("哈哈哈!");
     })
  
  </script>
  
  
  </head>
  
  <body>
   哈哈 
   <img  src="statics/images/cat.jpg" alt="没有图片">
  </body>
</html>
jsp文件夹下面的index.jsp文件

如果没有配置静态资源访问,那么页面上是没有任何效果的!因为我们配置的核心控制的url 为 /

所以这些静态的资源路径页会被拦截!两种方式解决,推荐使用后者

<?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">
  <display-name></display-name>    
  
  <!--  设置静态文件访问  -->
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>
  
  
  <!--配置springmvc的核心控制器  -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--  配置spring配置文件的位置 以及名称 -->
    <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:/springmvc-servlet.xml</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>
1.修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 处理器映射器 -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/hello1" value="myController" />
                <entry key="/hello2" value="myController" />
                <entry key="/hello3" value="myController" />
            </map>
        </property>
        <!-- <property name="mappings">
               <props> 
                <prop key="/hello1">myController</prop> 
                <prop key="/hello2">myController</prop>
                <prop key="/hello3">myController</prop> 
               </props>
             </property> -->
    </bean>

    <!-- 配置静态资源的访问 **代表子文件下面的所有文件 -->
    <mvc:resources location="/statics/" mapping="/statics/**" />
    <!-- 处理器 -->
    <bean id="myController" class="cn.bdqn.controller.MyController" />

    <!-- 配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
2.修改springmvc配置文件

 

posted @ 2016-11-30 11:01  @小葱拌豆腐  阅读(453)  评论(0编辑  收藏  举报

霸气