📄 userform.java
字号:
package com.lideedu.huang.addressBook.struts.form;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.util.LabelValueBean;
import org.apache.struts.validator.ValidatorForm;
@SuppressWarnings("serial")
public class UserForm extends ValidatorForm {
// fileds
private String username;
private String password;
private String passwordConfirm;
private int sex;
private int age;
private String year;
private String month;
private String date;
private Date birthday;
private String[] hobbies;
private Date regDate;
private static final String DATE_FORMATE = "yyyy-MM-dd";
// 初始化
@SuppressWarnings("deprecation")
public void reset(ActionMapping mapping, HttpServletRequest request) {
username = null;
password = null;
sex = 0;
age = 22;
hobbies = new String[] {};
ArrayList<LabelValueBean> yearOptions = new ArrayList<LabelValueBean>();
ArrayList<LabelValueBean> monthOptions = new ArrayList<LabelValueBean>();
ArrayList<LabelValueBean> dateOptions = new ArrayList<LabelValueBean>();
for (int i = 1970; i <= new Date().getYear() + 1900; i++) {
LabelValueBean option = new LabelValueBean(String.valueOf(i),
String.valueOf(i));
yearOptions.add(option);
}
for (int i = 1; i <= 12; i++) {
LabelValueBean option = null;
if (i < 10)
option = new LabelValueBean("0" + String.valueOf(i), "0"
+ String.valueOf(i));
else
option = new LabelValueBean(String.valueOf(i), String
.valueOf(i));
monthOptions.add(option);
}
for (int i = 1; i <= 31; i++) {
LabelValueBean option = null;
if (i < 10)
option = new LabelValueBean("0" + String.valueOf(i), "0"
+ String.valueOf(i));
else
option = new LabelValueBean(String.valueOf(i), String
.valueOf(i));
dateOptions.add(option);
}
request.setAttribute("yearOptions", yearOptions);
request.setAttribute("monthOptions", monthOptions);
request.setAttribute("dateOptions", dateOptions);
}
// validate
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
if (request.getParameter("method").equals("readyForUpdate")) {
return null;
} else {
ActionErrors errors = super.validate(mapping, request);
if (year.equals("1") || month.equals("1") || date.equals("1"))
errors.add("birthdayError", new ActionMessage("birthdayError"));
if (hobbies.length < 1)
errors.add("hobbiesEmpty", new ActionMessage("hobbiesEmpty"));
if (!password.equals(passwordConfirm))
errors.add("passwordConfirmError", new ActionMessage(
"passwordConfirmError"));
return errors;
}
}
// getter and setter
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMATE);
String str = dateFormat.format(birthday);
String[] d = str.split("-");
this.year = d[0];
this.month = d[1];
this.date = d[2];
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMATE);
try {
this.birthday = dateFormat.parse(getYear() + "-" + getMonth() + "-"
+ getDate());
} catch (ParseException e) {
e.printStackTrace();
}
}
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String getPasswordConfirm() {
return passwordConfirm;
}
public void setPasswordConfirm(String passwordConfirm) {
this.passwordConfirm = passwordConfirm;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public Date getRegDate() {
this.regDate = new Date();
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -