部门管理
测试map迭代时元素在栈顶
页面上迭代map
1
2
3
4
5
6
7
8public String showMap() {
Department department = new Department();
department .setName("华沙");
Map<String, Department> map = new HashMap<String, Department>();
map.put("m1", department);
ActionContext.getContext().put("map", map);
return listAction;
}1
2
3
4
5
6
7
8<!--
Map<String,Department>
map中含有Strign,Department
-->
<s:iterator value="#map">
<s:property value="key"/>
<s:property value="value.name"/>
</s:iterator>list中包含map进行迭代
1
2
3
4
5
6
7
8
9
10public String showListMap() {
List<Map<String, Department>> list = new ArrayList<Map<String,Department>>();
Map<String, Department> map = new HashMap<String, Department>();
Department department = new Department();
department.setName("沙耶香");
map.put("M1", department);
list.add(map);
ActionContext.getContext().put("listMap", list);
return listAction;
}1
2
3
4
5
6
7
8
9
10
11
12
13<!--
List<Map<String,Department>>
-->
<s:iterator value="#listMap">
<!--
list集合中当前迭代的元素在栈顶,所以map在栈顶,如果s:iterator中的value属性不写,默认迭代栈顶的元素
map的key为M1 这里可不写
-->
<s:iterator>
<s:property value="key"/>
<s:property value="value.name"/>
</s:iterator>
</s:iterator>map中包含list进行迭代
1
2
3
4
5
6
7
8
9
10public String showMapList() {
Map<String, List<Department>> map = new HashMap<String, List<Department>>();
List<Department> list = new ArrayList<Department>();
Department department = new Department();
department.setName("宫水三叶");
list.add(department);
map.put("list1", list);
ActionContext.getContext().put("maplist", map);
return listAction;
}1
2
3
4
5
6
7
8
9<!--
Map<String,List<Department>>
-->
<s:iterator value="#maplist">
<s:property value="key"/>
<s:iterator value="value">
<s:property value="name"/>
</s:iterator>
</s:iterator>list中包含map,map中包含list进行迭代
1
2
3
4
5
6
7
8
9
10
11
12
13
14public String showListMapList() {
List<Map<String, List<Department>>> list = new ArrayList<Map<String,List<Department>>>();
Department department = new Department();
department.setName("立花泷");
List<Department> dList = new ArrayList<Department>();
dList.add(department);
Map<String, List<Department>> map = new HashMap<String, List<Department>>();
map.put("map1", dList);
list.add(map);
ActionContext.getContext().put("listMapList", list);
return listAction;
}1
2
3
4
5
6
7
8
9
10
11<!--
List<Map<String,List<Department>>>
-->
<s:iterator value="#listMapList">
<s:iterator>
<s:property value="key"/>
<s:iterator value="value">
<s:property value="name"/>
</s:iterator>
</s:iterator>
</s:iterator>
部门模块添加注意事项
在添加页面add.jsp中
在form表单中,必须使用struts2的标签
1
2
3<s:form action="departmentAction_add.action">
<s:textfield name="name" cssClass="InputStyle"></s:textfield>
</s:form>
在后台action中:
- 模型驱动
- 获取表单中的数据
- 把数据放在模型驱动中用于数据回显
- 一般情况下不要把模型驱动传递到dao层
- 模型驱动
部门模块的增加
修改list.jsp文件
1
2
3
4
5
6
7<!-- 其他功能超链接 -->
<div id="TableTail">
<div id="TableTail_inside">
<a href="departmentAction_addUI.action"> <imgsrc="${pageContext.request.contextPath}/css/images/createNew.png" />
</a>
</div>
</div>DepartmentAction增加方法
1
2
3public String addUI() {
return addUI;
}修改struts-department.xml文件
1
2
3
4
5
6<package name="department" namespace="/" extends="struts-default">
<action name="departmentAction_*" method="{1}" class="departmentAction">
<result name="listAction">/WEB-INF/jsp/system/department/list.jsp</result>
<result name="addUI">/WEB-INF/jsp/system/department/add.jsp</result>
</action>
</package>编写add.jsp文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/common.jsp" %>
<html>
<head>
<title>部门设置</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<!-- 标题显示 -->
<div id="Title_bar">
<div id="Title_bar_Head">
<div id="Title_Head"></div>
<div id="Title"><!--页面标题-->
<img border="0" width="13" height="13" src="${pageContext.request.contextPath}/css/images/title_arrow.gif"/> 部门信息
</div>
<div id="Title_End"></div>
</div>
</div>
<!--显示表单内容-->
<div id=MainArea>
<s:form action="departmentAction_add.action">
<div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1">
<IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/css/blue/images/item_point.gif" /> 部门信息 </DIV> -->
</div>
<!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr><td>部门名称</td>
<td><s:textfield name="name" cssClass="InputStyle"></s:textfield></td>
</tr>
<tr><td>职能说明</td>
<td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td>
</tr>
</table>
</div>
</div>
<!-- 表单操作 -->
<div id="InputDetailBar">
<input type="image" src="${pageContext.request.contextPath}/css/images/save.png"/>
<a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/css/images/goBack.png"/></a>
</div>
</s:form>
</div>
<div class="Description">
说明:<br />
1,上级部门的列表是有层次结构的(树形)。<br/>
2,如果是修改:上级部门列表中不能显示当前修改的部门及其子孙部门。因为不能选择自已或自已的子部门作为上级部门。<br />
</div>
</body>
</html>DepartmentService接口修改 增加方法
1
public void saveDepartment(Department department);
DepartmentServiceImpl实现类修改 增加方法
1
2
3
4
public void saveDepartment(Department department) {
this.departmentDao.saveEntry(department);
}action增加方法
1
2
3
4
5
6
7
8
9public String add() {
Department department = new Department();
/*
* 把model的值赋值给department
*/
BeanUtils.copyProperties(this.getModel(), department);
this.departmentService.saveDepartment(department);
return action2action;
}修改struts-department.xml配置文件
1
2
3
4
5
6
7<package name="department" namespace="/" extends="struts-default">
<action name="departmentAction_*" method="{1}" class="departmentAction">
<result name="listAction">/WEB-INF/jsp/system/department/list.jsp</result>
<result name="addUI">/WEB-INF/jsp/system/department/add.jsp</result>
<result name="action2action" type="redirectAction">departmentAction_showAllDepartment.action</result>
</action>
</package>增加测试类DepartmentTest测试后台代码是否可用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public void testSessionFactory() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
DepartmentService departmentService = (DepartmentService)context.getBean("departmentService");
Department department = new Department();
department.setName("技术部");
department.setDescription("大佬多");
departmentService.saveDepartment(department);
}
//测试方法 会报异常
public void Test() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Department department =(Department) session.get(Department.class, 1L);
Department department2 = new Department();
department2.setDid(1L);
department2.setDescription("abc");
session.update(department2);
transaction.commit();
session.close();
}