一、Spring和Hibernate集成
1、并且通过测试Test
2、把applicationContext.xml分成四个文件
- applicationContext.xm:通用配置、引用下面三类配置
- applicationContext-dao.xml:dao相关配置
- applicationContext-service.xml:service相关配置
- applicationContext-action.xml:action相关配置
3、测试DAO
二、Struts2集成
1、在struts.xml配置通配符
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="default" namespace="/" extends="struts-default">
<!--拦截器栈
在执行prepare拦截器前执行params拦截器
-->
<default-interceptor-ref name="paramsPrepareParamsStack"></default-interceptor-ref>
<!--通配符-->
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="*_*" class="{1}Action" method="{2}">
<result name="list">/WEB-INF/views/{1}/list.jsp</result>
<result name="success" type="redirectAction">{1}</result>
<result name="{2}">/WEB-INF/views/{1}/{2}.jsp</result>
</action>
</package>
</struts>
问题1:通配符无效
<!--通配符-->
<global-allowed-methods>regex:.*</global-allowed-methods>
三、遇到问题
3.1、更新操作时,密码丢失
-
产生情景:因为在编辑用户信息时,没有显示密码输入框,所以在jsp传值到Action中Employee对象时,password为null,如何直接把该Employee对象更新到数据库时,就会造成丢失密码。
-
解决方案:让Action实现 Preparable 接口来拦截saveOrUpdate方法。由于使用默认拦截器栈(defaultStack),prepare拦截器是在params拦截器前,所以在执行prepareSaveOrUpdate的时候是无法拿到参数值,所以需要换一个拦截器栈paramsPrepareParamsStack
/**
* 会拦截所有的方法
* 会在所有action执行前执行
*/
@Override
public void prepare() throws Exception {
}
/**
* 只会在 saveOrUpdate方法执行前执行
* 默认拦截器栈defaultStack,prepare拦截器是在params拦截器前
* 所以在执行prepareSaveOrUpdate的时候是无法拿到参数值,所以
* 需要换一个拦截器栈paramsPrepareParamsStack
*/
public void prepareSaveOrUpdate() {
if (employee.getId() != null) {
employee = employeeService.get(employee.getId());
}
}
3.2、更新操作时,employee对象是如何刷新成为最新数据
package com.revanwang.ssh.rbac.web.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.revanwang.ssh.rbac.domain.Department;
import com.revanwang.ssh.rbac.domain.Employee;
import com.revanwang.ssh.rbac.service.IDepartmentService;
import com.revanwang.ssh.rbac.service.IEmployeeService;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
public class EmployeeAction extends ActionSupport implements Preparable {
private final String LIST = "list";
@Setter
private IEmployeeService employeeService;
@Setter
private IDepartmentService departmentService;
@Getter
private Employee employee = new Employee();
@Override
public String execute() throws Exception {
System.out.println("EmployeeAction.execute");
List<Employee> employees = employeeService.getList();
List<Department> departments = departmentService.getList();
ActionContext.getContext().put("employees", employees);
ActionContext.getContext().put("depts", departments);
return LIST;
}
@Override
public String input() throws Exception {
System.out.println("DepartmentAction.input" + employee);
List<Department> departments = departmentService.getList();
ActionContext.getContext().put("depts", departments);
if (employee.getId() != null) {
employee = employeeService.get(employee.getId());
}
return INPUT;
}
public String delete() {
System.out.println("DepartmentAction.delete" + employee);
if (employee.getId() != null) {
employeeService.delete(employee);
}
return SUCCESS;
}
/**
* 保存或者更新
*/
public String saveOrUpdate() {
System.out.println("DepartmentAction.saveOrUpdate" + employee);
if (employee.getId() == null) {
//新增Employee
employeeService.save(employee);
} else {
//更新Employee
employeeService.update(employee);
}
return SUCCESS;
}
/**
* 会拦截所有的方法
* 会在所有action执行前执行
*/
@Override
public void prepare() throws Exception {
}
/**
* 只会在 saveOrUpdate方法执行前执行
* 默认拦截器栈defaultStack,prepare拦截器是在params拦截器前
* 所以在执行prepareSaveOrUpdate的时候是无法拿到参数值,所以
* 需要换一个拦截器栈paramsPrepareParamsStack
*/
public void prepareSaveOrUpdate() {
System.out.println("EmployeeAction.prepareSaveOrUpdate_1:" + employee);
if (employee.getId() != null) {
System.out.println("EmployeeAction.prepareSaveOrUpdate_2:" + employeeService.get(employee.getId()));
employee = employeeService.get(employee.getId());
System.out.println("EmployeeAction.prepareSaveOrUpdate_3:" + employee);
}
}
}
- 输出打印
EmployeeAction.prepareSaveOrUpdate_1:Employee{id=7, name='Revan_W', password='null', email='Revan@qq.com', age=18, admin=true, department=Department{id=2, name='null', sn='null'}}
Hibernate:
select
employee0_.id as id1_1_0_,
employee0_.name as name2_1_0_,
employee0_.password as password3_1_0_,
employee0_.email as email4_1_0_,
employee0_.age as age5_1_0_,
employee0_.admin as admin6_1_0_,
employee0_.depart_id as depart_i7_1_0_
from
Employee employee0_
where
employee0_.id=?
Hibernate:
select
department0_.id as id1_0_0_,
department0_.name as name2_0_0_,
department0_.sn as sn3_0_0_
from
Department department0_
where
department0_.id=?
EmployeeAction.prepareSaveOrUpdate_2:Employee{id=7, name='Revan', password='1', email='Revan@qq.com', age=28, admin=true, department=Department{id=2, name='???', sn='Z01'}}
EmployeeAction.prepareSaveOrUpdate_3:Employee{id=7, name='Revan', password='1', email='Revan@qq.com', age=28, admin=true, department=Department{id=2, name='???', sn='Z01'}}
DepartmentAction.saveOrUpdateEmployee{id=7, name='Revan_W', password='1', email='Revan@qq.com', age=18, admin=true, department=Department{id=2, name='???', sn='Z01'}}
Hibernate:
update
Employee
set
name=?,
password=?,
email=?,
age=?,
admin=?,
depart_id=?
where
id=?