という事で今回はValidatorを実装します。
Mavenを使っているのでpom.xmlに以下を追加しました。
Validatorにもいろいろな実装がありますが、今回は
hibernate-validatorです。
[pom.xml]
<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.0.2.GA</version>
</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.0.2.GA</version>
</dependency>
次にController
[UserController.java]
@Controller
public class UserController {
@ModelAttribute
public InputModel setUpInputModel() {
return new InputModel();
}
@RequestMapping(method = RequestMethod.GET)
public String input() {
return "user/input";
}
@RequestMapping(method = RequestMethod.POST)
public String show(@Valid @ModelAttribute("inputModel") InputModel inputModel,
BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
return "user/input";
}
model.addAttribute("inputModel", inputModel);
return "user/show";
}
}
public class UserController {
@ModelAttribute
public InputModel setUpInputModel() {
return new InputModel();
}
@RequestMapping(method = RequestMethod.GET)
public String input() {
return "user/input";
}
@RequestMapping(method = RequestMethod.POST)
public String show(@Valid @ModelAttribute("inputModel") InputModel inputModel,
BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
return "user/input";
}
model.addAttribute("inputModel", inputModel);
return "user/show";
}
}
注目して欲しいのはValidアノテーションです。
Validアノテーションをつける事でその引数を入力チェックします。
BindingResultはチェック結果が格納され、エラーが存在した場合は
hasErrorsでTrueが返ります。
BindingResultはモデルクラスの後に引数で指定しないと例外に
なります。
どんな入力チェックを行うかはモデルクラス内に記述します。
[InputModel.java]
public class InputModel {
@NotEmpty
@Size(max = 6)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@NotEmpty
@Size(max = 6)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
上記のようにアノテーションで行うチェックを指定します。
NotEnptyであれば空白チェック、Sizeであれば桁数チェックです。
ここでエラーとなった内容を画面表示します。
[input.jsp]
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta charset="utf-8">
<title>inputのサンプル</title>
</head>
<body>
<form:form modelAttribute="inputModel">
<form:errors path="name" cssStyle="color:red"/><br>
name: <form:input path="name"/>
<input type="submit"/>
</form:form>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>inputのサンプル</title>
</head>
<body>
<form:form modelAttribute="inputModel">
<form:errors path="name" cssStyle="color:red"/><br>
name: <form:input path="name"/>
<input type="submit"/>
</form:form>
</body>
</html>
formのerrorsタグでエラー内容を出力できます。
同時にcssStyle属性でCSSを設定してます。
実行してエラーがあれば以下のように表示されます。
デフォルトでは上記のように英語表示されますが、嫌ならば
クラスパス上にValidationMessages_ja.propertiesという名前で
メッセージファイルを作成して以下のように記述します。
[ValidationMessages_ja.properties]
javax.validation.constraints.Size.message={min}と{max}の間を入力して下さい
org.hibernate.validator.constraints.NotEmpty.message=値を入力して下さい
org.hibernate.validator.constraints.NotEmpty.message=値を入力して下さい
ライブラリ名.エラーチェック名.messageという形式になってますので
適宜追加するといいです。
変更したところ