この記事では、参照のためにモールプロジェクトのユーザー登録モジュールについて説明します。最初にレンダリングを見てみましょう:
1。フロントデスクJS検証:
イベントトリガー:onsubmit = "checkform()"
2。Ajaxを使用して、非同期ユーザー名が存在するかどうかの検証を完了します
①イベントトリガー:onblur = "checkusername()"
②ajax
function checkusername(){var username = $( "#username")。val(); $( "#span1")3。バックエンドStruts2のデータ検証2
①。 <form action =” $ {pagecontext.request.contextpath}/user_regist.action”/> form
②。作用中の書き込み方法
③。完全なデータ検証:
アクションが配置されているパッケージの下にクラス名を作成します - メソッドはアクセスパス-validation.xmlに対応します
useraction-user_regist-validation.xml
<?xml version = "1.0" encoding = "utf-8"?> <!doctype validators public " - // apache struts // xwork validator 1.0.3 // en" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd" name = "username"> <field-validator type = "rebulystring"> <message> usernameは空にすることはできません!</message> </field-validator> </field> <! - name:field name name fide field name = "password =" password "> <field-validator typen <field name = "email"> <field-validator type = "email"> <message>メールボックス形式は間違っています!</message> </field-validator> </field> <! - verify hone-> <field-validator type = "regex"> <param name = "regex"> < <メッセージ>違法な電話番号</message> </field-validator> </field> </validators>
4.アクティベーションメールを送信します
activation.jarmail.jarの2つのパッケージを紹介します
②userservice.java
/***ユーザーを登録する方法* @paramユーザー*/public void save(user user){//データベースに保存:user.setState(0); // 0:アクティブ化されていない1:文字列code = uuidutils.getuuid()+uuid(); user.setCode(code); userdao.save(user); //アクティベーションの送信メール:Mailutils.sendmail(user.getEmail()、code);}mailutils
/** *電子メールの送信方法: */public static void sendmail(string to、string code){properties props = new Properties(); props.setProperty( "mail.smtp"、 "localhost"); //接続を取得:セッションセッション= session.getInstance(props、new Authenticator(){@Override Protected ApsadrowAuthentication getPassWordAuthentication(){return new PasswordAuthentication( "[email protected]"、 "111");}}); // 2。メッセージメッセージのオブジェクトを作成しますメッセージ= new Mimemessage(session); //送信者の設定:try {message.setfrom(new InternetAddress( "[email protected]")); //受信者を設定します:message.setRecipient(ReciontientType.to、new InternetAddress(to)); //件名の設定:message.setsubject( "itcastshopモールのアクティベーションメール"から "); //電子メールを設定する:message.setContent( "<h1> itcastshopショッピングパラダイスからのアクティベーションメール</h1> <h3> <a href = 'http://192.168.30.123:8080/itcastshop/user_active.action?code = "+code+"'> "text/html; charset = utf-8"); //電子メールの送信:Transport.send(メッセージ); } catch(adstressException e){e.printstacktrace(); }}5。ユーザーのアクティベーション
[電子メールアドレスの接続]をクリックして、アクションに送信します。
アクティベーションコードをアクティブ化します:
アクティベーションコードに従って、このユーザーを照会します。
*クエリの場合:
*ユーザーステータスを変更します
*ユーザーがいない場合:
*アクティベーションが失敗しました:
/**ユーザーアクティベーションのメソッド: */public String Active(){//モデルドライバーはアクティベーションコードを受信します://アクティベーションコードに従ってユーザーをクエリ:user esceduser = userservice.findbycode(user.getCode()); if(expecuser == null){//アクティベーションコードはthis.addactionmessage( "アクティベーション障害:アクティベーションコードが改ざんされた!"); } else {// activation:ユーザーステータスexpecuser.setState(1); userservice.update(expecuser); // Activation sucessに:this.addactionMessage( "Activation custers:login!"); } "msg";}を返しますgithubフルコード:https://github.com/ganchuanpu/itcastshop
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。