struts1 记录(数据库1个字段,在jsp页面 拆成2个输入框的情况 )

数据库1个字段,在jsp页面 拆成2个输入框的情况
1
User.java

public class User {
private String name;
private String birthday; // 200906

}

2 UserForm.java

public class UserForm extends ActionForm {

private String username;

private String birthday;

//特别说明,在User.jsp界面,把birthday分成2个widget,一个年份,一个月份
//供用户输入,而不是1个widget,但数据库的字段只有1个birthday

private String birthdayYear;//2009
private String birthdayMonth;//06

public void setBirthdayYear(String birthdayYear) {
this.birthdayYear = birthdayYear;
}

public void setBirthdayMonth(String birthdayMonth) {
this.birthdayMonth = birthdayMonth;
}
===================== 底下就不同 =====================
public void setBirthday(String birthday) { //例如birthday = 200906
this.birthdayYear = birthday.subString(0,4); // =2009
this.birthdayMonth = birthday.subString(4,6) // = 06
}
=====================================================
public String getBirthdayYear() {
return birthdayYear;
}
public String getBirthdayMonth() {
return birthdayMonth;
}
===================== 底下就不同 =====================
public String getBirthday() {
return birthdayYear + birthdayMonth; //2009+06
}
=====================================================

}

3 User.jsp

<html:text property="username" />
<html:text property="birthdayYear" />
<html:text property="birthdayMonth"/>
=================说明================================
property会调用getField这个方法,显示结果为getField()这个方法的返回直,而不是
field这个直,

而没有<html property='birthday'/>这个widget在jsp页面,而是分成
年份(birthdayYear),月份(birthdayMonth)为2个输入框,


4
从一个数据库查得的一条记录信息 显示在jsp界面上
比如:
a user record {
username='lu';
birthday='20090613';
}

构造一个UserForm,显示在jsp界面
UserForm userForm = new UserForm();

userForm.setBirthday( user.getBirthday() );
//说明,调用setBirthday(..);会分别给birthdayYear,birthdayMonth赋直

userForm.setUsername(user.getBirthday());

request.getSession().setAttribute("userform",userForm);

5 显示在jsp界面上的1条记录 插入到数据库去
public class SaveAction extends Action {
public ActionForward execute() {
UserForm userForm = (UserForm)actionForm;
User user = new User();

user.setUsername(userForm.getUsername());

user.setBirthday(userForm.getBirthday();)
//说明:userForm.getBirthday() 返回birthdayYear+birthdayMonth

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值