java網上商城項目第1篇之使用者註冊模組_java

來源:互聯網
上載者:User

本文為大家講解了商城項目使用者註冊模組,供大家參考,先看看效果圖:

1.前台JS校正:

 事件觸發: onsubmit=”checkForm()”

2.使用AJAX完成非同步使用者名稱是否存在校正

①事件觸發:onblur=”checkUserName()”

②AJAX

function checkUsername(){ var username = $("#username").val(); $("#span1").load("${pageContext.request.contextPath}/user_checkUsername.action",{'username':username});} 

3.後台Struts2的資料校正

①.編寫表單中的<form action=”${ pageContext.request.contextPath }/user_regist.action”/>

②.在Action中編寫方法

③.完成資料校正:

在Action所在包下建立一個類名-方法對應訪問路徑-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="username"> <field-validator type="requiredstring">  <message>使用者名稱不可為空!</message> </field-validator> </field>  <!-- name:要校正的欄位名 --> <field name="password"> <field-validator type="requiredstring">  <message>密碼不可為空!</message> </field-validator> </field>   <!-- 校正郵箱 --> <field name="email"> <field-validator type="email">  <message>郵箱格式不正確!</message> </field-validator> </field>  <!-- 校正電話 --> <field name="phone"> <field-validator type="regex">  <param name="regex"><![CDATA[^15\d{9}$]]></param>  <message>電話不合法</message> </field-validator> </field></validators>

4.發送啟用郵件

① 引入兩個包: activation.jarmail.jar

②UserService.java

/** * 註冊使用者的方法 * @param user */public void save(User user) { // 儲存到資料庫: user.setState(0); // 0:未啟用 1:已經啟用 String code = UUIDUtils.getUUID()+UUIDUtils.getUUID(); 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"); // 1.獲得串連: Session session = Session.getInstance(props, new Authenticator() {  @Override protected PasswordAuthentication getPasswordAuthentication() {  return new PasswordAuthentication("service@shop.com", "111"); }   }); // 2.建立一個郵件的對象 Message message = new MimeMessage(session); // 設定寄件者: try { message.setFrom(new InternetAddress("service@shop.com")); // 設定收件者: message.setRecipient(RecipientType.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+"'>http://192.168.30.123:8080/itcastshop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8"); // 發送郵件: Transport.send(message); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); }}

5.使用者啟用

在郵箱中點擊串連提交到Action.
Action中接收啟用碼:
按照啟用碼查詢這個使用者:
* 如果查詢到了:
* 修改使用者狀態
* 如果沒有使用者:
* 啟用失敗:

/** * 使用者啟用的方法: */public String active() { // 模型驅動會接收啟用碼: // 按照啟用碼查詢使用者 : User existUser = userService.findByCode(user.getCode()); if (existUser == null) { // 啟用碼篡改 this.addActionMessage("啟用失敗:啟用碼被篡改了!"); } else { // 啟用:修改使用者狀態 existUser.setState(1); userService.update(existUser); // 啟用成功: this.addActionMessage("啟用成功:請去登入!"); } return "msg";}

 github完整代碼:https://github.com/ganchuanpu/itcastshop

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.