📄 friendform.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 FriendForm extends ValidatorForm {
//fileds
private String name;
private int sex;
private int age;
private String year;
private String month;
private String date;
private String phone;
private String address;
private Date birthday;
private String[] hobbies;
private static final String DATE_FORMATE = "yyyy-MM-dd";
//初始化
@SuppressWarnings("deprecation")
public void reset(ActionMapping mapping, HttpServletRequest request) {
hobbies=new String[]{};
sex =0;
age=22;
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"));
return errors;
}
}
//getter and setter
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 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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -