استخدم تقنية IOC الربيع لتحقيق آلية التحقق لتسجيل الدخول إلى المستخدم والتحقق من تسجيل الدخول للمستخدمين.
أولاً ، استخدم وضع التجميع التلقائي الخاص بـ Spring لحقن كائن المستخدم في وحدة التحكم ، ثم تطابق اسم المستخدم وكلمة المرور التي أدخلها المستخدم مع اسم المستخدم وكلمة المرور للمستخدم القانوني المحدد في النظام.
عندما يتطابق اسم المستخدم وكلمة المرور بنجاح ، القفز إلى صفحة تسجيل الدخول الناجحة ؛ عندما لا يتطابق اسم المستخدم وكلمة المرور ، اقفز إلى الصفحة التي فشل فيها تسجيل الدخول.
1. إنشاء كائن مستخدم ، وتحديد اسم المستخدم وسمات كلمة المرور ، والرمز كما يلي:
حزمة com.importNew ؛ مستخدم الفئة العامة {private string username ؛ كلمة مرور السلسلة الخاصة ؛ السلسلة العامة getUserName () {return username ؛ } public void setusername (string username) {this.userName = username ؛ } السلسلة العامة getPassword () {return password ؛ } public void setPassword (سلسلة كلمة مرور) {this.password = password ؛ }}2. قم بإنشاء وحدة تحكم TestUtil ، حقن كائن المستخدم وقم بتسجيل الدخول للتحقق. الرمز كما يلي:
حزمة com.importnew ؛ استيراد javax.servlet.http.httpservletrequest AbstractController {user user user ؛ المستخدم العام getUser () {return user ؛ } public void setUser (user user) {this.user = user ؛ } Override ModelandView HandleRequestinternal (httpservletrequest arg0 ، httpservletresponse arg1) يلقي الاستثناء {String username = arg0.getParameter ("username") ؛ سلسلة كلمة مرور = arg0.getParameter ("كلمة المرور") ؛ if (username.equals (user.getUserName ()) && password.equals (user.getPassword ())) {return new ModelAndView ("yes") ؛ } آخر {return new ModelAndView ("error") ؛ }}}3. قم بتعيين القيم لخصائص كائن المستخدم في Application Configuration File ApplicationContext.xml ، وحقن كائن المستخدم في TestUtil وحدة التحكم باستخدام التجميع التلقائي. الرمز كما يلي:
<؟ xmlns: context = "http://www.springframework.org/schema/context" xmlns: tx = "http://www.springframework.org/schema/tx 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"> <property name="prefix"> <value>/</value> </property> <property name="suffix"> <Value> .jsp </value> </sprention> </bou> <bean id = "user"> <property name = "username"> <suale> admin </value> </sopperation> <property name = "password"> <value> 123 </value> </propert </bean> </bans>
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 <Srevlet-Class> org.springframework.web.servlet.dispatcherservlet </isherlet-class> <Init-param> <param-name> contextConfiglocation </param-name> <Param-value> /applicationContext.xml </param-value> <Srevlet-mapping> <Sradlet-Name> Dispatcherservlet </revlet-name> <url-pattern>*.
ملاحظة:
يتطلب Class AbstractController الموروثة في TestUtil دعم حزمة JAR لـ Spring-Web-MVC.
////نهاية
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.