当使用struts+HIbernate把表单数据存储到数据库中时,如果表单中要提交的数据有很多,在struts自定义的Action中把属性一个个写出来就很麻烦,而且属性多时,要写的getter()和setter()也很多,导致页面代码会很混乱。这时,我们可以先建立一个类TbUser,对应于表单中的每一个要存储的字段。然后在自定义action中把TbUser作为变量就可以了。
1、表单:一个注册页面的表单,数据有点多:
<form role="form" action="userAction!save" method="post">
<div class="form-group">
用户名:
<input name="name" type="text" required class="form-control">
<span id="showinfo"></span>
</div>
<div class="form-group">
密码:
<input name="pwd" type="password" required class="form-control">
</div>
<div class="form-group">
性别:
<input name="sex" type="text" required class="form-control">
</div>
<div class="form-group">
出生日期:
<input name="birthday" type="date" required>
</div>
<div class="form-group">
身份证号:
<input name="idCode" type="text" required class="form-control">
</div>
<div class="form-group">
学历:
<label>
<input type="radio" name="education" value="本科"
id="tbUser.education_0">
本科
</label>
<label>
<input type="radio" name="education" value="硕士"
id="tbUser.education_1">
硕士
</label>
<label>
<input type="radio" name="education" value="博士"
id="tbUser.education_2">
博士
</label>
<label>
<input type="radio" name="education"
value="大专" id="tbUser.education_3">
大专
</label>
<label>
<input type="radio" name="education" value="其他"
id="tbUser.education_4">
其他
</label>
</div>
<div class="form-group">
婚姻状况:
<label>
<input type="radio" name="married" value="已婚"
id="tbUser.married_0">
已婚
</label>
<label>
<input type="radio" name="married" value="未婚"
id="tbUser.married_1">
未婚
</label>
</div>
<div class="form-group">
到职日期:
<input type="date" name="enterTime" />
</div>
<div class="form-group">
职务:
<label>
<input type="radio" name="position" value="普通员工"
id="tbUser.position_0">
普通员工
</label>
<label>
<input type="radio" name="position" value="主管"
id="tbUser.position_1">
主管
</label>
<label>
<input type="radio" name="position" value="经理"
id="tbUser.position_2">
经理
</label>
</div>
<div class="form-group">
部门:
<label>
<input type="radio" name="department" value="财务部"
id="department_0">
财务部
</label>
<label>
<input type="radio" name="department" value="人事部"
id="department_1">
人事部
</label>
<label>
<input type="radio" name="department" value="采购部"
id="department_2">
采购部
</label>
<label>
<input type="radio" name="department" value="业务部"
id="department_3">
业务部
</label>
</div>
<div class="form-group">
手机号码:
<input type="number" name="phone" required class="form-control" />
</div>
<div class="checkbox">
<label>
<input type="checkbox" />
Check me out
</label>
<br>
<br>
<button type="submit" class="btn btn-primary" id="submit">
提交
</button>
<button type="reset" class="btn btn-primary" id="reset">
重置
</button>
</div>
</form>
2、一个对应于数据库数据表的类:TbUser:
public class TbUser implements java.io.Serializable {
// Fields
private Integer id;
private String isAdmin;
private String name;
private String pwd;
private String sex;
private String idCode;
private Date birthday;
private String education;
private String married;
private Date enterTime;
private String position;
private String department;
private String phone;
// Constructors
/** default constructor */
public TbUser() {
}
/** minimal constructor */
public TbUser(String name, String pwd, String sex, String idCode,
Date birthday, String education, String married, Date enterTime,
String position, String department, String phone) {
this.name = name;
this.pwd = pwd;
this.sex = sex;
this.idCode = idCode;
this.birthday = birthday;
this.education = education;
this.married = married;
this.enterTime = enterTime;
this.position = position;
this.department = department;
this.phone = phone;
}
/** full constructor */
public TbUser(String isAdmin, String name, String pwd, String sex,
String idCode, Date birthday, String education, String married,
Date enterTime, String position, String department, String phone) {
this.isAdmin = isAdmin;
this.name = name;
this.pwd = pwd;
this.sex = sex;
this.idCode = idCode;
this.birthday = birthday;
this.education = education;
this.married = married;
this.enterTime = enterTime;
this.position = position;
this.department = department;
this.phone = phone;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getIsAdmin() {
return this.isAdmin;
}
public void setIsAdmin(String isAdmin) {
this.isAdmin = isAdmin;
}
省略了其他getter()和setter()
3、自定义的Action:userAction。继承于ActionSupport,实例化接口ModelDriven(这样可以轻松地把表单数据注入到Action的属性中):
public class userAction extends ActionSupport implements ModelDriven<TbUser>{
private TbUser tbUser=new TbUser();//变量tuUser
public String save() throws Exception{
Session session = null;
Transaction tran=null;
try{
session =HibernateSessionFactory.getSession();
tran=session.beginTransaction();
session.save(tbUser); //把tbUser保存到数据库中,注意,tbUser的各属性值已经由表单自行注入
tran.commit();
}
catch (Exception e) {
e.printStackTrace();
tran.rollback();// 事务回滚
} finally {
HibernateSessionFactory.closeSession();// 关闭Session
}
return "testtest";
}
public void setUser(TbUser user) {
this.tbUser= user;
}
public TbUser getModel() {
// TODO Auto-generated method stub
return this.tbUser;
}
}
以上就是实现驱动模型ModelDriven的过程,相比于,把属性一个个地注入,这种方法更省事省力!