5.SpringMVC静态文件的访问

时间:2021-12-27 14:55:38

我们在进行springMVC开发时,必定会在jsp页面引入js文件、img文件和css文件。大多数人会将这些分类存放在WebRoot文件下新建的文件夹下面。同时,会在web.xml文件中配置拦截所有请求即配置<url-pattern> / </url-pattern>。这样就造成了页面无法访问到js、img和css文件夹中的文件了。

在SpringMVC中可以利用 <mvc:resources location="/img/" mapping="/img/**"/>来访问。从而解决了上述问题。

引入jar包

1.web.xml配置

<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!--基础配置有springMVC配置的servlet路径-->
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--如果需要加载其他地方的多个springMVC配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/SpringMVC-servlet.xml</param-value>
<!--classpath*代表在src下寻找config文件夹再在其中寻找以-servlet.xml文件结尾的文件-->
</init-param>
<!--配置加载顺序的-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern><!-- 拦截所有请求 -->
</servlet-mapping>
</web-app>

web.xml

2.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-3.0.xsd">
<!-- 常规单class多方法bean -->
<bean name="/test1/multi" class="controller.MultiController">
<property name="methodNameResolver">
<ref bean="paramMethodResolver" />
</property>
</bean>
<!-- 常规单class多方法bean,静态资源访问 -->
<bean name="/test1/static" class="controller.StaticController">
<property name="methodNameResolver">
<ref bean="paramMethodResolver" />
</property>
</bean>
<!-- 常规单class单方法bean -->
<bean name="/test1/hello" class="controller.HelloSpringMVCController"></bean> <!-- 静态资源访问 -->
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!--以上两句就是设置spring的拦截器不对img文件夹与js文件夹的文件进行拦截--> <!-- 参数名称解析 -->
<bean id="paramMethodResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action"></property>
</bean> <!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property> <!-- 前缀 -->
<property name="suffix" value=".jsp"></property> <!-- 后缀 -->
</bean>
</beans>

3.StaticController之间转换器,控制器

package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class StaticController extends MultiActionController {
public ModelAndView img(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("----img----");
return new ModelAndView("/staticFile");
}
public ModelAndView jQuey(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("----jquery----");
return new ModelAndView("/JQuery");
}
}

4.jQuery.jsp和

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>实现一个controller写多个方法</title>
</head>
<body>
<h>图片访问</h>
<div>
<img alt="图片" src="../img/3.png">
</div>
</body>
</html>

staticFile.jsp

结果截图

5.SpringMVC静态文件的访问

反馈的视图

<%@ 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>
<script type="text/javascript" src="../js/jquery-1.10.2.min.js">
$(document).ready(function(){
alert(11);
});
</script>
</head>
<body>
jQuery请求<br>
</body>
</html>

jQuery.jsp

5.SpringMVC静态文件的访问