Struts2异常处理及用户查询
struts2的异常处理
全局异常处理
新建异常处理包及类-建立一个异常处理类
cn.itcast.shoa.exception
cn.itcast.shoa.exception.MyException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package cn.itcast.shoa.exception;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MyException extends ActionSupport{
private Exception exception;
public Exception getException() {
return exception;
}
public void setException(Exception exception) {
this.exception = exception;
}
public String execute() {
//将获取到的异常放入栈顶
ActionContext.getContext().getValueStack().push(exception.getMessage());
return "error";
}
}struts.xml配置文件进行配置
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
<struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.ui.theme" value="simple"/>
<include file="struts/struts-person.xml"></include>
<include file="struts/struts-department.xml"></include>
<include file="struts/struts-role.xml"></include>
<!-- 异常处理 -->
<package name="struts-global" namespace="/" extends="struts-default">
<global-results>
<result name="errHandler" type="chain">
<param name="actionName">errorProcessor</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="errHandler" />
</global-exception-mappings>
<action name="errorProcessor" class="cn.itcast.shoa.exception.MyException">
<result name="error">WEB-INF/jsp/error.jsp</result>
</action>
</package>
</struts>struts-role.xml配置文件 在role管理使用异常处理测试 所以配置文件继承异常处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<struts>
<!-- 继承异常处理 -->
<package name="role" namespace="/" extends="struts-global">
<action name="roleAction_*" method="{1}" class="roleAction">
<result name="listAction">/WEB-INF/jsp/system/role/list.jsp</result>
<result name="addUI">/WEB-INF/jsp/system/role/add.jsp</result>
<result name="updateUI">/WEB-INF/jsp/system/role/update.jsp</result>
<result name="action2action" type="redirectAction">roleAction_showAllRole.action</result>
</action>
</package>
</struts>从上述代码看来:extends=”struts-global”,所以role模块就拥有了struts-global包(package)里的所有功能
测试 在RoleServiceImpl
1
2
3
4
5
6
7
public Collection<Role> getAllRole() {
// TODO Auto-generated method stub
int i = 1/0;
//throw new RuntimeException("abc");
return this.roleDao.getAllEntry();
}error.jsp
1
2
3
4
5
6
7
8
9
10
11<%@ 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>
<s:property/>
</body>
</html>
用户管理模块
用户的查询
在页面显示的数据来源于3张表
- user
- department
- role
后台的sql:
- from user
- 该sql只是把user的数据查出,没有把相应的部门数据和岗位数据查出
在前台
显示部门名称
如果在web.xml文件配置了OpenSessionInView模式
1
2
3<s:iterator value="#userList">
<s:property value="department.name"/>
</s:iterator>如果没有配置OpenSessionInView模式
1
from user u left outer join fetch u.department d left outer join fetch u.role r
vo:view object
UserView
username
departmentname
roles
bo:busineww object
pojo:entity object
如果采用vo和pojo会造成从vo到pojo的转换
一般采用的技术方式是:
在web.xml文件配置OpenSessionInView模式,这样在jsp页面中session也不会关闭
UserDao
1
2
3
4
5
6
7
8package cn.itcast.shoa.dao;
import cn.itcast.shoa.dao.base.BaseDao;
import cn.itcast.shoa.domain.system.User;
public interface UserDao extends BaseDao<User>{
}UserDaoImpl
1
2
3
4
5
6
7
8
9
10
11
12package cn.itcast.shoa.dao.impl;
import org.springframework.stereotype.Repository;
import cn.itcast.shoa.dao.UserDao;
import cn.itcast.shoa.dao.base.impl.BaseDaoImpl;
import cn.itcast.shoa.domain.system.User;
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{
}UserService
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
36package cn.itcast.shoa.service;
import java.io.Serializable;
import java.util.Collection;
import cn.itcast.shoa.domain.system.User;
public interface UserService {
/*
* 查找所有用户
*/
public Collection<User> getAllUser();
/*
* 查找单个用户
*/
public User getUserById(Serializable id);
/*
* 保存用户
*/
public void saveUser(User user);
/*
* 删除用户
*/
public void deleteUser(Serializable id);
/*
* 修改用户
*/
public void updateUser(User user);
}UserServiceImpl
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
52package cn.itcast.shoa.service.impl;
import java.io.Serializable;
import java.util.Collection;
import javax.annotation.Resource;
import org.hibernate.annotations.Source;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.shoa.dao.UserDao;
import cn.itcast.shoa.domain.system.User;
import cn.itcast.shoa.service.UserService;
public class UserServiceImpl implements UserService{
private UserDao userDao;
public Collection<User> getAllUser() {
// TODO Auto-generated method stub
return this.userDao.getAllEntry();
}
public User getUserById(Serializable id) {
// TODO Auto-generated method stub
return this.userDao.getEntryById(id);
}
public void saveUser(User user) {
// TODO Auto-generated method stub
this.userDao.saveEntry(user);
}
public void deleteUser(Serializable id) {
// TODO Auto-generated method stub
this.userDao.deleteEntry(id);
}
public void updateUser(User user) {
// TODO Auto-generated method stub
this.userDao.updateEntry(user);
}
}UserAction
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
30package cn.itcast.shoa.struts.action;
import java.util.Collection;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import cn.itcast.shoa.domain.system.User;
import cn.itcast.shoa.service.UserService;
import cn.itcast.shoa.struts.action.base.BaseAction;
public class UserAction extends BaseAction<User>{
private UserService userService;
public String showAllUser() {
Collection<User> userList = this.userService.getAllUser();
ActionContext.getContext().put("userList", userList);
return listAction;
}
}struts.xml
1
<include file="struts/struts-user.xml"></include>
struts-user.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<struts>
<!-- 继承异常处理 -->
<package name="user" namespace="/" extends="struts-default">
<action name="userAction_*" method="{1}" class="userAction">
<result name="listAction">/WEB-INF/jsp/system/user/list.jsp</result>
<result name="addUI">/WEB-INF/jsp/system/user/add.jsp</result>
<result name="updateUI">/WEB-INF/jsp/system/user/update.jsp</result>
<result name="action2action" type="redirectAction">userAction_showAllUser.action</result>
</action>
</package>
</struts>list.jsp-WEB-INF/jsp/system/user/list.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101<%@ 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">
<table cellspacing="0" cellpadding="0" class="TableStyle">
<!-- 表头-->
<thead>
<tr align=center valign=middle id=TableTitle>
<td width="100">姓名</td>
<td width="100">所属部门</td>
<td>所属岗位</td>
<td>相关操作</td>
</tr>
</thead>
<!--显示数据列表-->
<tbody id="TableData" class="dataContainer" datakey="userList">
<s:iterator value="#userList">
<tr class="TableDetail1 template">
<td><s:property value="username"/></td>
<td><s:property value="department.name"/></td>
<td>
<s:iterator value="roles">
<s:property value="name"/>
</s:iterator>
</td>
<td><a onClick="return delConfirm()" href="list.html">删除</a>
<a href="saveUI.html">修改</a>
<a href="javascript:privilegeclick();">设置权限</a>
</td>
</tr>
</s:iterator>
</tbody>
</table>
<div id="TableTail">
<div id="TableTail_inside">
<a href="saveUI.html"><img src="${pageContext.request.contextPath}/css/images/createNew.png" /></a>
</div>
</div>
<div class="ItemBlock_Title1" id="userTitle" style="display: none;"><!-- 信息说明 --><div class="ItemBlock_Title1">
<img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif"/>用户:王欣然
<div id="userImage"></div>
</div>
<div class="ItemBlock_Title1" id="privilegeTitle" style="display: none;"><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" style="display: none;" id="privilegeContent">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<!--表头-->
<thead>
<tr align="LEFT" valign="MIDDLE" id="TableTitle">
<td width="300px" style="padding-left: 7px;">
<!-- 如果把全选元素的id指定为selectAll,并且有函数selectAll(),就会有错。因为有一种用法:可以直接用id引用元素 -->
<input type="checkbox" id="allchecked" onchange="privilegeCheckedAll(this.checked,this.id)"/>
<label for="cbSelectAll">全选</label>
</td>
</tr>
</thead>
<!--显示数据列表-->
<tbody id="TableData">
<tr class="TableDetail1">
<!-- 显示权限树 -->
<td>
<ul id='privilegeTree' class="tree">
</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- 表单操作 -->
<div id="InputDetailBar">
<image onclick="savePrivilege()" src="${pageContext.request.contextPath}/css/images/save.png"/>
</div>
</div>
</body>
</html>