This article for everyone to explain the Mall project user Registration module, for your reference, first look at the effect map:
1. Front Desk JS check:
Event Trigger: onsubmit= "Checkform ()"
2. Use Ajax to complete the existence of an asynchronous user name checksum
① Event Trigger:onblur= "Checkusername ()"
②ajax
function Checkusername () {
var username = $ ("#username"). Val ();
$ ("#span1"). Load ("${pagecontext.request.contextpath}/user_checkusername.action", {' username ': username});
3. Background STRUTS2 Data Verification
①. Write the <form action= "${pageContext.request.contextPath}/user_regist.action" in the form/>
②. Write a method in action
③. Complete data validation:
Create a class name under the action package-method corresponding access path-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-validato R-1.0.3.dtd "> <validators> <!--Name: Field name to validate--> <field name=" username "> <field-validator type = "Requiredstring" > <message> user name cannot be empty!</message> </field-validator> </field> <!--name: Field name to validate--> <field name= "password" > <field-validator type= "requiredstring" > <message> password cannot be empty! </message> </field-validator> </field> <!--check mailbox--> <field name= "email" > <field -validator type= "Email" > <message> mailbox format is not correct!</message> </field-validator> </field> <!-- Check phone--> <field name= "Phone" > <field-validator type= "regex" > <param name= "regex" ><! [cdata[^15\d{9}$]]></param> <message> Telephone not valid </message> </field-validator> </fielD> </validators>
4. Send Activation message
① introduced two packages: Activation.jarmail.jar
②userservice.java
/**
* Registration User's method
* @param user/public
void Save (user user) {
///Save to Database:
user.setstate (0);//0: Inactive 1:
String code = UUIDUTILS.GETUUID () +uuidutils.getuuid () has been activated;
User.setcode (code);
Userdao.save (user);
Send an Activation message:
mailutils.sendmail (User.getemail (), code);
③mailutils
/** * Send mail method: * * public static void SendMail (String to,string code) {Properties Props = new properties ();
Props.setproperty ("mail.smtp", "localhost"); 1. Get Connection: Session session = Session.getinstance (props, New Authenticator () {@Override protected passwordauthenticat
Ion Getpasswordauthentication () {Return to New Passwordauthentication ("Service@shop.com", "111");
}
});
2. Create an object message messages = new MimeMessage (session);
Set Sender: try {message.setfrom (new internetaddress ("service@shop.com"));
Set recipient: Message.setrecipient (recipienttype.to, New internetaddress (to));
Set theme: Message.setsubject ("from Itcastshop Mall to activate Mail"); To set the message body: message.setcontent ("
5. User activation
Click the link in the mailbox to submit to action.
Receive activation code in action:
Follow the activation code to query this user:
* If the query is:
* Modify User Status
* If no users:
* Activation failed:
/**
* User-activated method: */Public
String Active () {
//model driver will receive activation code:
//query user by Activation code:
Existuser = Userservice.findbycode (User.getcode ());
if (Existuser = = null) {
//Activation Code tampering
this.addactionmessage ("Activation failed: Activation code tampered!");
} else {
//activation: Modifying user status
existuser.setstate (1);
Userservice.update (existuser);
Activation succeeded:
this.addactionmessage ("Activation successful: Please login!");
}
Return "MSG";
GitHub Complete code: Https://github.com/ganchuanpu/itcastshop
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.