成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

開發 后端
本文將對Struts2驗證框架如何用戶注冊進行講解,其中Struts2比起Struts1的驗證框架來更加方便。

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

核心代碼如下:

1.用戶注冊頁面register.jsp

  

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

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

    
     
       確認密碼:
     
     
       < INPUT type=password name=user.rePassword>
     
    

 

    


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


  

2.注冊成功歡迎頁面welcome.jsp

congratulations!${user.userName} 

3.注冊處理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", "請輸入相同的密碼");
      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.配置驗證文件RegisterAction-validation.xml

< VALIDATORS>

   < !-- 驗證字符串不能為空 -->
   < FIELD-VALIDATOR type="requiredstring">
    < !-- 去空格 -->
    < PARAM name="trim">true
    < !-- 錯誤提示信息 -->
    < MESSAGE>用戶名不能為空
   < /FIELD-VALIDATOR>
 
   < !-- 驗證字符串長度 -->
   < FIELD-VALIDATOR type="stringlength">
    < PARAM name="minLength">2< /PARAM>
    20
    < MESSAGE>用戶名長度應在2到18個字符間
  


   < FIELD-VALIDATOR type="requiredstring">
    true
    < MESSAGE>密碼不能為空
   < /FIELD-VALIDATOR>
 
   < FIELD-VALIDATOR type="stringlength">
    < PARAM name="minLength">6< /PARAM>
    18
    密碼長度應在6到18個字符之間< /MESSAGE>
   < /FIELD-VALIDATOR>
< /FIELD>


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

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

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

6.struts2框架默認加載的配置文件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服務器啟動時加載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驗證xml文檔的名字有講究:格式為:Action名字-validation.xml;

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

【編輯推薦】

  1. 在Eclipse中開發struts應用程序
  2. 手把手教你在Eclipse中配置開發Struts
  3. Eclipse下開發struts完整解決亂碼問題
  4. Struts相關背景介紹
  5. 使用Easy Struts for Eclipse開發Struts
責任編輯:張燕妮 來源: 新浪博客
相關推薦

2011-03-30 09:03:57

struts2

2009-06-25 15:37:12

Struts2教程Validation框

2009-02-04 13:13:03

2009-06-04 09:41:50

struts2上傳文件

2009-06-04 11:08:32

struts2 val框架

2009-06-08 16:44:00

Struts2文件上傳

2009-07-29 09:54:34

struts2和str

2009-06-25 15:33:12

Struts2教程使用validate驗證數據

2009-06-05 10:48:01

struts2 ite功能

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-06-04 08:45:01

Struts2下載

2009-06-04 08:01:25

Struts2攔截器原理

2009-02-04 12:00:08

2012-08-30 09:48:02

Struts2Java

2009-06-08 16:44:00

2011-06-28 09:14:23

Struts 2WebWork

2009-06-05 10:43:29

struts2 checheckbox實例

2009-02-04 10:51:07

2009-07-03 09:35:57

Struts2 JSP

2011-11-25 13:01:16

JavaMVCstruts2
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 在线视频国产一区 | 成人在线观看欧美 | 欧美一级特黄aaa大片在线观看 | 成人av免费在线观看 | 青青青伊人 | 亚洲成人av在线播放 | 国产日韩一区二区三免费高清 | 福利片在线观看 | 亚洲福利一区 | 91久久精品一区二区二区 | 国产高清久久 | 久久国产精品久久久久久久久久 | 一区二区三区欧美 | 精品国产区 | 青青草视频网站 | 国产日韩精品一区 | 久久久久久亚洲精品 | 久久毛片 | 久久精品女人天堂av | 欧美日韩久久精品 | 久久久久亚洲 | 伊人超碰| 国产免费视频 | 亚洲国产精品成人无久久精品 | 亚洲色欧美另类 | 国产成人精品免费 | 日韩电影一区二区三区 | av影音资源| 欧美日韩一卡二卡 | 久久久久一区 | 免费成人高清在线视频 | 欧美一级片免费看 | 亚洲黄色av | 精品久久久久久久 | 日韩一区二区三区av | 国产三级日本三级 | 黄色在线播放视频 | 国产黄色精品在线观看 | 欧美一区二区三区在线 | 久久国产婷婷国产香蕉 | 91亚洲精品久久久电影 |