OA的Action.Service.dao一些细节

Spring的声明式事务处理,在框架搭建的时候已经搭建完成;

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="delete*" read-only="false"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut expression="execution(* com.itcast.oa.service.impl.*.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
上面的意思是将所有的execution(* com.itcast.oa.service.impl.*.*(..))进行事务处理声明;
Spring是对各部分进行管理操作;例如Action对Service的引用(使用哪个实现类),Service使用哪个Dao实现类,这些都是
实现了面向切面编程;管理实现的类;

测试Spring:
SessionFactory测试:
public class SpringInit {
public static ApplicationContext context;
static{
context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
}

public class SessionFactoryTest extends SpringInit{
@Test
public void testSessionFactory(){
SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
}
}

然后是Dao Service
Dao:getAllXxx getXxxById updateXxx saveXxx deleteXxx
Serialiable id
Dao extends HibernateDaoSupport
这里如果异常控制太死的话,异常就很难打印出来了,比如传进id都要判断是否为null,这时候出现的问题是,异常就不能
打印出来;所以在dao一般不做复杂的异常操作,也就是不进行throws Exception

@Override
public void deleteDepartment(Serializable id) throws Exception {
if(id==null){
throw new RuntimeException("在进行删除的时候id不能为null");
}
Department department = (Department)this.getHibernateTemplate().get(Department.class, id);
this.getHibernateTemplate().delete(department);
}
这样也可以,不过不能很明确具体的问题;
Spring切面变成的注册具体实现类;
这里单独用一个文件写:
<bean id="departmentDao" class="com.itcast.oa.dao.impl.DepartmentDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<bean id="departmentService" class="com.itcast.oa.service.impl.DepartmentServiceImpl">
<property name="departmentDao">
<ref bean="departmentDao"/>
</property>
</bean>

<bean id="departmentAction" class="com.itcast.oa.struts2.action.DepartmentAction"
scope="prototype">
<property name="departmentService">
<ref bean="departmentService"/>
</property>
</bean>
Dao Service Action 注册;

然后单元测试是否实现这个流程;
public class DepartmentTest extends SpringInit{
@Test
public void testSaveDepartment() throws Exception{
DepartmentService departmentService = (DepartmentService)context.getBean
("departmentService");
Department department = new Department();
department.setDname("研发部");
department.setDescription("都是程序员");
departmentService.saveDeparment(null);
}
}

---------------
Action
extends ActionSupport
页面,添加 修改 列表 等各个属性都具有,可以写一个BaseAction ;
public class BaseAction extends ActionSupport{

public static String LISTACTION = "listAction";

/**
* 跳转到列表页面
*/
public static String listAction = LISTACTION;

/*
* 跳转到添加页面
*/
public static String ADDUI = "addUI";

public static String addUI = ADDUI;

/**
* 跳转到修改页面
*/
public static String UPDATEUI = "updateUI";

public static String updateUI = UPDATEUI;

/**
* action跳转到action
*/
public static String ACTION2ACTION = "action2action";

public static String action2action = ACTION2ACTION;
}

action跳转到action两种方式:chain和redirectAction ;
区别:
一个action跳转到另一个action 跳转方式为chain 会保留request对象,redirectAction 则不能保留

struts2 从一个action 跳转到另一个action的两种方法:
http://liminhappygirl.iteye.com/blog/1290340

从action1直接跳转到action2,有两种方法:

1. 需要保存前一个action的属性信息时使用,保存住action1的request对象:
<result type= "chain " name="a2">action2</result>
2. 不保存前一个action的参数可以用这种方法:
<result type= "redirectAction "> action2</result>
Action里面用Service,这里set和get方法,就可以使用配置文件声明注入;

控制反转/依赖注入:http://www.blogjava.net/yejiansuo/archive/2010/10/18/335440.html
控制反转是早期叫法,后来改为依赖注入,更贴切;

public String showAllDepartment() throws Exception{
Collection<Department> dList = this.departmentService.getAllDepartment();
//ActionContext.getContext().put("dList", dList);
ActionContext.getContext().getValueStack().push(dList);
return listAction;
}

public String add() throws Exception{
/**
* struts2的模型驱动的作用两个
* 1、获取页面表单的数据
* 2、对数据进行回显
* 说明:最好不要把模型驱动的对象和dao层发生交互
*/
Department department = new Department();
BeanUtils.copyProperties(this.getModel(), department);
this.departmentService.saveDeparment(department);
return action2action;
}
public String delete() throws Exception{
this.departmentService.deleteDepartment(this.getModel().getDid());
return action2action;
}
public String updateUI() throws Exception{
Department department = this.departmentService.getDepartmentByID(this.getModel().getDid());
ActionContext.getContext().getValueStack().push(department);
return updateUI;
}

public String update() throws Exception{
/**
* 根据ID把原来的值提取出来
*/
Department department = this.departmentService.getDepartmentByID(this.getModel().getDid());
/**
* 把模型驱动中最新的值赋值给dpearment
*/
BeanUtils.copyProperties(this.getModel(), department);
/**
* 修改department
*/
this.departmentService.updateDepartment(this.getModel());
return action2action;
}


-----
session这部分是需要用用户登录;这里需要先将权限做好,然后登录就可以得到对应的菜单;
异常处理部分的代码:
<struts>
<!-- 配置文件改了以后不用重新启动 -->
<constant name="struts.devMode" value="true"/>
<constant name="struts.ui.theme" value="simple"/>

<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="com.itcast.oa.exception.ErrorPrcessor">
<result>error.jsp</result>
</action>
</package>

<include file="struts2/struts-department.xml"></include>
</struts>

使用Spring进行统一日志管理+++统一异常管理+:
http://wenku.baidu.com/link?url=5k0ru12Sy-
p9jtMO3N_4R26lOyEO26QWgl5WqfOSuKQsu2JM4eDpCisvSjluwMxwcaBM5eT2sjkXg4WnD2XB4_RxfuWvN0MTCKvs4YSTW-W

Spring3.0异常处理配置:
http://wenku.baidu.com/link?
url=NCvhBJR2mNWVBY9hRjveDg9rn18xeWGycQ1fld90N1mfD98JAawv3M7cuqxhYEwkM2e1aNAQxtPrjVCRtY5GwXi6ziNTs8Y94NSTMJuX
LyS
Spring3.0中对异常的处理方法一共提供了两种:
一种是使用HandlerExceptionResolver接口;
一种是在Controller类内部使用@ExceptionHandler注解。
使用第一种方式可以实现全局异常控制,并且Spring已经提供了一个默认的实现类
SimpleMappingExceptionResolver;
使用第二种方式可以在Controller内部实现更个性化点异常处理方式,灵活性更高。一般来说,项目中只需要采用第一种方
式就可以了,每个人都自己定义异常的展现方式,太过个性了,不统一。
两种方法不能共存;


public class ErrorPrcessor extends ActionSupport{
private Exception exception;

public Exception getException() {
return exception;
}

public void setException(Exception exception) {
this.exception = exception;
}

@Override
public String execute() {
ActionContext.getContext().getValueStack().push(
this.exception.getMessage());
return SUCCESS;
}
}
这个Action不需要放进Spring容器,因为不需要使用Session;

error.jsp:
<body>
This is my JSP page. <br>
<s:property/>
</body>

配置Action:
<package name="department" namespace="/" extends="struts-global">
<action name="departmentAction_*" method="{1}" class="departmentAction">
<result name="listAction">WEB-INF/jsp/department/list.jsp</result>
<result type="redirectAction"
name="action2action">departmentAction_showAllDepartment.action</result>
<result name="addUI">WEB-INF/jsp/department/add.jsp</result>
<result name="aaa">WEB-INF/jsp/department/update.jsp</result>
</action>
</package>

页面的修改和数据的迭代;
注意方法名不要使用getXxx,这样容易出现注入的问题;
比如同时使用标签<s:debug/>,这时候报错是:java.util.ConcurrentModificationException并发不能修改;
而如果没有使用<s:debug/>标签,就不会出错,这是属于标签的bug;
原因:<s:debug/>标签回去自动过滤栈顶中的属性,如果使用push(Object),action正好在栈顶,值栈就会根据getXxx去解
析属性;而这个getXxx可能解析不了,比如方法里面与User,还需要将其解析,而User可能涉及到懒加载,就会报错;
因此,起名为get方法是有安全性隐患的;

posted @ 2014-02-27 17:53  教程学习  阅读(168)  评论(0)    收藏  举报