In the previous blog, the small part of the main combination of SSH online mall This project, a simple introduction of how to implement the message sent by this function, the mail sent, the next is activated, for what? Now most of the site to be activated by the account, and then to register the success, this is to prevent vicious registration measures, as long as the registered account when the email address to find the registration site sent to the mail, and click on the activation link to complete the activation. In the previous blog, the activation of the message has been sent, the next thing to do is to click on the activation email inside the mailbox to complete the activation of the login, then how to achieve this feature? Today this blog, small series on the main simple to introduce how to implement the user activation of this function, I hope that the need for a small partner to help, but also please Daniel many advice Oh ' (*∩_∩*)!
First, we write a user-activated method in the Useraction.java class, as shown in the following code:
/** * User-activated method * Author Ding */public String Active () {//query user based on Activation code: User Existuser=userservice.findbycode (User.getcode ()); /judgment if (existuser==null) {//Activation Code Error This.addactionmessage ("Activation failed: Activation code Error!");} else{//activation succeeds//modifies the user's state existuser.setstate (1); Existuser.setcode (null); Userservice.update (Existuser); This.addactionmessage ("Activation succeeded: please go to login!"); Return "MSG";}
then, in the second step, we will write the method in the Userservice.java class, query the user according to 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 * */@Transactionalpublic class UserService {//inject userdaoprivate Userdao userdao;public void SetUser Dao (Userdao Userdao) {This.userdao =userdao;} Method of 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) {//data is stored in the database user.setstate (0);//0 on behalf of User Activation 1 means the user has activated string code = Uuidutils.getuuid () +uuidutils.getuuid (); User.setcode (code); userdao.save (user);//Send activation mail mailuitls.sendmail ( User.getemail (), code);} The business layer queries the user public Findbycode (String code) {return Userdao.findbycode (code) according to the activation code; }//Modify the user's state method public void Update (user existuser) {userdao.update (Existuser);}}
then, in the third step, we'll write the methods in the Userdao.java class, as shown in the following code:
package Cn.itcast.shop.user.dao;import Org.springframework.orm.hibernate3.support.hibernatedaosupport;import Java.util.list;import cn.itcast.shop.user.vo.user;/** * User Module Persistence layer code * @author Ding * */public class Userdao extends Hibernatedaosupport {//by rank query whether There is a user public, Findbyusername (string username) {String hql = "from user where username=?"; List <User> list=this.gethibernatetemplate (). Find (Hql,username), if (list!=null && list.size () > 0) { Return List.get (0);} return null;} Registered user Save Database Code implementation public void saved (user user) {//TODO auto-generated Method Stubthis.gethibernatetemplate (). Save (user); Query user public based on activation Code Findbycode (string code) {String hql = "from user where Code =?"; List<user> list=this.gethibernatetemplate (). Find (Hql,code), if (list!=null && list.size () > 0) { Return List.get (0);} return null;} Methods for modifying user state public void update (user existuser) {this.gethibernatetemplate (). Update (Existuser);}
at this point, our Code section has been written, we look at the effect of the operation, we first to register a user, as shown in:
Click Agree to the following agreement and register, the following interface appears:
Next, take a look at whether the small-numbered mailbox receives an activation message, as shown in:
Click on the activation link, and we'll look at what the miracle Nie looks like:
Small Series of words: The blog, the small part of the main simple introduction of how to activate the user, in general, the realization of the idea is to click the activation link in the mailbox client, and then according to the transfer activation code for the user query, if the user is not empty, we modify the user's status; If the user is empty, That proves that the activation code has been tampered with, ssh online shop, wonderful not to be continued ~~~~~~
SSH Online Store---user activation