package com.myframe.ssh.action;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.myframe.ssh.pojo.Department;
import com.myframe.ssh.pojo.Employee;
import com.myframe.ssh.service.EmployeeService;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
@Controller
@Scope("prototype")
public class EmployeeAction implements RequestAware, ModelDriven<Employee>,
Preparable {
@Autowired
private EmployeeService employeeService;
private Map<String, Object> request;
private Employee employee;
private Integer empId;
public String delete() {
employeeService.deleteEmp(empId);
return "list";
}
public void prepareDoupdate() {
//employee = new Employee();
employee = employeeService.getEmp(empId);
}
public String doupdate() {
employeeService.updateEmp(employee);;
return "list";
}
/**
* 需要在前缀方法执行之前,执行参数拦截器
*/
public void prepareToupdate() {
employee = employeeService.getEmp(empId);
}
public String toupdate() {
List<Department> deptList = employeeService.getAllDept();
request.put("deptList", deptList);
return "update";
}
public void prepareDosave() {
employee = new Employee();
}
// 保存功能使用模型驱动
public String dosave() {
employeeService.saveEmp(employee);
return "list";
}
public String tosave() {
List<Department> deptList = employeeService.getAllDept();
request.put("deptList", deptList);
return "save";
}
public String getAll() {
// 要想保存到request中必须实现RequestAware
// 要想在页面获取必须实现XxxAware
List<Employee> empList = employeeService.getAllEmp();
request.put("empList", empList);
return "success";
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
/**
* 框架会将表单提交的数据自动封装到我们准备的模型对象中(使用参数拦截器),同时也可以在页面获取模型对象中的值(它使用getModel保存在对象栈中)
* 将模型对象压入栈中,注:对象不能是null,若是null不进行压栈工作
* 因为getModel方法每次处理请求之前都会执行,因此不在这个方法中给模型对象赋值 使用前缀方法
*/
@Override
public Employee getModel() {
return employee;
}
/**
* 它也是每个请求都会执行,因此也不在这个里面赋值
*/
@Override
public void prepare() throws Exception {
System.out.println("prepare....");
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<!-- 这里的class可以直接使用类名首字母小写 -->
<package name="default" namespace="/" extends="struts-default">
<default-interceptor-ref name="paramsPrepareParamsStack"/>
<action name="getAll" class="com.myframe.ssh.action.EmployeeAction" method="getAll">
<result>list.jsp</result>
</action>
<!-- 向保存页面传递一些部门信息 -->
<action name="tosave" class="com.myframe.ssh.action.EmployeeAction" method="tosave">
<!-- 到保存页面 -->
<result name="save">save.jsp</result>
</action>
<action name="dosave" class="com.myframe.ssh.action.EmployeeAction" method="dosave">
<result type="redirectAction" name="list">
<param name="actionName">getAll</param>
<param name="namespace">/</param>
</result>
</action>
<action name="toupdate" class="employeeAction" method="toupdate">
<result name="update">update.jsp</result>
</action>
<action name="doupdate" class="employeeAction" method="doupdate">
<result type="redirectAction" name="list">getAll</result>
</action>
<action name="delete" class="employeeAction" method="delete">
<result type="redirectAction" name="list">getAll</result>
</action>
</package>
</struts>
二级缓存配置
可以配置一个二级缓存对性能进行优化(hibernate配置文件中配置)
<!-- 使用二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 配置一个缓存工厂组件 -->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 指定那些类使用二级缓存 -->
<class-cache usage="read-only" class="com.myframe.ssh.pojo.Department"/>
SSH框架搭建完成,CRUD功能完成!!!