日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
淺談Struts2驗(yàn)證框架及用戶注冊(cè)

Struts2驗(yàn)證比起Struts1驗(yàn)證框架來(lái),好用多了,使程序更加清晰易讀,充分利用了配置文件的作用,也算是解耦的表現(xiàn)吧.

核心代碼如下:

1.用戶注冊(cè)頁(yè)面register.jsp

  

    
     

    < ?xml:namespace prefix = s />< s:fielderror>< /s:fielderror>
    < /FONT>
   

    < !-- 讀取顯示提示信息 -->
    < TABLE>
    
     
       用戶名:
     
     
      
     
    
    
     
       密碼:
     
     
       < INPUT type=password name=user.password>
     
    

    
     
       確認(rèn)密碼:
     
     
       < INPUT type=password name=user.rePassword>
     
    

    


     
       年齡:
     
     
       < INPUT name=user.age>
     
    
    
     
       生日:
     
     
       < INPUT name=user.birthday>
     
    
    
     
      
     
    
   


  

2.注冊(cè)成功歡迎頁(yè)面welcome.jsp

congratulations!${user.userName} 

3.注冊(cè)處理action RegisterAction

package org.kingtoon.action;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.kingtoon.bean.User;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

private User user;
@Override
public String execute() throws Exception {
   if(!(user.getPassword().equals(user.getRePassword()))){
      this.addFieldError("password", "請(qǐng)輸入相同的密碼");
      return "input";
   }
   else
   {
    HttpServletRequest request = ServletActionContext.getRequest ();
    request.setAttribute("user", user);
    return SUCCESS;
   }
 
}
public User getUser() {
   return user;
}
public void setUser(User user) {
   this.user = user;
}
}

4. 用戶Bean User.java

package org.kingtoon.bean;

import java.util.Date;

public class User {

private String userName;
private String password;
private String rePassword;
private Integer age;
private Date birthday;
public Integer getAge() {
   return age;
}
public void setAge(Integer age) {
   this.age = age;
}
public Date getBirthday() {
   return birthday;
}
public void setBirthday(Date birthday) {
   this.birthday = birthday;
}
public String getPassword() {
   return password;
}
public void setPassword(String password) {
   this.password = password;
}
public String getRePassword() {
   return rePassword;
}
public void setRePassword(String rePassword) {
   this.rePassword = rePassword;
}
public String getUserName() {
   return userName;
}
public void setUserName(String userName) {
   this.userName = userName;
}

}

5.配置驗(yàn)證文件RegisterAction-validation.xml

< VALIDATORS>

   < !-- 驗(yàn)證字符串不能為空 -->
   < FIELD-VALIDATOR type="requiredstring">
    < !-- 去空格 -->
    < PARAM name="trim">true
    < !-- 錯(cuò)誤提示信息 -->
    < MESSAGE>用戶名不能為空
   < /FIELD-VALIDATOR>
 
   < !-- 驗(yàn)證字符串長(zhǎng)度 -->
   < FIELD-VALIDATOR type="stringlength">
    < PARAM name="minLength">2< /PARAM>
    20
    < MESSAGE>用戶名長(zhǎng)度應(yīng)在2到18個(gè)字符間
  


   < FIELD-VALIDATOR type="requiredstring">
    true
    < MESSAGE>密碼不能為空
   < /FIELD-VALIDATOR>
 
   < FIELD-VALIDATOR type="stringlength">
    < PARAM name="minLength">6< /PARAM>
    18
    密碼長(zhǎng)度應(yīng)在6到18個(gè)字符之間< /MESSAGE>
   < /FIELD-VALIDATOR>
< /FIELD>


   < FIELD-VALIDATOR type="int">
    < PARAM name="min">1
    < PARAM name="max">150
    < MESSAGE>年齡應(yīng)在1到150之間
   < /FIELD-VALIDATOR>

< !-- 驗(yàn)證字符串為日期類型 -->

   < FIELD-VALIDATOR type="date">
    < PARAM name="min">1900-01-01
    < PARAM name="max">2008-10-16< /PARAM>
    < MESSAGE>出生日期應(yīng)在1900-01-01到2008-10-16< /MESSAGE>
  
< /FIELD>
< /VALIDATORS>

6.struts2框架默認(rèn)加載的配置文件struts.xml

< STRUTS>
    < CONSTANT name="struts.custom.i18n.resources" value="messageResource">< /CONSTANT>
  
   
   
       < RESULT name="success">/welcome.jsp< /RESULT>
       < RESULT name="input">/register.jsp< /RESULT>
    < /ACTION>
    < /PACKAGE>
< /STRUTS>

7.web服務(wù)器啟動(dòng)時(shí)加載Struts 配置文件 web.xml

< ?XML:NAMESPACE PREFIX = [default] < web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

< filter>
   < filter-name>struts-cleanup
  
    org.apache.struts2.dispatcher.ActionContextCleanUp
  

< filter>
   < filter-name>struts2
  
    org.apache.struts2.dispatcher.FilterDispatcher
  

< filter-mapping>
   < filter-name>struts-cleanup< /filter-name>
   /*

< filter-mapping>
   < filter-name>struts2< /filter-name>
   < url-pattern>/*< /url-pattern>

< welcome-file-list>
   < welcome-file>register.jsp< /welcome-file>
< /welcome-file-list>
< /web-app>

至此,完畢.不過需要注意:

1.配置Struts2驗(yàn)證xml文檔的名字有講究:格式為:Action名字-validation.xml;

2.驗(yàn)證文檔里的 中的type類型要和VO中的User屬性類型一致,否則會(huì)報(bào)類型轉(zhuǎn)換錯(cuò)誤

【編輯推薦】

  1. 在Eclipse中開發(fā)struts應(yīng)用程序
  2. 手把手教你在Eclipse中配置開發(fā)Struts
  3. Eclipse下開發(fā)struts完整解決亂碼問題
  4. Struts相關(guān)背景介紹
  5. 使用Easy Struts for Eclipse開發(fā)Struts

文章題目:淺談Struts2驗(yàn)證框架及用戶注冊(cè)
文章鏈接:http://www.5511xx.com/article/cdcedoo.html