首页 > 代码库 > SpringMVC访问静态文件

SpringMVC访问静态文件

最近在看SpringMVC的视频,做一下记录。

对于SpringMVC访问静态文件,



首先要配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springMVC1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- springMVC 入口 dispatcher -->
  <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>
  		<!-- 何时启动  大于0的值表示容器启动时初始化此servlet,正值越小优先级越高-->
  		<load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 拦截 -->
  <servlet-mapping>
  	<servlet-name>springMVC</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

其次,配置spring-servlet.xml文件

<?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.0.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.0.xsd">


   
	<!-- 路径对应调用的Controller -->
	<bean name="/test/helloworld" class="com.tgb.web.controller.HelloWorldController"/>
	<!-- 视图解析 -->
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      		<property name="prefix" value=http://www.mamicode.com/"/WEB-INF/jsp">>


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 add(HttpServletRequest request,HttpServletResponse response){
		System.out.println("--------------add---------");
		return new ModelAndView("/multi","method","add");
	}

	
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response){
		
		System.out.println("--------------update---------");
		return new ModelAndView("/multi","method","update");
	}
}


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 MultiController extends MultiActionController{

	public ModelAndView add(HttpServletRequest request,HttpServletResponse response){
		System.out.println("--------------add---------");
		return new ModelAndView("/multi","method","add");
	}

	
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response){
		
		System.out.println("--------------update---------");
		return new ModelAndView("/multi","method","update");
	}
}

package com.tgb.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloWorldController implements Controller{

	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("---------------is here-----------");
		String result = "I‘m coming!!";
		//return new ModelAndView("/welcome","result",result);//传递字符串参数
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("1", "sdf");
		map.put("2", "234324");
		
		return new ModelAndView("/welcome","map",map);//传递map参数
		
	}

}

JSP页面代码

staticFile.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>Insert title here</title>
</head>
<body>
	<h> 图片</h>
	<br/>
	<div>
		<img alt="图片" src=http://www.mamicode.com/"../img/a.png">>
multi.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>Insert title here</title>
</head>
<body>
	多方法Controller
	<br/>
	此次方法${method }
</body>
</html>

welcome.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>Insert title here</title>
</head>
<body>
	Spring MVC is coming to see you!!
	<br/>
	${result }
	<br/>
	取map值
	<div>
		
		<c:forEach items="${map }" var="m">
		
		${m.key } -----> ${m.value }
		
		</c:forEach>
	</div>
	
</body>
</html>

---------------------------------------------------------------------------------------------------------------------------

细节注意点:

1、路径中action的作用和配置,即如何调用方法


2、静态文件放置的位置(注意img文件夹的位置和使用时的配置,不要放在WEB-INF文件夹下tomcat容器会给屏蔽掉


此文件夹发布到tomcat后的位置


3、使用.do后缀拦截方式