springMVC 静态文件 访问
Posted wujixing909
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springMVC 静态文件 访问相关的知识,希望对你有一定的参考价值。
1. 新建web project
2. 导入所需jar包
3. 更改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <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/spring-servlet.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> </web-app>
4. 创建config 包, 新建spring-servlet.xml, 添加如下语句
<mvc:resources location="/img/" mapping="/img/**"/>
<?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"> <context:component-scan base-package="com.tgb.web.controller" /> <bean name="/test1/img" class="com.tgb.web.controller.StaticController"> <property name="methodNameResolver"> <ref bean="paramMethodResolver"/> </property> </bean> <!-- 静态资源访问 --> <mvc:resources location="/img/" mapping="/img/**"/> <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="do"></property> </bean> <!-- ViewResolver 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
5. 新建controller包, com.tgb.web.controller, 新建StaticController.java
package com.tgb.web.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){ return new ModelAndView("/staticFile"); } }
6. 新建staticFile.jsp, 目录在webroot下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘staticFile.jsp‘ starting page</title> </head> <body> <h> 图片访问 </h> <br> <div> <img alt="图片" src="${pageContext.request.contextPath}/img/a.jpg"> </div> </body> </body> </html>
7. webroot下新建img文件夹, 放入a.jpg文件
8. IE输入网址测试
http://localhost:8080/springMVC2/test1/img?do=img
以上是关于springMVC 静态文件 访问的主要内容,如果未能解决你的问题,请参考以下文章
Springday05 SpringMVC访问静态资源操作请求域控制器方法返回值JSON数据交互RESTful风格文件上传异常处理拦截器