Example1
UserAction.java
package action; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.validator.annotations.DateRangeFieldValidator; import com.opensymphony.xwork2.validator.annotations.EmailValidator; import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator; import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator; import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator; import com.opensymphony.xwork2.validator.annotations.Validations; import java.util.Date; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; @Validations public class UserAction extends ActionSupport implements RequestAware, SessionAware { private String userName; private String password; private String confPassword; private String email; private Integer age; private Date bday; private Map<String, Object> requestMap; private Map<String, Object> sessionMap; public void validate() { if (!password.equals(confPassword)) { addActionError("Password mismatch"); } } public String login() { return SUCCESS; } @DateRangeFieldValidator(max="12/31/2010", min="12/21/2010", message="Invalid Date Range") public Date getBday() { return bday; } public void setBday(Date bday) { this.bday = bday; } @StringLengthFieldValidator(maxLength="20", minLength="10", message="Invalid password length", trim=false) @RequiredStringValidator(message="Enter Password") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @StringLengthFieldValidator(maxLength="20", minLength="10", message="Invalid username length", trim=true) @RequiredStringValidator(message="Enter Username") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @RequiredStringValidator(message="Enter Confirm Password") public String getConfPassword() { return confPassword; } public void setConfPassword(String confPassword) { this.confPassword = confPassword; } @RequiredStringValidator(message="Enter email") @EmailValidator(message="Invalid email") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @IntRangeFieldValidator(min="18", max="25",message="Invalid age") public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public void setRequest(Map<String, Object> map) { System.out.println("set request"); requestMap = map; } public void setSession(Map<String, Object> map) { System.out.println("set session" ); sessionMap = map; } }
Example 2
UserAction.java
package action; import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator; import com.opensymphony.xwork2.validator.annotations.Validation; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @Validation public class UserAction extends ActionSupport { private String userName; private String password; public String signIn() { if(check user name and password with DB. if return true){ return SUCCESS; } else{ addActionError("Invalid Usernme or Password"); return INPUT; } } @RequiredStringValidator(message="Username Is Required") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @RequiredStringValidator(message="Password Is Required") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
No comments:
Post a Comment