เมื่อใช้ initbinder สำหรับการตรวจสอบข้อมูลบางส่วนที่ส่งในคอนโทรลเลอร์นี้จำเป็นต้องมีลักษณะทางธุรกิจนั่นคือมันจะถูกใช้ในสถานการณ์การตรวจสอบที่ค่อนข้างซับซ้อนเท่านั้น การตรวจสอบรูปแบบที่ง่ายที่สุดสามารถแก้ไขได้โดยใช้การตรวจสอบคำอธิบายประกอบ
สำหรับการใช้การตรวจสอบคำอธิบายประกอบโปรดดูที่: http://www.vevb.com/article/136448.htm
สิ่งหนึ่งที่ควรทราบที่นี่: initbinder และคำอธิบายประกอบสามารถเลือกหนึ่งในสองการตรวจสอบเท่านั้น หากใช้ initbinder ไม่สามารถใช้การตรวจสอบคำอธิบายประกอบได้
การกำหนดค่าก่อนหน้าของ web.xml และ spring.xml จะไม่ถูกทำซ้ำโปรดดูการกำหนดค่าในลิงค์ด้านบน เหมือนกันทุกประการ
อัปโหลดรหัสโดยตรง:
1. คลาสเอนทิตีรุ่น USER5
แพ็คเกจ com.my.controller.bean; นำเข้า java.util.date; ผู้ใช้ระดับสาธารณะ user5 {ID ยาวส่วนตัว; ชื่อสตริงส่วนตัว; รหัสผ่านสตริงส่วนตัว วันที่ส่วนตัว createTime; อายุ int ส่วนตัว; Public Long getId () {return id; } โมฆะสาธารณะ setId (Long id) {this.id = id; } สตริงสาธารณะ getName () {ชื่อคืน; } โมฆะสาธารณะ setName (ชื่อสตริง) {this.name = name; } สตริงสาธารณะ getPassword () {ส่งคืนรหัสผ่าน; } โมฆะสาธารณะ setPassword (รหัสผ่านสตริง) {this.password = รหัสผ่าน; } วันที่สาธารณะ getCreateTime () {return createTime; } โมฆะสาธารณะ setCreateTime (วันที่ createTime) {this.createTime = createTime; } public int getage () {return Age; } การตั้งค่าโมฆะสาธารณะ (อายุ int) {this.age = อายุ; -2. เพิ่มตัวตรวจสอบใหม่:
แพ็คเกจ com.my.controller.validator; นำเข้า org.springframework.stereotype.component; นำเข้า org.springframework.validation.errors; นำเข้า org.springframework.validation.validationutils; com.my.controller.bean.user5; @componentpublic class testValidator ใช้ตัวตรวจสอบ {@Override Public Boolean รองรับ (คลาส <s?> ParamClass) {return user5.class.equals (paramclass); } @Override void public Void (Object OBJ, ข้อผิดพลาดข้อผิดพลาด) {user5 user = (user5) obj; Validationutils.rejectifeMpyorWhitespace (ข้อผิดพลาด "ชื่อ", "ust.name", null, ""); if (user.getage () <18) {errors.rejectValue ("อายุ", "ust.agemin", วัตถุใหม่ [] {"อายุ", 18}, "อายุไม่น้อยกว่า {1} ปี"); -@component จำเป็นต้องเพิ่มที่นี่และ DI ถูกฉีด
3. คอนโทรลเลอร์
แพ็คเกจ com.my.controller; นำเข้า java.util.linkedhashmap นำเข้า java.util.list; นำเข้า java.util.map นำเข้า Javax.validation.valid; นำเข้า org.springframework.beans.factory.annotation.autowired; org.springframework.stereotype.controller; นำเข้า org.springframework.validation.bindingresult; นำเข้า org.springframework.validation.fielderror; นำเข้า org.springframework.validation.validator; org.springframework.web.bind.annotation.initbinder; นำเข้า org.springframework.web.bind.annotation.modelattribute; นำเข้า org.springframework.web.bind.annotation.Requestmapping; org.springframework.web.servlet.modelandview; นำเข้า com.my.controller.bean.user5; @controller @requestmapping (value = "binder") คลาสสาธารณะ testinitbindercontroller {@autowired @qualifier @InitBinder เป็นโมฆะส่วนตัว initbinder (WebDatabinder Binder) {Binder.SetValidator (ตัวตรวจสอบ); } @RequestMapping (method = requestMethod.get) ดัชนีสตริงสาธารณะ () {return "/testinitBinder/index"; } @RequestMapping (value = "เพิ่ม", method = requestMethod.post) โมเดลสาธารณะและวิววิวเพิ่ม (@modelattribute @valid user5 ผู้ใช้, ผล bindingResult) {modelandView view = modelandView ใหม่ ("testinitBinder/index"); view.addobject ("ผู้ใช้", ผู้ใช้); if (result.haserrors ()) {list <fielderror> errs = result.getFielderRors (); แผนที่ <สตริง, สตริง> mapErrors = ใหม่ linkedHashMap <สตริง, สตริง> (); สำหรับ (fielderror err: errs) {system.out.println ("objectName:" + err.getObjectName () + "/tfieldName:" + err.getField () + "/tfieldValue:" err.getRejectedValue () + "/tmessage:" mapErrors.put (err.getField (), err.getDefaultMessage ()); view.addobject ("ข้อผิดพลาด", maperrors); } return view; } return view; -ฉีด Validator ลงในคอนโทรลเลอร์
ในความเป็นจริงการใช้ initbinder วิธี err.getDefaultMessage () ในคอนโทรลเลอร์เพิ่มไม่สามารถรับข้อความที่ถูกต้องที่สอดคล้องกันได้ คุณสามารถดูผลลัพธ์การพิมพ์อินพุตสุดท้าย
4. ดู
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core uri = "http://java.sun.com/jsp/jstl/fmt" คำนำหน้า = "fmt"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "fn" uri = "http://www.springframework.org/tags"%> <%@ taglib คำนำหน้า = "ฟอร์ม" uri = "http://www.springframework.org/tags/form"%> <! "http://www.w3.org/tr/html4/loose.dtd"><html> <head> <meta http-equiv =" content-ype "content =" text/html; charset = utf-8 " action = "/testspringMvc1/binder/add" method = "post" modelattribute = "user5"> ชื่อผู้ใช้: <อินพุตประเภท = "text" id = "name" name = "name" value = "$ {user.name}"/> <br/> รหัสผ่าน: type = "text" id = "อายุ" name = "อายุ" value = "$ {user.age}"/> <br/> <อินพุต type = "ส่ง" value = "เพิ่ม"/> <hr/> ข้อผิดพลาด: <br/>โปรดทราบว่าคุณสามารถใช้ <ฟอร์ม: ข้อผิดพลาด/> เพื่อรับข้อมูลข้อผิดพลาดและสิ่งนี้ <ฟอร์ม: ข้อผิดพลาด/> ต้องอยู่ใน <ฟอร์ม: ฟอร์ม/>
5. การทดสอบผลลัพธ์
คลิกปุ่มเพิ่ม:
พิมพ์:
คุณจะเห็นว่าข้อมูลที่ถูกต้องของข้อผิดพลาดไม่สามารถรับได้ที่นี่
ในความเป็นจริงเมื่อข้อมูลที่ส่งในหน้าแบบฟอร์มที่ซับซ้อนมากมีตรรกะทางธุรกิจบางอย่างเริ่มต้นไม่ควรใช้มากนักเพราะหลายครั้งที่เราสามารถใช้แผนที่เพื่อแทรกข้อผิดพลาดลงไปและอ่านบนหน้า ตัวอย่างเช่น:
แผนที่ <สตริงสตริง> ข้อผิดพลาด;
Errors.add ("ชื่อ", "ชื่อผู้ใช้ไม่สามารถว่างเปล่า!");
-
-
คุณต้องใช้เท่านั้น:
<span style = "color: red;"> $ {errors.name} <span>แค่ทำ
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น