熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

淺談Struts2驗證框架及用戶注冊

2013-11-23 20:22:44  來源: Java開源技術 

  Struts驗證比起Struts驗證框架來好用多了使程序更加清晰易讀充分利用了配置文件的作用也算是解耦的表現吧

  核心代碼如下:

  用戶注冊頁面registerjsp

  

  

  

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

< ! 讀取顯示提示信息 >
< TABLE>


用戶名:







密碼:


< INPUT type=password name=userpassword>

  

確認密碼:


< INPUT type=password name=userrePassword>



年齡:


< INPUT name=userage>




生日:


< INPUT name=userbirthday>








  注冊成功歡迎頁面welcomejsp

  

  congratulations!${useruserName}

  注冊處理action RegisterAction

  

  package orgkingtoonaction;

  import javaxservlethttpHttpServletRequest;
import orgapachestrutsServletActionContext;
import orgkingtoonbeanUser;
import comopensymphonyxworkActionSupport;

  public class RegisterAction extends ActionSupport {

  private User user;
@Override
public String execute() throws Exception {
if(!(usergetPassword()equals(usergetRePassword()))){
thisaddFieldError(password 請輸入相同的密碼);
return input;
}
else
{
HttpServletRequest request = ServletActionContextgetRequest ();
requestsetAttribute(user user);
return SUCCESS;
}

}
public User getUser() {
return user;
}
public void setUser(User user) {
thisuser = user;
}
}

   用戶Bean Userjava

  

  package orgkingtoonbean;

  import javautilDate;

  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) {
thisage = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
thisbirthday = birthday;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
public String getRePassword() {
return rePassword;
}
public void setRePassword(String rePassword) {
thisrePassword = rePassword;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
thisuserName = userName;
}

  }

  配置驗證文件RegisterActionvalidationxml

  

  

  < VALIDATORS>

< ! 驗證字符串不能為空 >
< FIELDVALIDATOR type=requiredstring>
< ! 去空格 >
< PARAM name=trim>true


< ! 錯誤提示信息 >
< MESSAGE>用戶名不能為空
< /FIELDVALIDATOR>

< ! 驗證字符串長度 >
< FIELDVALIDATOR type=stringlength>
< PARAM name=minLength>< /PARAM>

  


< MESSAGE>用戶名長度應在個字符間

  
< FIELDVALIDATOR type=requiredstring>

  true


< MESSAGE>密碼不能為空
< /FIELDVALIDATOR>

< FIELDVALIDATOR type=stringlength>
< PARAM name=minLength>< /PARAM>

  


密碼長度應在個字符之間< /MESSAGE>
< /FIELDVALIDATOR>
< /FIELD>

  
< FIELDVALIDATOR type=int>
< PARAM name=min>


< PARAM name=max>


< MESSAGE>年齡應在之間

< /FIELDVALIDATOR>

< ! 驗證字符串為日期類型 >

< FIELDVALIDATOR type=date>
< PARAM name=min>


< PARAM name=max>< /PARAM>
< MESSAGE>出生日期應在< /MESSAGE>

< /FIELD>
< /VALIDATORS>

  struts框架默認加載的配置文件strutsxml

  

  < STRUTS>
< CONSTANT name=strutscustominresources value=messageResource>< /CONSTANT>

< RESULT name=success>/welcomejsp< /RESULT>
< RESULT name=input>/registerjsp< /RESULT>
< /ACTION>
< /PACKAGE>
< /STRUTS>

   

  web服務器啟動時加載Struts 配置文件 webxml

  

  < ?XML:NAMESPACE PREFIX = [default] < webapp xmlns=
xmlns:xsi=instance
xsi:schemaLocation=
app__xsd>

  < filter>
< filtername>strutscleanup

orgapachestrutsdispatcherActionContextCleanUp

  < filter>
< filtername>struts

orgapachestrutsdispatcherFilterDispatcher

  < filtermapping>
< filtername>strutscleanup< /filtername>
/*

  < filtermapping>
< filtername>struts< /filtername>
< urlpattern>/*< /urlpattern>

  < welcomefilelist>
< welcomefile>registerjsp< /welcomefile>
< /welcomefilelist>
< /webapp>

  至此完畢不過需要注意:

  配置Struts驗證xml文檔的名字有講究:格式為:Action名字validationxml;

  驗證文檔裡的中的type類型要和VO中的User屬性類型一致否則會報類型轉換錯誤


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28393.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.