当前位置: 代码迷 >> Web前端 >> 基于Spring MVC的Web应用开发(九) - Exceptions -Validation
  详细解决方案

基于Spring MVC的Web应用开发(九) - Exceptions -Validation

热度:736   发布时间:2013-03-04 17:22:13.0
基于Spring MVC的Web应用开发(9) - Exceptions --Validation

本文介绍SpringMVC中的异常处理,@Controller注解的方法可能由于各种各样的原因抛出异常,如果没有写try...catch()...语句,异常的堆栈信息将直接抛给浏览器,这样对用户来说很不友好,并且异常的堆栈信息可能含有一些敏感信息(如数据库的表字段,sql语句等等...)是不能暴露出去的。因此在程序中最好捕捉到所有的异常并处理后将友好的界面或者信息返回给客户端,SpringMVC提供了一个Handler,该handler指定一种异常,并返回一个view,举个例子,增加一个Controller,叫ExceptionController:

?

Java代码 复制代码?收藏代码
  1. package?org.springframework.samples.mvc.exceptions; ??
  2. ??
  3. import?org.springframework.stereotype.Controller; ??
  4. import?org.springframework.web.bind.annotation.ExceptionHandler; ??
  5. import?org.springframework.web.bind.annotation.RequestMapping; ??
  6. import?org.springframework.web.bind.annotation.ResponseBody; ??
  7. ??
  8. @Controller??
  9. public?class?ExceptionController?{ ??
  10. ??
  11. ????@ExceptionHandler??
  12. ????public?@ResponseBody?String?handle(IllegalStateException?e)?{ ??
  13. ????????return?"IllegalStateException?handled!"; ??
  14. ????} ??
  15. ???? ??
  16. ????@RequestMapping("/exception") ??
  17. ????public?@ResponseBody?String?exception()?{ ??
  18. ????????throw?new?IllegalStateException("Sorry!"); ??
  19. ????} ??
  20. ??
  21. }??
package org.springframework.samples.mvc.exceptions;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ExceptionController {

	@ExceptionHandler
	public @ResponseBody String handle(IllegalStateException e) {
		return "IllegalStateException handled!";
	}
	
	@RequestMapping("/exception")
	public @ResponseBody String exception() {
		throw new IllegalStateException("Sorry!");
	}

}

?

@ExceptionHandler注解的方法接收一个异常类型的参数,返回值类型和@RequestMapping一样(String,void,ModelAndView...),

访问http://localhost:8080/web/exception,浏览器显示"IllegalStateException handled!"

?

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

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

?

本文介绍SpringMVC的验证(validation),在Convert一文中,我们知道SpringMVC能很方便的将提交的参数转成(convert)自定义的JavaBean,现在加入对JavaBean的验证,比如要求JavaBean的属性number(private Integer number)不允许为null且最大为5,属性date(private Date date)满足时间是未来的(今天以后的日期)。为此,pom.xml中需要添加两个依赖:

Xml代码 复制代码?收藏代码
  1. <!--?JSR?303?with?Hibernate?Validator?-->??
  2. <dependency>??
  3. ????<groupId>javax.validation</groupId>??
  4. ????<artifactId>validation-api</artifactId>??
  5. ????<version>1.0.0.GA</version>??
  6. </dependency>??
  7. <dependency>??
  8. ????<groupId>org.hibernate</groupId>??
  9. ????<artifactId>hibernate-validator</artifactId>??
  10. ????<version>4.1.0.Final</version>??
  11. </dependency>??
	<!-- JSR 303 with Hibernate Validator -->
	<dependency>
		<groupId>javax.validation</groupId>
		<artifactId>validation-api</artifactId>
		<version>1.0.0.GA</version>
	</dependency>
	<dependency>
		<groupId>org.hibernate</groupId>
		<artifactId>hibernate-validator</artifactId>
		<version>4.1.0.Final</version>
	</dependency>

?javax.validation.validation-api-1.0.0.GA.jar是一个标准,只有接口方法(以javax开头的包一般都是接口包,业界定义的一些标准,比如servlet-api-2.5.jar中的javax.servlet.HttpServletRequest),还有一个是Hibernate提供的实现包org.hibernate.hibernate-validator-4.1.0.Final。

增加一个Controller,ValidationController:

Java代码 复制代码?收藏代码
  1. package?org.springframework.samples.mvc.validation; ??
  2. ??
  3. import?javax.validation.Valid; ??
  4. ??
  5. import?org.springframework.stereotype.Controller; ??
  6. import?org.springframework.validation.BindingResult; ??
  7. import?org.springframework.web.bind.annotation.RequestMapping; ??
  8. import?org.springframework.web.bind.annotation.ResponseBody; ??
  9. ??
  10. @Controller??
  11. public?class?ValidationController?{ ??
  12. ??
  13. ????//?enforcement?of?constraints?on?the?JavaBean?arg?require?a?JSR-303?provider?on?the?classpath ??
  14. ???? ??
  15. ????@RequestMapping("/validate") ??
  16. ????public?@ResponseBody?String?validate(@Valid?JavaBean?bean,?BindingResult?result)?{ ??
  17. ????????if?(result.hasErrors())?{ ??
  18. ????????????return?"Object?has?validation?errors"; ??
  19. ????????}?else?{ ??
  20. ????????????return?"No?errors"; ??
  21. ????????} ??
  22. ????} ??
  23. ??
  24. }??
package org.springframework.samples.mvc.validation;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ValidationController {

	// enforcement of constraints on the JavaBean arg require a JSR-303 provider on the classpath
	
	@RequestMapping("/validate")
	public @ResponseBody String validate(@Valid JavaBean bean, BindingResult result) {
		if (result.hasErrors()) {
			return "Object has validation errors";
		} else {
			return "No errors";
		}
	}

}

和一个JavaBean:

Java代码 复制代码?收藏代码
  1. package?org.springframework.samples.mvc.validation; ??
  2. ??
  3. import?java.util.Date; ??
  4. ??
  5. import?javax.validation.constraints.Future; ??
  6. import?javax.validation.constraints.Max; ??
  7. import?javax.validation.constraints.NotNull; ??
  8. ??
  9. import?org.springframework.format.annotation.DateTimeFormat; ??
  10. import?org.springframework.format.annotation.DateTimeFormat.ISO; ??
  11. ??
  12. public?class?JavaBean?{ ??
  13. ???? ??
  14. ????@NotNull??
  15. ????@Max(5) ??
  16. ????private?Integer?number; ??
  17. ??
  18. ????@NotNull??
  19. ????@Future??
  20. ????@DateTimeFormat(iso=ISO.DATE) ??
  21. ????private?Date?date; ??
  22. ??
  23. ????public?Integer?getNumber()?{ ??
  24. ????????return?number; ??
  25. ????} ??
  26. ??
  27. ????public?void?setNumber(Integer?number)?{ ??
  28. ????????this.number?=?number; ??
  29. ????} ??
  30. ??
  31. ????public?Date?getDate()?{ ??
  32. ????????return?date; ??
  33. ????} ??
  34. ??
  35. ????public?void?setDate(Date?date)?{ ??
  36. ????????this.date?=?date; ??
  37. ????} ??
  38. ??
  39. }???
package org.springframework.samples.mvc.validation;

import java.util.Date;

import javax.validation.constraints.Future;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;

public class JavaBean {
	
	@NotNull
	@Max(5)
	private Integer number;

	@NotNull
	@Future
	@DateTimeFormat(iso=ISO.DATE)
	private Date date;

	public Integer getNumber() {
		return number;
	}

	public void setNumber(Integer number) {
		this.number = number;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

}?

??@NotNull、@Future就是javax.validation.validation-api-1.0.0.GA.jar中定义的注解,满足之前说到的需求。

ValidationController的validate方法绑定了一个参数BindingResult result,通过这个变量就可以得到具体的验证信息,比如在validate方法中,如果验证不通过(result.hasErrors()),返回给浏览器文本"Object has validation errors"。

?

1. 访问"http://localhost:8080/web/validate",浏览器显示"Object has validation errors",number和date不能为空;

?

2. 访问"http://localhost:8080/web/validate?number=1",浏览器显示"Object has validation errors",date不能为空;

?

3. 访问"http://localhost:8080/web/validate?number=1&date=2012-03-31",浏览器显示"Object has validation errors",非今天以后的日期;

?

4. 访问"http://localhost:8080/web/validate?number=1&date=2012-04-01",浏览器显示"No errors";

?

5. 访问"http://localhost:8080/web/validate?number=6&date=2012-04-01",浏览器显示"Object has validation errors",number不能大于5;

  相关解决方案