静态资源无法访问问题

 静态资源无法访问的问题有三种解决方式:

1.使用Tomcat默认的servlet解决

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>

 

2.会将对静态资源的访问请求通过HandlerMapping映射到默认Servlet请求处理器DefaultServletRequestHandler对象。而该处理器调用了Tomcat的DefaultServlet来处理静态资源的访问请求。当然需要引入mvc约束

<mvc:defult-servlet-handler/>3.方式三 在Spring3.0.4之后,Spring定义了专门用于处理静态资源请求的处理器ResourceHttpRequestHandler。
并且添加了<mvc:resources/>标签,专门用于解决静态资源无法访问问题。 Location:静态资源所在目录
mapping: 对资源的请求

<mvc:resources location="/image/"    mapping="/image / ** "></mvc:resources>

 在没有添加静态资源的解决方法之前,jpg,js,css,都无法加载出来。下面使我们添加处理后的代码。

搭建架构

FirstController
import javax.naming.ldap.Control;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;

public class FirstController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv=new ModelAndView();
         /*  mv.setViewName("index.jsp");*/
        mv.addObject("msg","hello mvc");//key he value 
        mv.setViewName("index");
        return mv;
    }
}

Springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <bean id="/hello.do" class="cn.kitty.controller.FirstController"></bean>
    <!--方式一-->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!--前缀 prefix -->
    <property name="prefix" value="/"></property>
  <!--后缀 suffix -->
    <property name="suffix" value=".jsp"></property>
</bean>
   
        </beans>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>

<html>
<body>
<img src="image/2.jpg">
<h2>Hello World!</h2>
${msg}
</body>
</html>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
  </servlet-mapping>

</web-app>

运行结果  美美哒

后两种方式的代码都是写在

 

posted @ 2017-10-27 14:56  春眠不觉笑  阅读(2631)  评论(0编辑  收藏  举报