This article explains the user registration module of the mall project for your reference. Let’s take a look at the renderings first:
1. Front desk JS verification:
Event trigger: onsubmit="checkForm()"
2. Use AJAX to complete the verification of whether the asynchronous username exists
① Event trigger: onblur="checkUserName()"
②AJAX
function checkUsername(){ var username = $("#username").val(); $("#span1").load("${pageContext.request.contextPath}/user_checkUsername.action",{'username':username});}3. Data verification of backend Struts2
①. Write <form action=”${ pageContext.request.contextPath }/user_regist.action”/> in the form
②.Writing methods in Action
③. Complete data verification:
Create a class name under the package where Action is located - the method corresponds to the 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-validator-1.0.3.dtd"><validators> <!-- name: field name to be verified--> <field name="username"> <field-validator type="requiredstring"> <message>Username cannot be empty!</message> </field-validator> </field> <!-- name: The field name to be verified --> <field name="password"> <field-validator type="requiredstring"> <message>Passage cannot be empty!</message> </field-validator> </field> <!-- Verify mailbox--> <field name="email"> <field-validator type="email"> <message>The mailbox format is incorrect!</message> </field-validator> </field> <!-- Verify phone--> <field name="phone"> <field-validator type="regex"> <param name="regex"><![CDATA[^15/d{9}$]]></param> <message>Illegal phone number</message> </field-validator> </field></validators>4. Send activation email
① Introduce two packages: activation.jarmail.jar
②UserService.java
/** * Method to register a user* @param user */public void save(User user) { // Save to database: user.setState(0); // 0: Not activated 1: String code = UUIDUtils.getUUID()+UUID(); user.setCode(code); userDao.save(user); // Send an activation email: MailUtils.sendMail(user.getEmail(), code);}③MailUtils
/** * Method of sending email: */public static void sendMail(String to,String code){ Properties props = new Properties(); props.setProperty("mail.smtp", "localhost"); // 1. Get the connection: Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]", "111"); } }); // 2. Create an object of the message Message message = new MimeMessage(session); // Set the sender: try { message.setFrom(new InternetAddress("[email protected]")); // Set the recipient: message.setRecipient(RecipientType.TO, new InternetAddress(to)); // Set the subject: message.setSubject(" from ITCASTSHOP mall activation email"); // Set the email body: message.setContent("<h1> Activation email from ITCASTSHOP shopping paradise</h1><h3><a href='http://192.168.30.123:8080/itcastshop/user_active.action?code="+code+"'>http://192.168.30.123:8080/itcastshop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8"); // Send email: Transport.send(message); } catch (AddressException e) { e.printStackTrace(); }}5. User activation
Click Connect in the email address to submit to Action.
Receive activation code in Action:
Follow the activation code to query this user:
* If query:
* Modify user status
* If there is no user:
* Activation failed:
/** * Method for user activation: */public String active() { // The model driver will receive the activation code: // Query the user according to the activation code: User existUser = userService.findByCode(user.getCode()); if (existUser == null) { // The activation code tampers with this.addActionMessage("Activation failed: the activation code was tampered with!"); } else { // Activation: Modify the user status existUser.setState(1); userService.update(existUser); // Activation successfully: this.addActionMessage("Activation successful: please log in!"); } return "msg";}github full code: https://github.com/ganchuanpu/itcastshop
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.