Spring IOCテクノロジーを使用して、ユーザーログインの検証メカニズムを実現し、ユーザーのログインを検証します。
まず、Springの自動アセンブリモードを使用して、ユーザーオブジェクトをコントローラーに挿入し、システムで指定されたリーガルユーザーのユーザー名とパスワードをユーザーが入力したユーザー名とパスワードを一致させます。
ユーザー名とパスワードが正常に一致したら、ログインの成功ページにジャンプします。ユーザー名とパスワードが一致しない場合は、ログインが失敗したページにジャンプします。
1.ユーザーオブジェクトを作成し、ユーザー名とパスワード属性を定義します。コードは次のとおりです。
パッケージcom.importnew; public class user {private string username;プライベート文字列パスワード。 public string getUsername(){return username; } public void setUsername(string username){this.username = username; } public string getPassWord(){パスワードを返します。 } public void setPassword(string password){this.password = password; }}2。コントローラーTestutilを作成し、ユーザーオブジェクトを挿入してログインして確認します。コードは次のとおりです。
パッケージcom.importnew; Import javax.servlet.http.httpservletrequest; Import javax.servlet.http.httpservletresponse; import org.springframework.web.servlet.modelandview; Import org.springframework.wab.sput.mvc.mvc.mvc.mvc.mvc.mvc.mvc.mvc.mvc.mvc.mvc.mvc.mvc.mvc.mvc.mvc.springconwork. AbstractController {プライベートユーザー; public user getUser(){return user; } public void setuser(user user){this.user = user; } @Override Protected ModelandView HandLeRequestInternal(httpservletrequest arg0、httpservletresponse arg1)スロー例外{string username = arg0.getParameter( "username");文字列パスワード= arg0.getParameter( "password"); if(username.equals(user.getusername())&& password.equals(user.getPassWord())){return new ModelandView( "yes"); } else {return new ModelandView( "error"); }}}3. Springの構成ファイルApplicationContext.xmlのユーザーオブジェクトのプロパティに値を割り当て、自動アセンブリを使用してユーザーオブジェクトをコントローラーTestutilに挿入します。コードは次のとおりです。
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns:context = "http://www.springframework.org/schema/context" xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:aop = " xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.x.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <bean id =" ViewResolver "> <Propetities name ="> </propiciz <value> .jsp </value> </property> </bean> <bean id = "user"> <property name = "username"> <value> admin </value> </property> <property name = "password"> <balue> 123 </value> </property> </bean> <bean autowire = "byname" id = "> <of"> "> <fime"> "> <bean" </bean> </bean>
4。web.xmlファイルでapplicationcontext.xmlの自動読み込みを構成します。プロジェクトが開始されると、プログラムは構成ファイルに情報を自動的にロードします。コードは次のとおりです。
<!doctype web-app public " - // Sun Microsystems、Inc .//dtd Webアプリケーション2.3 // en" "http://java.sun.com/dtd/web-app> <display-name> Archytype作成されたWebアプリケーション</Displet-Name> <servlet> <servlet> <servlet> <Servlet-Class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <init-param> <param-name> contextconfiglocation </param-name> <param-value>/applicationContext.xml </parm-value> </init-servtup> </load-mltup> <Servlet-Name> DispatcherServlet </servlet-name> <url-pattern>*
述べる:
Testutilに継承されたクラスAbstractControllerには、Spring-WEB-MVCのJARパッケージサポートが必要です。
////終わり
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。