ใช้เทคโนโลยี Spring IOC เพื่อรับรู้กลไกการตรวจสอบสำหรับการเข้าสู่ระบบของผู้ใช้และตรวจสอบการเข้าสู่ระบบของผู้ใช้
ขั้นแรกให้ใช้โหมดแอสเซมบลีอัตโนมัติของสปริงเพื่อฉีดวัตถุผู้ใช้ลงในคอนโทรลเลอร์จากนั้นจับคู่ชื่อผู้ใช้และรหัสผ่านที่ผู้ใช้ป้อนด้วยชื่อผู้ใช้และรหัสผ่านของผู้ใช้ตามกฎหมายที่ระบุไว้ในระบบ
เมื่อชื่อผู้ใช้และรหัสผ่านจับคู่ได้สำเร็จให้ข้ามไปที่หน้าเข้าสู่ระบบที่ประสบความสำเร็จ เมื่อชื่อผู้ใช้และรหัสผ่านไม่ตรงกันให้ข้ามไปที่หน้าเว็บที่การเข้าสู่ระบบล้มเหลว
1. สร้างวัตถุผู้ใช้กำหนดชื่อผู้ใช้และแอตทริบิวต์รหัสผ่านรหัสมีดังนี้:
แพ็คเกจ com.importNew; ผู้ใช้ระดับสาธารณะ {ชื่อผู้ใช้สตริงส่วนตัว; รหัสผ่านสตริงส่วนตัว สตริงสาธารณะ getUserName () {ส่งคืนชื่อผู้ใช้; } โมฆะสาธารณะ setUserName (ชื่อผู้ใช้สตริง) {this.userName = ชื่อผู้ใช้; } สตริงสาธารณะ getPassword () {ส่งคืนรหัสผ่าน; } โมฆะสาธารณะ setPassword (รหัสผ่านสตริง) {this.password = รหัสผ่าน; -2. สร้างตัวควบคุมการทดสอบวัตถุผู้ใช้และเข้าสู่ระบบเพื่อตรวจสอบ รหัสมีดังนี้:
แพ็คเกจ com.importnew; นำเข้า Javax.servlet.http.httpservletrequest นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า org.springframework.web.servlet.modelandview; AbstractController {ผู้ใช้ส่วนตัว; ผู้ใช้สาธารณะ getUser () {return user; } โมฆะสาธารณะ setUser (ผู้ใช้ผู้ใช้) {this.user = ผู้ใช้; } @Override Model และ View HandlereQuestInternal (httpservletRequest arg0, httpservletResponse arg1) โยนข้อยกเว้น {สตริงชื่อผู้ใช้ = arg0.getParameter ("ชื่อผู้ใช้"); สตริงรหัสผ่าน = arg0.getParameter ("รหัสผ่าน"); if (username.equals (user.getUserName ()) && password.equals (user.getPassword ())) {ส่งคืน modelandView ใหม่ ("ใช่"); } else {ส่งคืน modelandview ใหม่ ("ข้อผิดพลาด"); -3. กำหนดค่าให้กับคุณสมบัติของวัตถุผู้ใช้ในไฟล์กำหนดค่าของ Spring ApplicationContext.xml และฉีดวัตถุผู้ใช้ลงในตัวควบคุมการทดสอบโดยใช้แอสเซมบลีอัตโนมัติ รหัสมีดังนี้:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: aop = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <bean id =" viewresolver "> <value> .JSP </alues> </property> </ebean> <Bean ID = "ผู้ใช้"> <property name = "ชื่อผู้ใช้"> <value> admin </value> </property> <property name = "รหัสผ่าน"> <value> 123 </alue> </property> </ebean> </ebeans>
4. กำหนดค่าการโหลดอัตโนมัติของ ApplicationContext.xml ในไฟล์ web.xml เมื่อโครงการเริ่มต้นโปรแกรมจะโหลดข้อมูลโดยอัตโนมัติในไฟล์การกำหนดค่า รหัสมีดังนี้:
<! doctype web-app public "-// Sun Microsystems, Inc.//dtd Web Application 2.3 // en" "http://java.sun.com/dtd/web-app_2_3 <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <init-param> <param-name> contextconfiglocation </param-name> <param-value>/ApplicationContext.xml </param-value> <servlet-mapping> <servlet-name> dispatcherServlet </servlet-name> <url-pattern>*. ทำ </url-pattern> </servlet-mapping> </eb-app>
คำพูด:
คลาส AbstractController ที่ได้รับการสืบทอดใน TestUtil ต้องการการสนับสนุนแพ็คเกจ JAR สำหรับ Spring-Web-MVC
////จบ
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น