1. 新建web project
2. 导入所需jar包
3. 更改web.xml
index.jsp springMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath*:config/spring-servlet.xml 1 springMVC /
4. 创建config 包, 新建spring-servlet.xml, 添加如下语句
<mvc:resources location="/img/" mapping="/img/**"/>
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" %>My JSP 'staticFile.jsp' starting page 图片访问
7. webroot下新建img文件夹, 放入a.jpg文件
8. IE输入网址测试
http://localhost:8080/springMVC2/test1/img?do=img
- 如果要访问jquery文件的话, 在spring-servlet.xml里加入
然后新建一个jquery.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>My JSP 'jquery.jsp' starting page jquery请求
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"); } public ModelAndView jquery(HttpServletRequest request, HttpServletResponse response){ return new ModelAndView("/jquery"); }}