SSH online marketplace-user activation
In the previous blog, I briefly introduced how to implement the mail sending function in conjunction with the SSH online store project. After the mail is sent, the next step is activation. Why? Currently, most websites must activate their accounts before they can be successfully registered. This is a measure to prevent malicious registration. You only need to find the email sent from the registered website in the email address you entered during account registration, click the activation link to complete the activation. In the previous blog, the activation email has been sent, and the next step is to click the activation email in the mailbox to complete the activation and login. How can this function be implemented? In this blog today, I will briefly introduce how to activate this function for users and hope to help those who need it, please also advise me a lot. '(* tips _ tips *)′!
The first step is to compile a user activation method in the UserAction. java class. The Code is as follows:
/*** User activation method * author Ding Guohua */public String active () {// query the User by activation code: User existUser = userService. findByCode (user. getCode (); // determines if (existUser = null) {// the activation code is incorrect. this. addActionMessage ("activation failed: activation code error! ");} Else {// activation successful // modify the user's status existUser. setState (1); existUser. setCode (null); userService. update (existUser); this. addActionMessage ("activated successfully: Log On! ");} Return" msg ";}
Next, the second step is to compile methods in the UserService. java class and query users based on the activation code. The specific code is as follows:
Package cn. itcast. shop. user. service; import org. springframework. transaction. annotation. transactional; import cn. itcast. shop. user. dao. userDao; import cn. itcast. shop. user. vo. user; import cn. itcast. shop. utils. mailUitls; import cn. itcast. shop. utils. UUIDUtils;/*** User Module Business Layer Code * @ author Ding Guohua **/@ Transactionalpublic class UserService {// inject UserDaoprivate UserDao userDao; public void setUserDao (UserDao userDao) {this. userDao = userDao;} // Method for querying users by User name public User findByUsername (String username) {return userDao. findByUsername (username);} // The business layer completes the User registration code public void save (user User) {// saves the data to the database user. setState (0); // 0 indicates that the user is activated. 1 indicates that the user has activated String code = UUIDUtils. getUUID () + UUIDUtils. getUUID (); user. setCode (code); userDao. save (user); // send the activation email MailUitls. sendMail (user. getEmail (), code);} // The service layer queries the User's public User findByCode (String code) {return userDao Based on the activation code. findByCode (code);} // Method for modifying the User's status public void update (User existUser) {userDao. update (existUser );}}
Next, the third step is to compile the methods in the UserDao. java class. The specific code is as follows:
Package cn. itcast. shop. user. dao; import org. springframework. orm. hibernate3.support. hibernateDaoSupport; import java. util. list; import cn. itcast. shop. user. vo. user; /*** code of the User Module persistent layer * @ author Ding Guohua **/public class UserDao extends HibernateDaoSupport {// query by rank whether the User public User findByUsername (String username) exists) {String hql = "from User where username =? "; List
List = this. getHibernateTemplate (). find (hql, username); if (list! = Null & list. size ()> 0) {return list. get (0);} return null;} // The code for saving a registered User to the database implements public void save (user User user) {// TODO Auto-generated method stubthis. getHibernateTemplate (). save (user) ;}// query the User's public User findByCode (String code) {String hql = "from user where code =? "; List
List = this. getHibernateTemplate (). find (hql, code); if (list! = Null & list. size ()> 0) {return list. get (0) ;}return null ;}// Method for modifying the User State public void update (User existUser) {this. getHibernateTemplate (). update (existUser );}}
So far, the Code has been compiled. Let's take a look at the running effect. First, register a user, as shown in:
Click agree to the following agreement and register it. The following page is displayed:
Next, let's see if the mailbox of xiaobian has received the activation email, as shown in:
Click the activation link to see what miracle will happen, as shown in:
Small message: This blog post briefly introduces how to activate a user. In general, the idea is to click the activation link in the mailbox client and then query the user based on the activation code passed, if the user is not empty, we will modify the user's status. If the user is empty, it will prove that the activation code has been tampered with. The SSH online store is not complete yet ~~~~~~