我用的是mysql的数据库,
jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML>
<div class="form-group">
<div class="col-sm-10">
<center><h3>编辑部门信息</h3></center>
</div>
</div>
<div class="form-group">
<label for="dpname" class="col-sm-2 control-label">部门名称:</label>
<div class="col-sm-6">
<input name="department.dpname" type="text" class="form-control" id="dpname" value="${department.dpname}">
</div>
</div>
<div class="form-group">
<label for="dpnum" class="col-sm-2 control-label">部门人数:</label>
<div class="col-sm-6">
<input name="department.dpnum" type="number" class="form-control" id="dpnum" value="${department.dpnum}">
</div>
</div>
<div class="form-group">
<label for="dpmanager" class="col-sm-2 control-label">部门经理:</label>
<div class="col-sm-6">
<input name="department.dpmanager" type="text" value="${department.dpmanager}" class="form-control" id="dpmanager">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-6">
<button type="submit" class="btn btn-default">
提交</button>
</div>
</div>
</form>
</div>
<script src="js/jquery-1.11.1.min.js" ></script>
<script src="js/bootstrap.min.js" ></script>
action
package com.admin.actions;
import hbn.Department;
import hbn.DepartmentDAO;
import java.util.List;
import org.hibernate.Transaction;
public class DeptAction {
private Department department;
private List dlist;
private String dmessage;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public List getDlist() {
return dlist;
}
public void setDlist(List dlist) {
this.dlist = dlist;
}
public String getDmessage() {
return dmessage;
}
public void setDmessage(String dmessage) {
this.dmessage = dmessage;
}
public String deptlist() {
DepartmentDAO dd = new DepartmentDAO();
Transaction tx = dd.getSession().beginTransaction();
dlist = dd.findAll();
tx.commit();
dd.getSession().flush();
String result = "deptlist";
return result;
}
public String deptupdate() {
String result;
try {
DepartmentDAO dd = new DepartmentDAO();
department = dd.findById(department.getId());
dd.getSession().beginTransaction().commit();
result = "deptupdate";
} catch (Exception e) {
// TODO: handle exception
dmessage = "无法编辑该部门信息!";
result = "dmessage";
}
return result;
}
public String deptup() {
String result;
try {
DepartmentDAO dd = new DepartmentDAO();
Transaction tx = dd.getSession().beginTransaction();
System.out.println(department);
dd.merge(department);
tx.commit();
result = "success";
} catch (Exception e) {
// TODO: handle exception
dmessage = "部门信息修改失败!";
result = "error";
}
return result;
}
}
求大家帮忙看看!
在 deptup() 里面,用id从数据库中再取一次department对象。
然后把action的department的每个field复制到刚刚获取的对象中去。
最后在同一个transaction里保存刚才获取的department对象。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。