跟着汤阳光同志做一个OA系统(八):抽取页面公共部分、提取BaseAction、ModelDriven支持、Service和DAO合并、BaseAction中Service声明、非常好的增删改查流程

来源:互联网 发布:最好的手机网络防火墙 编辑:程序博客网 时间:2024/04/29 17:32

@一些改进:

页面公共部分可以合并

通过<%@includefile="/WEB-INF/jsp/public/commons.jspf"%>

这个页面中,下面两句话都是可以包含的,因为@include先合并再解析

<%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>

<%@ taglib prefix="s"uri="/struts-tags"%>

<metahttp-equiv="Content-Type"content="text/html; charset=utf-8" />

<scriptlanguage="javascript"src="${pageContext.request.contextPath}/script/jquery.js"></script>

<scriptlanguage="javascript"src="${pageContext.request.contextPath}/script/pageCommon.js"charset="utf-8"></script>

<scriptlanguage="javascript"src="${pageContext.request.contextPath}/script/PageUtils.js"charset="utf-8"></script>

<linktype="text/css"rel="stylesheet"href="${pageContext.request.contextPath}/style/blue/pageCommon.css"/>

<scripttype="text/javascript">

</script>

1,Action:增加BaseAction

•    我们所写的Action都继承这个类

public abstract classBaseAction<T> /*extends ActionSupport*/ implements ModelDriven<T> {

 

         // =============== ModelDriven的支持 ==================

 

         protected T model;

 

         public BaseAction() {

                   try {

                            // 通过反射获取model的真实类型

                            ParameterizedType  pt =

(ParameterizedType)this.getClass().getGenericSuperclass();

                            Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];

                            // 通过反射创建model的实例

                            model = clazz.newInstance();

                   } catch (Exception e) {

                            throw new RuntimeException(e);

                   }

         }

 

         public T getModel() {

                   return model;

         }

 

         // =============== Service实例的声明,本身自己是单实例==================

         @Resource

         protected RoleService roleService;

         @Resource

         protected DepartmentService departmentService;

         @Resource

         protected UserService userService;

        

 

}

2,Service:合并Service与Dao

•    使用两层

•    Service可以直接使用hibernateSession操作实体

public List<Department> findTopList() {

       returnsessionFactory.getCurrentSession().createQuery(//

              "FROMDepartment d WHERE d.parent IS NULL")//

              .list();

    }

 

    public List<Department> findChildren(Long parentId) {

       returnsessionFactory.getCurrentSession().createQuery(//

              "FROMDepartment d WHERE d.parent.id=?")//

              .setParameter(0, parentId)//

              .list();

    }

3,JSP页面:

•    使用公共的页面,通过静态包含导入公共的代码

<%@ includefile="/WEB-INF/jsp/public/public.jspf"%>

•    addUI.jsp与editUI.jsp基本一样时可以合并为一个【使用隐藏字段id区别】

找相同点和不同点。相同的地方太多的话就可以抽取;而不同点就是区别两个的根据,越简单越好。

 

@流程总结:实现增删改查一组功能的步骤

 

一、做Action相关的准备:

                        ActionJSP、配置[springstruts.xml的配置]

1,创建MyAction extends BaseAction.

2,定义出Action中的方法,要写出方法名、作用、返回值。

3,创建出所用到的JSP页面(目前还没有具体内容)。

4,配置Action:

                        1,在MyAction上写注解 @Controller与@Scope("prototype").

                        2,在strtus.xml中配置这个Action与所用到的result.

 

二、做Service相关的准备:

                        接口、实现类、配置[spring]

1,创建接口MyService extends BaseDao.

2,创建实现类MyServiceImpl extends BaseDaoImpl.

3,配置:在MyServiceImpl上写注解:

                             @Service与 @Transactional

4,声明:在BaseAction中声明:
@Resource protected MyService myService;

 

三、填空:

                        Action方法、Service方法、JSP页面

1,Action方法。

2,新增的Service方法。

3,JSP页面的内容:

                        a,拷贝静态页面中的源代码到JSP中。

                        b,包含进来公共的资源:

                  <%@ includefile=“../public/commons.jspf" %>

                        c,把 ../ 替换为 ${pageContext.request.contextPath}/

                        d,修改页面内容(使用自定义标签)

 

alt+shift+a选择矩形区域,而不是常规选择

ctrl+T打开某个类的层次

ctrl+shift+o导入所有的包

 

 

@Transactional

         可以写在方法上。

                   对本方法有效

         可以定在类上。

                   对本类中所有public方法有效。

                   对子类的中方法有效。

                   对父类声明的方法无效。

所以如果在父类中没有声明,在子类中声明,造成删除这样的操作无效

 

Collections.EMPTY_LIST返回一个空集合,可以避免返回null造成调用的时候异常,而如果使用new ArrayList返回的是10个元素的数组,浪费。

 

数据库存MD5加密密码,可以防止数据库管理员直接查看,传过去的明文密码经过加密后与数据库中的比对。MD5是不可逆的,128bit---16byte---32Hex。既可以使用JDK中的MessageDigest类,也可以使用Apachecommons-codec包中的DigestUtils.md5Hex("1234");


0 0