传智播客-----OA页面的增删改查

今天汤老师给我们讲了oa页面的增删改查!

1,介绍
组织机构
审批流转
信息发布
知识管理
网上交流、即时通讯
实用工具
...

struts1.3.8 + hibernate3.2 + jbpm3.2.2 + junit4
view + service(service + dao)

0,准备环境
1,目录结构
2,框架环境
3,事务控制
1,组织机构
1,部门
1,列表
显示同一层的部门列表
默认显示最顶级的部门
2,添加
显示所有的部门列表,有层次
添加完后转到新增的部门所在的列表页面
3,删除
不能删除含有子部门的部门
2,员工
1,添加
员工属于一个部门
员工可以有职务
2,审批流转


2,准备环境
1,创建目录结构
2,添加框架环境
1,struts, jstl, jbpm(hibernate)与jdbc驱动, junit的jar与配置文件
2,解决struts与hibernate的jar包冲突问题
3,修改数据库连接信息并创建数据库表
-,显示层与业务层结构
1,设计
2,BaseService与BaseAction
public class DepartmentAction extends BaseAction {

/**
* 树状显示所有的部门
*/
public ActionForward tree(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("tree");
}

/**
* 显示同级的部门列表,默认显示最顶级的部门列表 parentId=5
*/
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
DepartmentActionForm actionForm = (DepartmentActionForm) form;
Department parent = departmentService.getDepartment(actionForm.getParentId()); //
request.setAttribute("parent", parent);

List<Department> depts = departmentService.findDepartments(parent);
request.setAttribute("depts", depts);
return mapping.findForward("list");
}

/**
* 添加页面
*/
public ActionForward addUI(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
List<Department> depts = departmentService.findAll();
request.setAttribute("allDepts", depts);
return mapping.findForward("saveUI");
}

/**
* 添加
*/
public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
DepartmentActionForm actionForm = (DepartmentActionForm) form;
Department parent = departmentService.getDepartment(actionForm.getParentId());

Department department = new Department();
department.setName(actionForm.getName());
department.setParent(parent); // null

departmentService.addDepartment(department);

ActionForward af = mapping.findForward("toList");
return new ActionForward(af.getPath() + "&parentId=" + actionForm.getParentId(), af.getRedirect());
}

/**
* 修改页面
*/
public ActionForward editUI(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {

// 1,准备actionForm
DepartmentActionForm actionForm = (DepartmentActionForm) form;
Department department = departmentService.getDepartment(actionForm.getId());
actionForm.setName(department.getName());
if (department.getParent() != null) {
actionForm.setParentId(department.getParent().getId());
}

// 2,准备 allDepts
List<Department> depts = departmentService.findAll();
request.setAttribute("allDepts", depts);

return mapping.findForward("saveUI"); // forward
}

/**
* 修改
*/
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
DepartmentActionForm actionForm = (DepartmentActionForm) form;
Department parent = departmentService.getDepartment(actionForm.getParentId());

Department department = departmentService.getDepartment(actionForm.getId());
department.setName(actionForm.getName());
department.setParent(parent); // null

departmentService.updateDepartment(department);

ActionForward af = mapping.findForward("toList");
return new ActionForward(af.getPath() + "&parentId=" + actionForm.getParentId(), af.getRedirect());
}

/**
* 删除
*/
public ActionForward del(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
DepartmentActionForm actionForm = (DepartmentActionForm) form;
try {
departmentService.delDepartment(actionForm.getId());
} catch (NotEmptyException e) {
ActionErrors errors = new ActionErrors();
errors.add("del", new ActionMessage(e.getMessage(), false));
saveErrors(request, errors);

ActionForward af = mapping.findForward("toList");
// 出错后,应转发到列表页面
return new ActionForward(af.getPath() + "&parentId=" + actionForm.getParentId(), false);
}

ActionForward af = mapping.findForward("toList");
return new ActionForward(af.getPath() + "&parentId=" + actionForm.getParentId(), af.getRedirect());
}
}
3,事务与JbpmContext管理
1,设计
1,自己用的Session与JbpmContext中的Session要是同一个
2,如果请求没有用到JbpmContext(如转到页面),就不要创建他
2,JbpmContextManager
3,JbpmContextFilter
4,BaseService 中的 getJbpmContext() 与 getSession()

public class JbpmContextManager {
private static final ThreadLocal<JbpmContext> threadLocal = new ThreadLocal<JbpmContext>();
private static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();

/**
* 获取与当前线程对应的JbpmContext
*
* @param create
* 当前线程没有对应的JbpmContext时,是否创建一个新的JbpmContext,并且对应起来
*/
public static JbpmContext getCurrentJbpmContext(boolean create) {
JbpmContext jbpmContext = threadLocal.get();
if (jbpmContext == null && create) {
System.out.println("创建 JbpmContext ...");
jbpmContext = jbpmConfiguration.createJbpmContext();
threadLocal.set(jbpmContext);
}
return jbpmContext;
}

/**
* 关闭当前线程对应的JbpmContext,并且移除关联
*/
public static void closeCurrentJbpmContext() {
JbpmContext jbpmContext = threadLocal.get();
if (jbpmContext != null) {
jbpmContext.close();
threadLocal.remove();
}
}
}

4,解决提交表单内容的中文乱码的问题
public class JbpmContextFilter implements Filter {

public void destroy() {
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
try {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
} catch (Exception e) {
JbpmContext jbpmContext = JbpmContextManager.getCurrentJbpmContext(false);
if (jbpmContext != null) {
jbpmContext.setRollbackOnly();
}
throw new ServletException(e);
} finally {
JbpmContextManager.closeCurrentJbpmContext();
}
}

public void init(FilterConfig filterConfig) throws ServletException {
}

}

3,组织机构管理
1,介绍需求(页面效果)
2,设计实体,写映射文件
1.<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.oa.entities">
<class name="Department" table="itcast_department">
<id name="id">
<generator class="native" />
</id>
<property name="name" />

<many-to-one name="parent" column="parentId"></many-to-one>

<set name="children">
<key column="parentId"></key>
<one-to-many class="Department" />
</set>
</class>
</hibernate-mapping>

2.<hibernate-mapping package="cn.itcast.oa.entities">
<class name="Employee" table="itcast_employee">
<id name="id">
<generator class="native"/>
</id>
<property name="loginName"/>
<property name="password"/>

<property name="name"/>
<many-to-one name="department" column="departmentId"></many-to-one>
<set name="roles" table="itcast_employee_role">
<key column="employeeId"></key>
<many-to-many class="Role" column="roleId"></many-to-many>
</set>

<property name="sex"/>
<property name="birthday" type="date"/>
<property name="phoneNumber"/>
<property name="email"/>
<property name="description"/>
</class>
</hibernate-mapping>

3.<hibernate-mapping package="cn.itcast.oa.entities">
<class name="Role" table="itcast_role">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="description"/>
</class>
</hibernate-mapping>
3,部门的树形显示
1,显示所有部门的名称
2,显示所有部门时用全角空格分出层次
3,xTree的使用
4,在页面中使用xTree显示树状部门列表
5,在页面中显示模式(与非模式)对话框
6,在模式对话框中选择一个部门,就会更新员工所属部门的信息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值