SpringMVC之访问静态文件
我们在进行springMVC开发时,必定会在jsp页面引入js文件、img文件和css文件。大多数人会将这些分类存放在WebRoot文件下新建的文件夹下面。同时,会在web.xml文件中配置拦截所有请求。这样就造成了页面无法访问到js、img和css文件夹中的文件了。
在SpringMVC中可以利用 <mvc:resources location="/img/" mapping="/img/**"/>来访问。从而解决了上述问题。
下面是,我写的一个demo。
先看看其它文件。
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>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 设置开启时的导入配置文件 。可以不是必须的文件名称,可自定义-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/*-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup> <!-- 启动tomcat时启动springMFC -->
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern><!-- 拦截所有请求 -->
</servlet-mapping>
</web-app>
先看显示图片的:img.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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 'hello.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="styles.css">
-->
</head>
<body>
你好SpringMVC!!!<br/>
<h4>显示一张图片</h4>
<br/>
<img alt="图片" src="img/we.jpg">
</div>
</body>
</html>
在java文件中:
package com.yx.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 MultiController extends MultiActionController {
public ModelAndView img(HttpServletRequest request,HttpServletResponse response){
System.out.println("-----img-------");
return new ModelAndView("/img");
}
public ModelAndView js(HttpServletRequest request,HttpServletResponse response){
System.out.println("-----js-------");
return new ModelAndView("/testJS");
}
}
以上文件与前面讲的十分的相似,改动不大。
下面是spring的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 常规单class多方法bean -->
<bean name="/test/multi" class="com.yx.controller.MultiController">
<property name="methodNameResolver">
<ref bean="paramMethodResolver"/>
</property>
</bean>
<!-- 常规单class单方法bean -->
<bean name="/test/hello" class="com.yx.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="/jsp"></property> <!-- 前缀 -->
<property name="suffix" value=".jsp"></property> <!-- 后缀 -->
</bean>
</beans>
这样子图片就可以显示出来了: