SpringMVC form標籤、伺服器表單驗證、錯誤資訊回顯
阿新 • • 發佈:2019-01-02
form標籤
應用場景:方便伺服器資料在form表單上的展示
使用方式:1.引入標籤庫 2.建立表單
例如:建立兩個實體類
@[email protected]@ToString public class User { private String username; private Integer age; private Integer gender; private String[] hobby; private Pet pet; } @[email protected]@ToString public class Pet { private String name; private Integer id; }
假設在伺服器獲取的資料如下:
@RequestMapping("/update/{id}") public String update(@PathVariable Integer id, Model model){ ArrayList<Object> arrayList = new ArrayList<>(); arrayList.add("籃球"); arrayList.add("乒乓球"); arrayList.add("足球"); model.addAttribute("allhobbies",arrayList); ArrayList<Object> petList = new ArrayList<>(); Pet pet1 = new Pet(); pet1.setName("狗"); pet1.setId(1); Pet pet2 = new Pet(); pet2.setName("貓"); pet2.setId(2); Pet pet3 = new Pet(); pet3.setName("蛇"); pet3.setId(3); petList.add(pet1); petList.add(pet2); petList.add(pet3); model.addAttribute("petlist",petList); System.out.println(id); User user = new User(); user.setUsername("aaa"); user.setAge(23); user.setGender(0); String[] hobby = new String[]{"籃球","足球"}; user.setHobby(hobby); user.setPet(pet3); model.addAttribute("user",user); return "/result.jsp"; }
form表單的書寫(注意引入標籤庫):
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib uri="http://www.springframework.org/tags/form" prefix="fm"%> <html> <head> <title>Title</title> </head> <body> <h1>result</h1> <fm:form modelAttribute="user" action="${pageContext.request.contextPath}/update2"> 姓名:<fm:input path="username"/> 年齡:<fm:input path="age"/> <!--單選框--> 性別:<fm:radiobutton path="gender" value="0" label="男"/> <fm:radiobutton path="gender" value="1" label="女"/><br/> <!--複選框--> 愛好:<fm:checkboxes path="hobby" items="${allhobbies}"/><br/> <!--下拉列表--> 寵物:<fm:select path="pet.id" items="${petlist}" itemValue="id" itemLabel="name"/> <input type="submit" value="修改"> </fm:form> </body> </html>
展示截圖如下:
注意點:form標籤的modelAttribute一般要寫model中傳過來的key值,不寫就會報錯,因為預設的key值是command,如果form表單中沒寫key值,那麼model中的key值就得設定為command
伺服器表單驗證及回顯
第一步:匯入相關jar包
https://download.csdn.net/download/weixin_43014205/10889254
第二步:spring的配置檔案中配置相關注解 <mvc:annotation-driven/>
第三步:在實體類上標註檢驗條件及錯誤回顯資訊
@[email protected]@ToString
public class User {
@NotBlank(message = "姓名不能為空")
private String username;
@NotNull
@Max(value = 200,message = "年齡非法")
private Integer age;
private Integer gender;
private String[] hobby;
private Pet pet;
}
第四步:在接受引數的requestmapping中設定@valid,並繫結錯誤資訊BindingResult
@RequestMapping("/update2")
public String update2(@Valid User user, BindingResult result,Model model){
//判斷有無錯誤資訊
if (result.getErrorCount() != 0){
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add("籃球");
arrayList.add("乒乓球");
arrayList.add("足球");
model.addAttribute("allhobbies",arrayList);
ArrayList<Object> petList = new ArrayList<>();
Pet pet1 = new Pet();
pet1.setName("狗");
pet1.setId(1);
Pet pet2 = new Pet();
pet2.setName("貓");
pet2.setId(2);
Pet pet3 = new Pet();
pet3.setName("蛇");
pet3.setId(3);
petList.add(pet1);
petList.add(pet2);
petList.add(pet3);
model.addAttribute("petlist",petList);
//回到原來介面
return "/result.jsp";
}
return "/result2.jsp";
}
第五步:錯誤資訊回顯
<fm:form modelAttribute="user" action="${pageContext.request.contextPath}/update2">
姓名:<fm:input path="username"/> <fm:errors path="username" cssStyle="color: red"/>
年齡:<fm:input path="age"/> <fm:errors path="age" cssStyle="color: blue"/>
性別:<fm:radiobutton path="gender" value="0" label="男"/>
<fm:radiobutton path="gender" value="1" label="女"/><br/>
愛好:<fm:checkboxes path="hobby" items="${allhobbies}"/><br/>
寵物:<fm:select path="pet.id" items="${petlist}" itemValue="id" itemLabel="name"/>
<input type="submit" value="修改">
</fm:form>
將名字改為空 年齡改為200以上