department.java如下:
package com.struts.domain;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
/**
* Department generated by MyEclipse - Hibernate Tools
*/
public class Department implements java.io.Serializable {
// Fields
/**
*
*/
private final static long serialVersionUID = 416514565465465L;
private Integer departmentId;
private String departmentName;
private String departmentPhone;
private String departmentFax;
private String departmentFunction;
private String remark;
private Integer listNo;
private Department parentDepartment;
private Corporation corporation;
private List<Department> childDepartments;
private List<User> users;
private List<BookInfo> bookInfos;
// Constructors
/** default constructor */
public Department() {
}
/** minimal constructor */
public Department(Integer departmentId) {
this.departmentId = departmentId;
}
/** full constructor */
public Department(Integer departmentId, String departmentName, String departmentPhone, String departmentFax, String departmentFunction, String remark, Integer listNo) {
this.departmentId = departmentId;
this.departmentName = departmentName;
this.departmentPhone = departmentPhone;
this.departmentFax = departmentFax;
this.departmentFunction = departmentFunction;
this.remark = remark;
this.listNo = listNo;
}
// Property accessors
public Integer getDepartmentId() {
return this.departmentId;
}
@SuppressWarnings("unused")
private void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return this.departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getDepartmentPhone() {
return this.departmentPhone;
}
public void setDepartmentPhone(String departmentPhone) {
this.departmentPhone = departmentPhone;
}
public String getDepartmentFax() {
return this.departmentFax;
}
public void setDepartmentFax(String departmentFax) {
this.departmentFax = departmentFax;
}
public String getDepartmentFunction() {
return this.departmentFunction;
}
public void setDepartmentFunction(String departmentFunction) {
this.departmentFunction = departmentFunction;
}
public String getRemark() {
return this.remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Integer getListNo() {
return listNo;
}
public void setListNo(Integer listNo) {
this.listNo = listNo;
}
public List<Department> getChildDepartments() {
if(this.childDepartments == null)
this.childDepartments = new ArrayList<Department>();
return childDepartments;
}
public void setChildDepartments(List<Department> childDepartments) {
this.childDepartments = childDepartments;
}
public Corporation getCorporation() {
return corporation;
}
public void setCorporation(Corporation corporation) {
this.corporation = corporation;
}
public Department getParentDepartment() {
return parentDepartment;
}
public void setParentDepartment(Department parentDepartment) {
this.parentDepartment = parentDepartment;
}
public List<User> getUsers() {
if(this.users == null)
this.users = new ArrayList<User>();
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public List<BookInfo> getBookInfos() {
return bookInfos;
}
public void setBookInfos(List<BookInfo> bookInfos) {
this.bookInfos = bookInfos;
}
/**
* 得到调用Department对象及其以下
* 所有的Department对象
* @return Set<Department> Department对象的集合
*/
public Set<Department> getAllDepartments(){
Set<Department> set = new HashSet<Department>();
this.fillDepartments(set);
return set;
}
/**
* 遍历所有调用Department节点下的
* 子节点,装如Set
* @param set
*/
void fillDepartments(Set<Department> set){
set.add(this);
for(Department department : this.getChildDepartments())
department.fillDepartments(set);
}
@SuppressWarnings("unchecked")
public Set<User> getAllUsersOfDepartment(){
Set set = new HashSet();
//set.add(this);
for(User user : this.getUsers())
set.add(user);
return set;
}
public Department(String departmentName) {
super();
this.departmentName = departmentName;
}
}
映射文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.struts.domain.Department" table="department" lazy="false">
<id name="departmentId" type="java.lang.Integer">
<column name="department_id" />
<generator class="native"/>
</id>
<property name="departmentName" type="java.lang.String">
<column name="department_name" length="50" />
</property>
<property name="departmentPhone" type="java.lang.String">
<column name="department_phone" length="50" />
</property>
<property name="departmentFax" type="java.lang.String">
<column name="department_fax" length="50" />
</property>
<property name="departmentFunction" type="java.lang.String">
<column name="department_function" length="50" />
</property>
<property name="remark" type="java.lang.String">
<column name="remark" length="50" />
</property>
<property name="listNo" type="java.lang.Integer">
<column name="list_no"/>
</property>
<many-to-one name="parentDepartment" column="fk_parent_department" class="com.struts.domain.Department"/>
<many-to-one name="corporation" column="fk_corporation_id" class="com.struts.domain.Corporation"/>
<bag name="childDepartments" inverse="true" cascade="delete" lazy="false">
<key column="fk_parent_department"/>
<one-to-many class="com.struts.domain.Department"/>
</bag>
<bag name="users" inverse="true" lazy="false">
<key column="department_disp_code"/>
<one-to-many class="com.struts.domain.User"/>
</bag>
<bag name="bookInfos" inverse="true">
<key column="dept_id"/>
<one-to-many class="com.struts.domain.BookInfo"/>
</bag>
</class>
</hibernate-mapping>
createUser.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<%@taglib prefix="c" uri="/WEB-INF/c.tld" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<c:if test="${param.corporationPrepared == null}">
<jsp:forward page="/corporationPrepared.do?key=2"/>
</c:if>
<c:if test="${param.departmentPrepared == null}">
<jsp:forward page="/departmentPrepared.do?key=1"/>
</c:if>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>新建用户</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/treeJs/xtree.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/treeJs/xloadtree.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/treeJs/xmlextras.js"></script>
<LINK rel="stylesheet" href="${pageContext.request.contextPath}/css/treeCss/xtree.css" type="text/css" />
<script type="text/javascript">
function getCorporation(){
alert("corporation problem");
//alert(document.getElementById("corporationId").value);
}
function getDepartment(){
alert("department problem");
//alert(document.getElementById("departmentId").value);
}
</script>
</head>
<body>
<form id="userForm" name="userForm" method="post" action="">
<div id="frame">
<div id="frameTitle">用户操作</div>
<div id="frameContent">
<table>
<tbody>
<tr>
<td valign="top" width="30%">
<!--"leftPanel" --><br/>
请选择公司:
<select id="corporation" name="corporation" style="width:70px" οnchange="getCorporation();">
<c:forEach var="corporation" items="${requestScope.corporations}">
<option>${corporation.corporationName}</option>
</c:forEach>
</select>
<input type="hidden" name="corporationId" id="corporationId" value="${document.getElementById("corporation").options[document.getElementById("corporation").selectedIndex].text}"/>
<br/><br/>
请选择部门:
<select id="department" name="department" style="width:70px" οnchange="getDepartment">
<c:forEach var="department" items="${requestScope.departments}">
<option>${department.departmentName}</option>
</c:forEach>
</select>
<input type="hidden" name="departmentId" id="departmentId" value="${document.getElementById("department").options[document.getElementById("department").selectedIndex].text}"/>
<div id="leftPanelTitle">用户及部门列表</div>
<div id="leftPanelContent">
<script type="text/javascript">
webFXTreeConfig.rootIcon = "${pageContext.request.contextPath}/images/treeImages/foldericon.png";
webFXTreeConfig.openRootIcon = "${pageContext.request.contextPath}/images/treeImages/openfoldericon.png";
webFXTreeConfig.folderIcon = "${pageContext.request.contextPath}/images/treeImages/foldericon.png";
webFXTreeConfig.openFolderIcon = "${pageContext.request.contextPath}/images/treeImages/openfoldericon.png";
webFXTreeConfig.fileIcon = "${pageContext.request.contextPath}/images/treeImages/file.png";
webFXTreeConfig.lMinusIcon = "${pageContext.request.contextPath}/images/treeImages/Lminus.png";
webFXTreeConfig.lPlusIcon = "${pageContext.request.contextPath}/images/treeImages/Lplus.png";
webFXTreeConfig.tMinusIcon = "${pageContext.request.contextPath}/images/treeImages/Tminus.png";
webFXTreeConfig.tPlusIcon = "${pageContext.request.contextPath}/images/treeImages/Tplus.png";
webFXTreeConfig.iIcon = "${pageContext.request.contextPath}/images/treeImages/I.png";
webFXTreeConfig.lIcon = "${pageContext.request.contextPath}/images/treeImages/L.png";
webFXTreeConfig.tIcon = "${pageContext.request.contextPath}/images/treeImages/T.png";
webFXTreeConfig.blankIcon = "${pageContext.request.contextPath}/images/treeImages/blank.png";
var tree = new WebFXLoadTree("子部门", "${pageContext.request.contextPath}/createDepartment.do");
document.write(tree);
</script>
</div><!-- leftPanelContent结束 -->
</div><!-- leftPanel 结束 -->
</td>
<td>
<div id="rightPanel">
<div id="rightPanelTitle">用户数据操作</div>
<div id="rightPanelContent">
<table width="417" height="229" border='1' align="center" bordercolor="#CCCCCC"cellspacing="0" >
<tr>
<td width="111" height="25">用户名:</td>
<td width="290"><input type="text" name="userName" id="userName" height="18"/></td>
</tr>
<tr>
<td height="25">真实姓名:</td>
<td><input type="text" name="realName" id="realName" height="18"/></td>
</tr>
<tr>
<td height="25">性别:</td>
<td>
<select name="sex" id="sex" style="width:70px">
<option>男</option>
<option>女</option>
</select>
</td>
</tr>
<tr>
<td height="25">部门:</td>
<td>
<select name="department" id="department" style="width:70px">
<c:forEach var="department" items="${requestScope.departments}">
<option>${department.departmentName}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td height="25">角色:</td>
<td><select name="role" id="role" style="width:70px">
</select></td>
</tr>
<tr>
<td height="25">密码:</td>
<td><input type="text" name="password" id="password" height="18"/></td>
</tr>
<tr>
<td height="25">访问控制:</td>
<td><input type="checkbox" name="login" id="login" value="login" />禁止登录OA系统<br />
<input type="checkbox" name="queryUserList" id="queryUserList" value="queryUserList" />禁止查看用户列表<br />
<input type="checkbox" name="displayTable" id="displayTable" value="displayTable" />禁止显示桌面<br />
</td>
</tr>
<tr>
<td height="23">管理范围:</td>
<td>
<select name="manageScope" id="manageScope" style="width:70px">
</select>
</td>
</tr>
<tr>
<td height="25">考勤排班类型:</td>
<td>
<select name="checkWorkType" id="checkWorkType" style="width:70px">
</select>
</td>
</tr>
<tr>
<td height="25" colspan="2" align="center"><input type="submit" name="add" value="确定"/></td>
</tr>
</table>
</div><!-- rightPanelContent结束 -->
</div>
</td>
</tr>
</tbody>
</table>
</div><!-- frameContent结束 -->
</div><!-- frame结束 -->
</form>
</body>
</html>
createDepartment.java
package com.struts.action.personManage;
import com.struts.servic.DepartmentService;
import com.struts.domain.Department;
import java.util.List;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreateDepartmentAction extends Action{
final Log logger = LogFactory.getLog(CreateDepartmentAction.class);
DepartmentService departmentService;
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
@Override
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.setContentType("text/xml");
String departmentId = request.getParameter("departmentId");
OutputStream stream = response.getOutputStream();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Document treeDOM = this.getDepartmentTree(departmentId);
stream.flush();
transformer.transform(new DOMSource(treeDOM), new StreamResult(stream));
stream.close();
return null;
}
public Document getDepartmentTree(String departmentId) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.newDocument();
logger.info("test:createDepartment:getDeaprtmentTree");
List<Department> departments = null;
Element treeDOM = dom.createElement("tree");
if(departmentId == null){
departments = departmentService.getRootDepartment();
}
else{
logger.info("根据ID查询部门!");
departments = departmentService
.getDepartmentById(Integer.parseInt(departmentId))
.getChildDepartments();
}
for(Department department : departments){
Element tree = dom.createElement("tree");
tree.setAttribute("text", department.getDepartmentName());
tree.setAttribute("action", "javascript:fun('" + department.getDepartmentName() + "')");
if(department.getChildDepartments().size() > 0){
String src = "/OA/createDepartment.do?departmentId=" + department.getDepartmentId();
tree.setAttribute("src", src);
}
treeDOM.appendChild(tree);
}
dom.appendChild(treeDOM);
return dom;
}
}
createDepartment.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib prefix="c" uri="/WEB-INF/c.tld" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<c:if test="${param.corporationPrepared == null}">
<jsp:forward page="/corporationPrepared.do?key=1"/>
</c:if>
<c:if test="${param.departmentPrepared == null}">
<jsp:forward page="/departmentPrepared.do?key=0"/>
</c:if>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>新建部门</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/treeJs/xtree.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/treeJs/xloadtree.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/treeJs/xmlextras.js"></script>
<LINK rel="stylesheet" href="${pageContext.request.contextPath}/css/treeCss/xtree.css" type="text/css" />
<style type="text/css">
div.panelContent{
text-align: center;
padding:10px;
background-color: #ffffee;
}
</style>
<script type="text/javascript">
function verify(){
var list = document.getElementById("list").value;
var name = document.getElementById("departmentName").value;
if(list == null || list == ''){
alert("排序号不能为空!");
return;
}
if(name == null || name == ''){
alert("部门名称不能为空!");
return null;
}
}
function getCorporation(){
alert("开始执行了");
var corporationId = document.createDeptForm.corporation.options[document.createDeptForm.corporation.selectedIndex].text;
alert(corporationId);
Window.location="${pageContext.request.contextPath}/otherCreateDepartment.do?testName=" + corporationId;
alert(Window.location);
}
function changeCorporation(){
alert("test");
leftPanel.style.display="none";
rightPanel1.style.display="none";
rightPanel2.style.display="block";
alert(rightPanel2.style.display);
}
function fun(name){
var departmentName = name;
document.getElementById("parentDepartment").options[document.getElementById("parentDepartment").selectedIndex].text=departmentName;
}
</script>
</head>
<body>
<div id="frame">
<div id="frameTitle" align="center">部门管理</div>
<div id="frameContent">
<table>
<tbody>
<tr>
<td width="30%" valign="top">
<div id="leftPanel">
<div id="leftPaneTitle">部门列表<div>
<div id="leftPaneContent">
公司:
<select id="corporation" name="corporation" οnchange="changeCorporation();" >
<c:forEach var="corporation" items="${requestScope.corporations}">
<option>${corporation.corporationName}</option>
</c:forEach>
</select>
<script type="text/javascript">
webFXTreeConfig.rootIcon = "${pageContext.request.contextPath}/images/treeImages/company.png";
webFXTreeConfig.openRootIcon = "${pageContext.request.contextPath}/images/treeImages/company.png";
webFXTreeConfig.folderIcon = "${pageContext.request.contextPath}/images/treeImages/company.png";
webFXTreeConfig.openFolderIcon = "${pageContext.request.contextPath}/images/treeImages/company.png";
webFXTreeConfig.fileIcon = "${pageContext.request.contextPath}/images/treeImages/company.png";
webFXTreeConfig.lMinusIcon = "${pageContext.request.contextPath}/images/treeImages/Lminus.png";
webFXTreeConfig.lPlusIcon = "${pageContext.request.contextPath}/images/treeImages/Lplus.png";
webFXTreeConfig.tMinusIcon = "${pageContext.request.contextPath}/images/treeImages/Tminus.png";
webFXTreeConfig.tPlusIcon = "${pageContext.request.contextPath}/images/treeImages/Tplus.png";
webFXTreeConfig.iIcon = "${pageContext.request.contextPath}/images/treeImages/I.png";
webFXTreeConfig.lIcon = "${pageContext.request.contextPath}/images/treeImages/L.png";
webFXTreeConfig.tIcon = "${pageContext.request.contextPath}/images/treeImages/T.png";
webFXTreeConfig.blankIcon = "${pageContext.request.contextPath}/images/treeImages/blank.png";
var tree = new WebFXLoadTree("子部门", "${pageContext.request.contextPath}/createDepartment.do");
document.write(tree);
</script>
</div><!-- leftPanelContent结束 -->
</div><!-- leftPanel结束 -->
</td>
<td align="center" width="70%">
<div id="rightPanel1" align="center">
<div id="rightPanelTitle1" align="center">部门操作</div>
<div id="rightPanelContent1" style="text-align:center;display:block">
<html:form action="/otherCreateDepartment.do">
<table width="417" height="229" border='1' align="center" bordercolor="#CCCCCC"cellspacing="0" >
<tr>
<td width="111" height="25">部门排序号:</td>
<td width="290"><input type="text" name="list" id="list" height="18"/></td>
</tr>
<tr>
<td height="25">部门名称:</td>
<td><input type="text" name="departmentName" id="departmentName" height="18"/></td>
</tr>
<tr>
<td height="25"><p>电话:</p></td>
<td><input type="text" name="telNum" id="telNum" height="18"/></td>
</tr>
<tr>
<td height="25">传真:</td>
<td><input type="text" name="fax" id="fax" height="18"/></td>
</tr>
<tr>
<td height="23">上级部门:</td>
<td><select name="parentDepartment" id="parentDepartment">
<option>---无---</option>
<c:forEach var="department" items="${requestScope.departments}">
<option>${department.departmentName}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td height="25">职能:</td>
<td><textarea name="function" id="function"></textarea></td>
</tr>
<tr>
<td height="25" colspan="2" align="center"><input type="submit" name="Submit" value="新建" οnclick="verify();"/></td>
</tr>
</table>
</html:form>
</div>
</div><!-- rightPanel1 div 结束 -->
<div id="rightPanel2" style="display:none">
<div id="rightPanelTitle2">部门修改操作</div>
<div id="rightPanelContent2">
<jsp:include flush="true" page="/WebPage/personManage/editDepartment.jsp"/>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div><!-- frame div content -->
</div><!-- frame div 结束 -->
</body>
</html>