Spring MVC form:inputタグを使うサンプル
環境
Spring 5.2.22
Eclipse 4.24.0
説明
form:inputタグ
テキストの入力フィールドを作成するタグです。属性は標準的なものに加えて以下のものがあります。基本的にはHTML標準の属性です。
autocomplete HTML標準のautocomplete属性
maxlength HTML標準のmaxlength属性
readonly HTML標準のreadonly属性
size HTML標準のsize属性
操作方法
1.画面表示用モデルを作成します(InputModel.java)
public class InputModel { //クラスInputModelの定義
private String name; //名前
@Min(1)
@Max(100)
private Integer age; //年齢
}
public class InputModel { //クラスInputModelの定義
private String name; //名前
@Min(1)
@Max(100)
private Integer age; //年齢
}
public class InputModel { //クラスInputModelの定義 private String name; //名前 @Min(1) @Max(100) private Integer age; //年齢 }
ModelはStringとIntegerの2つの項目です。ageにはBean Validatorのアノテーションを設定し、1〜100までの値のみ入力可能としています。
2.コントローラは以下のようになります。
@Controller
public class FormController {
@ModelAttribute("inputModel")
public InputModel initInputModel() { //メソッドをつける
return new InputModel();
}
/*input メソッドを設定*/
@RequestMapping(value = "/input", method = RequestMethod.GET)
public String input(Model model) {
InputModel imd = new InputModel();
imd.setName("山田 太郎");
model.addAttribute("inputModel", imd);
return "input";
}
@RequestMapping(value = "/reg", method = RequestMethod.POST)
public String reg(
@Valid @ModelAttribute("inputModel") InputModel inputModel,
Errors errors) {
if (errors.hasErrors()) {
return "input";
}
return "forward:/";
}
}
@Controller
public class FormController {
@ModelAttribute("inputModel")
public InputModel initInputModel() { //メソッドをつける
return new InputModel();
}
/*input メソッドを設定*/
@RequestMapping(value = "/input", method = RequestMethod.GET)
public String input(Model model) {
InputModel imd = new InputModel();
imd.setName("山田 太郎");
model.addAttribute("inputModel", imd);
return "input";
}
@RequestMapping(value = "/reg", method = RequestMethod.POST)
public String reg(
@Valid @ModelAttribute("inputModel") InputModel inputModel,
Errors errors) {
if (errors.hasErrors()) {
return "input";
}
return "forward:/";
}
}
@Controller public class FormController { @ModelAttribute("inputModel") public InputModel initInputModel() { //メソッドをつける return new InputModel(); } /*input メソッドを設定*/ @RequestMapping(value = "/input", method = RequestMethod.GET) public String input(Model model) { InputModel imd = new InputModel(); imd.setName("山田 太郎"); model.addAttribute("inputModel", imd); return "input"; } @RequestMapping(value = "/reg", method = RequestMethod.POST) public String reg( @Valid @ModelAttribute("inputModel") InputModel inputModel, Errors errors) { if (errors.hasErrors()) { return "input"; } return "forward:/"; } }
3.JSPはValidationのエラー表示用のform:errorタグを追加しています。
サンプルコード
<form:form modelAttribute="inputModel" action="reg" method="post">
<form:errors path="*" element="div" />
<form:label path="name">名前:</form:label><form:input path="name" size="40" /><br>
<form:label path="age">年齢:</form:label><form:input path="age" size="5" cssErrorClass="error" /><br>
<input type="submit" value="送信">
</form:form>
<form:form modelAttribute="inputModel" action="reg" method="post">
<form:errors path="*" element="div" />
<form:label path="name">名前:</form:label><form:input path="name" size="40" /><br>
<form:label path="age">年齢:</form:label><form:input path="age" size="5" cssErrorClass="error" /><br>
<input type="submit" value="送信">
</form:form>
<form:form modelAttribute="inputModel" action="reg" method="post"> <form:errors path="*" element="div" /> <form:label path="name">名前:</form:label><form:input path="name" size="40" /><br> <form:label path="age">年齢:</form:label><form:input path="age" size="5" cssErrorClass="error" /><br> <input type="submit" value="送信"> </form:form>