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
- 如果要访问jquery文件的话, 在spring-servlet.xml里加入
<mvc:resources location="/js/" mapping="/js/**"/>
然后新建一个jquery.jsp
<%@ 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>
<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<title>My JSP 'jquery.jsp' starting page</title>
<script type="text/javascript">
$(document).ready(function(){
alert("jquery");
})
</script>
</head> <body>
<h> jquery请求 </h> <br>
</body>
</body>
</html>
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");
}
}