尚硅谷SpringMVC代码笔记之SpringMVC_2

项目结构:

首页index.jsp, 员工列表list.jsp, 添加或修改员工input.jsp界面

index.jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
	$(function(){
		$("#testJson").click(function(){
			var url=this.href;
			var args={};
			$.post(url,args,function(data){
				for(var i=0;i<data.length;i++){
					var id=data[i].id;
					var lastName=data[i].lastName;
					alert(id+": "+lastName);
				}
			});
			return false;
		});
	});
</script>
</head>
<body>

	<a href="emps">List All Employees</a>
	<br><br>

	<a href="testJson" id="testJson">Test Json</a>
	<br><br>

	<form action="testHttpMessageConverter" method="post" enctype="multipart/form-data">
		File: <input type="file" name="file"/>
		<br>
		Desc: <input type="text" name="desc"/>
		<input type="submit" value="Test HttpMessageConverter"/>
	</form>
	<br>

	<a href="testResponseEntity">Test ResponseEntity</a>
	<br>

	<!--
		关于国际化:
		1. 在页面上能够根据浏览器语言设置的情况对文本(不是内容), 时间, 数值进行本地化处理
		2. 可以在 bean 中获取国际化资源文件 Locale 对应的消息
		3. 可以通过超链接切换 Locale, 而不再依赖于浏览器的语言设置情况

		解决:
		1. 使用 JSTL 的 fmt 标签
		2. 在 bean 中注入 ResourceBundleMessageSource 的示例, 使用其对应的 getMessage 方法即可
		3. 配置 LocalResolver 和 LocaleChangeInterceptor
	-->
	<a href="i18n">I18N PAGE</a>
	<br><br>

	<form action="testFileUpload" method="post" enctype="multipart/form-data">
		File: <input type="file" name="file"/><br>
		Desc: <input type="text" name="desc"/>
		<input type="submit" value="文件上传"/>
	</form>
	<br>

	<a href="testExceptionHandlerExceptionResolver?i=10">Test ExceptionHandlerExceptionResolver</a>
	<br>

	<a href="testResponseStatusExceptionResolver?i=10">Test ResponseStatusExceptionResolver</a>
	<br>

	<a href="testDefaultHandlerExceptionResolver">Test DefaultHandlerExceptionResolver</a>
	<br>

	<a href="testSimpleMappingExceptionResolver?i=2">Test SimpleMappingExceptionResolver</a>
	<br><br>
</body>
</html>

list.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>
<!--
	SpringMVC 处理静态资源:
	1. 为什么会有这样的问题:
	优雅的 REST 风格的资源URL 不希望带 .html 或 .do 等后缀
	若将 DispatcherServlet 请求映射配置为 /,
	则 Spring MVC 将捕获 WEB 容器的所有请求, 包括静态资源的请求, SpringMVC 会将他们当成一个普通请求处理,
	因找不到对应处理器将导致错误。
	2. 解决: 在 SpringMVC 的配置文件中配置 <mvc:default-servlet-handler/>
-->
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function(){
	$(".delete").click(function(){
		var href = $(this).attr("href");
		$("form").attr("action", href).submit();
		return false;
	});
})
</script>
</head>
<body>

	<form action="" method="post">
		<input type="hidden" name="_method" value="DELETE"/>
	</form>

	<c:if test="${empty requestScope.employees }">
		没有任何员工信息.
	</c:if>
	<c:if test="${!empty requestScope.employees }">
		<table border="1" cellpadding="10" cellspacing="0">
			<tr>
				<th>ID</th>
				<th>LastName</th>
				<th>Email</th>
				<th>Gender</th>
				<th>Department</th>
				<th>Edit</th>
				<th>Delete</th>
			</tr>

			<c:forEach items="${requestScope.employees }" var="emp">
				<tr>
					<td>${emp.id }</td>
					<td>${emp.lastName }</td>
					<td>${emp.email }</td>
					<td>${emp.gender == 0 ? 'Female' : 'Male' }</td>
					<td>${emp.department.departmentName }</td>
					<td><a href="emp/${emp.id}">Edit</a></td>
					<td><a class="delete" href="emp/${emp.id}">Delete</a></td>
				</tr>
			</c:forEach>
		</table>
	</c:if>
	<br><br>

	<a href="emp">Add New Employee</a>
	<br><br>
</body>
</html>

input.jsp代码:

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fm" uri="http://www.springframework.org/tags/form" %>
<%@ 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>

	<!--
		1. WHY 使用 form 标签呢 ?
		可以更快速的开发出表单页面, 而且可以更方便的进行表单值的回显
		2. 注意:
		可以通过 modelAttribute 属性指定绑定的模型属性,
		若没有指定该属性,则默认从 request 域对象中读取 command 的表单 bean
		如果该属性值也不存在,则会发生错误。
	-->
	<fm:form action="${pageContext.request.contextPath }/emp" method="post"
		modelAttribute="employee">

		<fm:errors path="*"></fm:errors>
		<br>

		<!-- id为空则为添加,不为空则为修改 -->
		<c:if test="${employee.id==null }">
			LastName: <fm:input path="lastName"/>
			<fm:errors path="lastName"></fm:errors>
		</c:if>

		<c:if test="${employee.id!=null }">
			<fm:hidden path="id"/>
			<input type="hidden" name="_method" value="PUT"/>
			<%-- 对于 _method 不能使用 fm:hidden 标签, 因为 modelAttribute 对应的 bean 中没有 _method 这个属性 --%>
			<%--
			<fm:hidden path="_method" value="PUT"/>
			--%>
		</c:if>
		<br>

		Email: <fm:input path="email"/>
		<fm:errors path="email"></fm:errors>
		<br>

		<%
			Map<String,String> genders=new HashMap();
			genders.put("1", "Male");
			genders.put("0", "Female");

			request.setAttribute("genders", genders);
		%>
		Gender:<br>
		<fm:radiobuttons path="gender" items="${genders }" delimiter="<br>"/>
		<br>

		Department:
		<fm:select path="department.id" items="${departments }" itemLabel="departmentName"
		 itemValue="id"></fm:select>
		 <br>

		 <!--
			1. 数据类型转换
			2. 数据类型格式化
			3. 数据校验.
			1). 如何校验 ? 注解 ?
			①. 使用 JSR 303 验证标准
			②. 加入 hibernate validator 验证框架的 jar 包
			③. 在 SpringMVC 配置文件中添加 <mvc:annotation-driven />
			④. 需要在 bean 的属性上添加对应的注解
			⑤. 在目标方法 bean 类型的前面添加 @Valid 注解
			2). 验证出错转向到哪一个页面 ?
			注意: 需校验的 Bean 对象和其绑定结果对象或错误对象时成对出现的,它们之间不允许声明其他的入参
			3). 错误消息 ? 如何显示, 如何把错误消息进行国际化
		-->
		Birth: <fm:input path="birth"/>
		<fm:errors path="birth"></fm:errors>
		<br>

		Salary: <fm:input path="salary"/>
		<br>

		<input type="submit" value="Submit">
	</fm:form>
	<br><br>

	<form action="testConversionServiceConverer" method="post">
		<!-- lastname-email-gender-department.id 例如: [email protected] -->
		lastname-email-gender-department.id 例如: [email protected]<br>
		Employee: <input type="text" name="employee"/>
		<input type="submit" value="Submit"/>
	</form>
</body>
</html>

实体类:

public class Department {
	private Integer id;
	private String departmentName;
        .........
}
public class Employee {
	private Integer id;
	@NotEmpty
	private String lastName;
	@Email
	private String email;
	private Integer gender; // 1 male, 0 female

	private Department department;

	@Past
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birth;

	@NumberFormat(pattern="#,###,###.#")
	private Float salary;
        .......
}


dao代码:

@Repository
public class DepartmentDao {

	private static Map<Integer,Department> departments=null;

	static{
		departments=new HashMap<Integer, Department>();

		departments.put(101, new Department(101, "D-AA"));
		departments.put(102, new Department(102, "D-BB"));
		departments.put(103, new Department(103, "D-CC"));
		departments.put(104, new Department(104, "D-DD"));
		departments.put(105, new Department(105, "D-EE"));
	}

	public Collection<Department> getDepartments(){
		return departments.values();
	}

	public Department getDepartment(Integer id){
		return departments.get(id);
	}

}

@Repository
public class EmployeeDao {

	private static Map<Integer,Employee> employees=null;

	@Autowired
	private DepartmentDao departmentDao;

	static{
		employees = new HashMap<Integer, Employee>();

		employees.put(1001, new Employee(1001, "E-AA", "[email protected]", 1, new Department(101, "D-AA")));
		employees.put(1002, new Employee(1002, "E-BB", "[email protected]", 1, new Department(102, "D-BB")));
		employees.put(1003, new Employee(1003, "E-CC", "[email protected]", 0, new Department(103, "D-CC")));
		employees.put(1004, new Employee(1004, "E-DD", "[email protected]", 0, new Department(104, "D-DD")));
		employees.put(1005, new Employee(1005, "E-EE", "[email protected]", 1, new Department(105, "D-EE")));
	}

	private static Integer initId=1006;

	public void save(Employee employee){
		if(null==employee.getId()){
			employee.setId(initId++);
		}

		employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));

		employees.put(employee.getId(), employee);
	}

	public Collection<Employee> getAll(){
		return employees.values();
	}

	public Employee get(Integer id){
		return employees.get(id);
	}

	public void delete(Integer id){
		employees.remove(id);
	}

}

员工的增删改查handler控制器:

@Controller
public class EmployeeHandler {

	@Autowired
	private EmployeeDao employeeDao;
	@Autowired
	private DepartmentDao departmentDao;

	@RequestMapping("/emps")
	public String list(Map<String,Object> map){
		map.put("employees", employeeDao.getAll());
		return "list";
	}

	@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
	public String delete(@PathVariable("id") Integer id){
		employeeDao.delete(id);
		return "redirect:/emps";
	}

	@RequestMapping(value="/emp/{id}",method=RequestMethod.GET)
	public String input(@PathVariable("id") Integer id,Map<String,Object> map){
		map.put("employee", employeeDao.get(id));
		map.put("departments", departmentDao.getDepartments());
		return "input";
	}

	@RequestMapping(value="/emp",method=RequestMethod.PUT)
	public String update(Employee employee){
		employeeDao.save(employee);
		return "redirect:/emps";
	}

	@ModelAttribute
	public void getEmployee(@RequestParam(value="id",required=false) Integer id,
			Map<String,Object>map){
		if(null!=id){
			map.put("employee", employeeDao.get(id));
		}
	}

	@RequestMapping(value="/emp",method=RequestMethod.GET)
	public String input(Map<String,Object> map){
		map.put("departments", departmentDao.getDepartments());
		map.put("employee", new Employee());
		return "input";
	}

	@RequestMapping(value="/emp",method=RequestMethod.POST)
	public String save(@Valid Employee employee,Errors result,
			Map<String,Object> map){
		System.out.println("save: "+employee);
		System.out.println("error count: "+result.getErrorCount());
		if(result.getErrorCount()>0){
			System.out.println("出错了!");
			for(FieldError error:result.getFieldErrors()){
				System.out.println(error.getField()+" : "+error.getDefaultMessage());
			}
			//若验证出错, 则转向定制的页面
			map.put("departments", departmentDao.getDepartments());
			return "input";
		}
		employeeDao.save(employee);
		return "redirect:/emps";
	}
}

测试SpringMVC其他功能的handler控制器:

@Controller
public class SpringMVCTest {

	@Autowired
	private EmployeeDao employeeDao;
	@Autowired
	private ResourceBundleMessageSource messageSource;

	@RequestMapping("/testConversionServiceConverer")
	public String testConverter(@RequestParam("employee") Employee employee){
		System.out.println("save: "+employee);
		employeeDao.save(employee);
		return "redirect:/emps";
	}

	@ResponseBody
	@RequestMapping("/testJson")
	public Collection<Employee> testJson(){
		return employeeDao.getAll();
	}

	@ResponseBody
	@RequestMapping("/testHttpMessageConverter")
	public String testHttpMessageConverter(@RequestBody String body){
		System.out.println(body);
		return "helloworld! "+new Date();
	}

	@RequestMapping("/testResponseEntity")
	public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException{
		byte[] body=null;
		ServletContext servletContext=session.getServletContext();
		InputStream in=servletContext.getResourceAsStream("/files/abc.txt");
		body=new byte[in.available()];
		in.read(body);

		HttpHeaders headers=new HttpHeaders();
		headers.add("Content-Disposition", "attachment;filename=abc.txt");

		HttpStatus statusCode=HttpStatus.OK;

		ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body,headers,statusCode);
		return response;
	}

	@RequestMapping("/i18n")
	public String testI18n(Locale locale){
		String val=messageSource.getMessage("i18n.user", null, locale);
		System.out.println(val);
		return "i18n";
	}

	@RequestMapping("/testFileUpload")
	public String testFileUpload(@RequestParam("desc") String desc,
			@RequestParam("file") MultipartFile file) throws IOException{
		System.out.println("desc: "+desc);
		System.out.println("OriginalFilename: "+file.getOriginalFilename());
		System.out.println("InputStream: "+file.getInputStream());
		return "success";
	}

	@RequestMapping("/testExceptionHandlerExceptionResolver")
	public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){
		System.out.println("result: " + (10 / i));
		return "success";
	}

	/**
	 * 1. 在 @ExceptionHandler 方法的入参中可以加入 Exception 类型的参数, 该参数即对应发生的异常对象
	 * 2. @ExceptionHandler 方法的入参中不能传入 Map. 若希望把异常信息传导页面上, 需要使用 ModelAndView 作为返回值
	 * 3. @ExceptionHandler 方法标记的异常有优先级的问题.
	 * 4. @ControllerAdvice: 如果在当前 Handler 中找不到 @ExceptionHandler 方法来出来当前方法出现的异常,
	 * 则将去 @ControllerAdvice 标记的类中查找 @ExceptionHandler 标记的方法来处理异常.
	 */
//	@ExceptionHandler({ArithmeticException.class})
//	public ModelAndView handleArithmeticException(Exception ex){
//		System.out.println("出异常了: " + ex);
//		ModelAndView mv = new ModelAndView("error");
//		mv.addObject("exception", ex);
//		return mv;
//	}
	//这个用来证明异常优先级问题
//	@ExceptionHandler({RuntimeException.class})
//	public ModelAndView handleArithmeticException2(Exception ex){
//		System.out.println("[出异常了]: " + ex);
//		ModelAndView mv = new ModelAndView("error");
//		mv.addObject("exception", ex);
//		return mv;
//	}

	@ResponseStatus(reason="测试",value=HttpStatus.NOT_FOUND)
	@RequestMapping("/testResponseStatusExceptionResolver")
	public String testResponseStatusExceptionResolver(@RequestParam("i") int i){
		if(i==13){
			throw new UserNameNotMatchPasswordException();
		}
		System.out.println("testResponseStatusExceptionResolver...");
		return "success";
	}

	@RequestMapping(value="/testDefaultHandlerExceptionResolver",method=RequestMethod.POST)
	public String testDefaultHandlerExceptionResolver(){
		System.out.println("testDefaultHandlerExceptionResolver...");
		return "success";
	}

	@RequestMapping("/testSimpleMappingExceptionResolver")
	public String testSimpleMappingExceptionResolver(@RequestParam("i") int i){
		String [] vals = new String[10];
		System.out.println(vals[i]);
		return "success";
	}
}

异常处理类:

package com.atguigu.springmvc.test;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class SpringMVCTestExceptionHandler {

	@ExceptionHandler({ArithmeticException.class})
	public ModelAndView handleArithmeticException(Exception ex){
		System.out.println("--->出异常了:"+ex);
		ModelAndView mv=new ModelAndView("error");
		mv.addObject("exception",ex);
		return mv;
	}

}
package com.atguigu.springmvc.test;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value=HttpStatus.FORBIDDEN, reason="用户名和密码不匹配!")
public class UserNameNotMatchPasswordException extends RuntimeException{
  private static final long serialVersionUID = 1L;

}

国际化资源文件:

转换器:(把一个实体类按指定格式进行转换,然后存储)

package com.atguigu.springmvc.converters;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import com.atguigu.springmvc.entities.Department;
import com.atguigu.springmvc.entities.Employee;

@Component
public class EmployeeConverter implements Converter<String,Employee>{

	@Override
	public Employee convert(String source) {
		if(null!=source){
			String[] vals=source.split("-");
			//[email protected]
			if(null!=vals&&vals.length==4){
				String lastName=vals[0];
				String email=vals[1];
				Integer gender=Integer.parseInt(vals[2]);
				Department department=new Department();
				department.setId(Integer.parseInt(vals[3]));

				Employee employee=new Employee(null, lastName, email, gender, department);
				System.out.println(source+"--convert--"+employee);
				return employee;
			}
		}
		return null;
	}

}

两个拦截器:

package com.atguigu.springmvc.interceptors;

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

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class FirstInterceptor implements HandlerInterceptor{
	/**
	 * 该方法在目标方法之前被调用.
	 * 若返回值为 true, 则继续调用后续的拦截器和目标方法.
	 * 若返回值为 false, 则不会再调用后续的拦截器和目标方法.
	 * 可以考虑做权限. 日志, 事务等.
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		System.out.println("[FirstInterceptor] preHandle");
		return true;
	}

	/**
	 * 调用目标方法之后, 但渲染视图之前.
	 * 可以对请求域中的属性或视图做出修改.
	 */
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("[FirstInterceptor] postHandle");
	}

	/**
	 * 渲染视图之后被调用.
	 * 释放资源
	 */
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("[FirstInterceptor] afterCompletion");
	}

}

package com.atguigu.springmvc.interceptors;

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

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class SecondInterceptor implements HandlerInterceptor{

	/**
	 * 该方法在目标方法之前被调用.
	 * 若返回值为 true, 则继续调用后续的拦截器和目标方法.
	 * 若返回值为 false, 则不会再调用后续的拦截器和目标方法.
	 *
	 * 可以考虑做权限. 日志, 事务等.
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		System.out.println("[SecondInterceptor] preHandle");
		return true;
	}

	/**
	 * 调用目标方法之后, 但渲染视图之前.
	 * 可以对请求域中的属性或视图做出修改.
	 */
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("[SecondInterceptor] postHandle");
	}

	/**
	 * 渲染视图之后被调用. 释放资源
	 */
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("[SecondInterceptor] afterCompletion");
	}

}

SpringMVC配置文件springmvc.xml:

<?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: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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.atguigu.springmvc"/>

	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- default-servlet-handler 将在 SpringMVC 上下文中定义一个 DefaultServletHttpRequestHandler,
		它会对进入 DispatcherServlet 的请求进行筛查, 如果发现是没有经过映射的请求, 就将该请求交由 WEB 应用服务器默认的 Servlet
		处理. 如果不是静态资源的请求,才由 DispatcherServlet 继续处理 一般 WEB 应用服务器默认的 Servlet 的名称都是 default.
		若所使用的 WEB 服务器的默认 Servlet 名称不是 default,则需要通过 default-servlet-name 属性显式指定 -->
	<mvc:default-servlet-handler/>

	<mvc:annotation-driven conversion-service="conversionService"/>

	<!-- 配置 ConversionService -->
	<bean id="conversionService"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<ref bean="employeeConverter"/>
			</set>
		</property>
	</bean>

	<!-- 配置国际化资源文件 -->
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="i18n"></property>
	</bean>

	<!-- <mvc:view-controller path="/i18n" view-name="i18n"/> -->
	<mvc:view-controller path="/i18n2" view-name="i18n2" />

	<!-- 配置 SessionLocalResolver -->
	<bean id="localeResolver"
		class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
	</bean>

	<mvc:interceptors>
		<!-- 配置 LocaleChanceInterceptor -->
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
		<!-- 配置自定义的拦截器 -->
		<bean class="com.atguigu.springmvc.interceptors.FirstInterceptor"></bean>
		<!-- 配置拦截器(不)作用的路径 -->
		<mvc:interceptor>
			<mvc:mapping path="/emps" />
			<bean class="com.atguigu.springmvc.interceptors.SecondInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>

	<!-- 配置 MultipartResolver -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="UTF-8"></property>
		<property name="maxUploadSize" value="1024000"></property>
	</bean>

	<!-- 配置使用 SimpleMappingExceptionResolver 来映射异常 -->
	<bean
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionAttribute" value="exception"></property>
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
			</props>
		</property>
	</bean>
</beans>

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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>SpringMVC_2</display-name>

	<!-- 配置 SpringMVC 的 DispatcherServlet -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 配置 HiddenHttpMethodFilter: 把 POST 请求转为 DELETE、PUT 请求 -->
	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>
时间: 2024-10-05 01:52:27

尚硅谷SpringMVC代码笔记之SpringMVC_2的相关文章

尚硅谷SpringMVC代码笔记之SpringMVC_1

项目结构: 用来测试的index.jsp首页视图: index.jsp页面代码: <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "

尚硅谷SpringMVC代码笔记之SpringMVC_3

SpringMVC运行流程图: 项目结构: 首页index.jsp代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3

spring (4.0.2)——(尚硅谷)学习笔记1

1.Spring是什么? ①Spring 是一个开源框架: ②Spring 为简化企业级应用开发而生.使用Spring可以使简单的JavaBean实现以前只有EJB才能实现的功能. ③Spring是一个IOC(DI) 和 AOP容器框架. 2.具体描述Spring: ①轻量级:Spring是非侵入性的 - 基于Spring开发的应用中的对象可以不依赖于Spring 的 API: ②依赖注入:(DI --- dependency injection . IOC); ③面向切面编程:(AOP ---

2018年尚硅谷《全套Java、Android、HTML5前端视频》

全套整合一个盘里:链接:https://pan.baidu.com/s/1nwnrWOp 密码:h4bw 如果分类里没有请下载下边那些小项教程链接 感谢尚硅谷提供的视频教程:http://www.atguigu.com/ [全套Java教程--打包下载地址]Java基础阶段一.20天横扫Java基础(课堂实录)https://pan.baidu.com/s/1jJpzHv4 二.尚硅谷Java基础实战--Bank项目http://pan.baidu.com/share/link?shareid=

java学习笔记-尚硅谷0918班陈方荣:代码虐我千百遍,我视代码如初恋

伴随着周三考试的结束,尚硅谷北京Java培训第一阶段的学习也以落下帷幕,回顾这段时光,匆匆如流水,不禁感叹时间都去哪了. 分享我的学习经验: 1.加强对自己的控制,降低对自己的容忍度,以后对代码不仅要敲,还要手写. 2.在两天休息的时候对于琐碎的知识点时常复习,特别是细节上的问题. 3.对于复杂的代码,不能仅仅是死记硬背,要尝试去理解. 其实总结一下也就两个字“努力”,智商不够用的时候,这就是唯一的办法了,也是在发现与别人有着巨大差距的时候,心灵上告诫自己还有办法追赶的一种暗示吧,以上便是一个多

java学习笔记-0918班马义*,在大雨磅礴中来到尚硅谷

从9月23号在大雨磅礴中来到尚硅谷java培训,已经过去一月有余.五个月的学习路程已走完20%.第一阶段的考试也结束了.北京Java培训和我以往在大学学习的专业有很大不同,这一个多月的学习也让我的文科生思维慢慢转变过来,java很考逻辑思维,如果面向对象的思想有了,学习java就会容易一些.对于编程,我觉得最好的学习方法就是多敲代码,敲到一定程度,代码都是自然而然从指尖流露出来. 通过一个月的学习,我也慢慢找到了自己学习java的方法,每天晚上的自习时间看今天老师的java视频,根据遗忘曲线,如

java学习笔记-尚硅谷0918班刘蕾*:既然选择了远方,便只顾风雨兼程

从开学以来,一直都过着忙碌的生活,生活很规律,也很辛苦.第一次来到北京java培训这么久,自己一个人离开家这么久,才开始觉得很新鲜,特别享受一个人的感觉,但是时间久了,就想回家,怀念在家被爸妈宠爱的感觉,怀念和小伙伴们一起看电影,吃大餐的日子……但是我知道这些现在也只能想想了,毕竟选择了这条路,就不会轻易的说放弃. 尚硅谷北京java培训老师讲的很好,知识点讲的很细,在听课的时候都能够明白今天讲了什么.晚自习的时候会照着老师的代码敲一遍,然后自己单独敲,当天学的都可以很顺利的敲出来,只是过几天之

java学习笔记-0918班刘昌*:我们选择尚硅谷,也是尚硅谷选择了我们

其实,当我今天开始总结近一个月在java培训的学习期间的各种情况时,说实话,我有点不知道怎么去写,从来没有那么想过,但是还是浅谈一下吧,至少也会对自己的一个阶段有一个清醒的全面的认识. 先从北京java培训的三个方面下手吧,有补充的话,再添加一些拙见了. 一.教学方面:因为目前我们学的都是些基础,我们只是接触过宋红康老师,感觉挺不错的,教的很细致,思路很清晰. 1.当然说比起大学的老师好的多,但是客观来说,大学老师教的侧重点不一样嘛.听以前的同学说,各个老师教学技巧各有千秋,但是总的来说,宋红康

java学习笔记-尚硅谷0918班崔立*:宋老师的知识厚度,对于我们这种渴望知识的学生来讲,可为浩瀚

成长,让我感受到知识的价值与可贵,工作,让我认识到其实学习是一种常态:学校是一个单纯的地方,每个来学习的人都抱着对未来希望和憧憬. 这是我所想的:首先,我们应该先学得一身的好本事.然后就是有些事情要自己亲身去经历和体会,慢慢懂得怎样让自己这样一个体融入社会这个团体,适应社会,甚至于改变社会,这个过程比起单纯的学习知识来讲要漫长的多,没有人会言传身教. 对于在尚硅谷北京java培训的学习,以下是我的感受: 教学方面: 尚硅谷java培训在教学内容的安排上是紧凑,连贯性是很强的,便于初学者去学习和理