一个关于freemarker+webwork+spring+ibatis的demo文档

    技术2022-05-11  137

    1 任务 1.背景 利用所学框架完成以下系统: 我公司需要对所有固定资产(包括电脑、桌椅、书籍等办公设备)分门别类进行登记管理。系统涉及角色包括: 系统管理员,固定资产管理员,办公室主任,普通员工,中层管理人员,高层管理人员。 2.功能 系统完成功能包括固定资产清单管理(可以按公司、部门、个人查看), 固定资产出借管理(可以按公司、部门、个人查看), 3.规则 固定资产净值管理(固定资产分5年分摊,每年递减最初的20%), 资产额度大于10000的出借需要办公室主任批准,其他出借只需资产管理员登记即可。普通员工借用数量不能大于5件,中层管理人员不能大于10件,高层管理人员无限制。 此处件数为累积数量 2 使用框架和环境 工具: eclipse 3.2 + jdk 1.5 + Maven 2.0 + jetty-5.1 + Mysql 5.0 框架: freemaker + webwork + spring + ibatis 3 数据库 1.数据库使用规范 V0.1 001 1. 数据库定义中所有标识符必须标识符以英文字母开头,由一个或多个英文单词构成,单词构成为名词或动宾结构,每个单词之间必须以"_"分隔(数字除外)。 如     field_name     col_name1 2. 每一个数据库字段必须定义缺省值(Blob等大字段除外)。 如     product_name [VARCHAR] (100) NULL default '' product_type [INTEGER] NULL default 0, product_date [DATETIME] NULL default sysdate, 3. 所有数据表中必须有id字段,id字段长度为10个字符(用户帐号表除外),id字段是该表的主键,且需要对id字段作索引。id字段内容由数字字母串构成,区分大小写。 4. 所有数据表中必须有flag字段,flag字段为integer型,flag取值含义:0表示该记录未经审核,1表示已经审核通过,2表示该记录已经被锁定(审核未通过),3表示该记录当前还没有被激活,10表示该记录已经被删除。 5. 所有实体表(如用户表、产品表等表示具体事务属性的表)中必须包含以下字段(关联表可以不包含): modifier_id [varchar] (50) NULL default '', modify_date [datetime]       NULL default sysdate, creator_id    [varchar] (50) NULL default '', create_date [datetime]       NULL default sysdate, 6. 所有实体表前必须加前缀tbl_,关联表前必须加前缀jnt_,索引加后缀_ndx,存储过程为sp_projectname_ CREATE INDEX fzpl.User_Corp__Corp_ID__ndx ON fzpl.Jnt_User_Corp(Corp_ID) TABLESPACE indx; 2.数据库设计 Mysql 区分大小写  全部小写 Create database goods; 1. 固定资产 tbl_goods Create table tbl_goods(        Goods_id      varchar (20)   NOT NULL default '', Goods_name     varchar (100)  NOT NULL default '', Goods_value    long          , Goods_date      datetime, Goods_number    int           default 1, PRIMARY KEY (Goods_id ) ) 2.物品分类 jnt_goods_type Create table jnt_goods_type( Goods_type     varchar (100)   NOT NULL default '', Goods_id        varchar (20)   NOT NULL default '', )   3. 角色  tbl_role Create table tbl_role( role_id       int            //NOT NULL default 0, role_name    varchar (100)    NOT NULL default '', PRIMARY KEY (role_id ) ) INSERT INTO tbl_role (role_id , role_name ) VALUES  (1,'系统管理员 '),  (2,’ 固定资产管理员 '),  (3,'办公室主任 '),  (4,'高层管理人员 '), (5,'中层管理人员 '), (6,'普通员工 ') ; 4.用户 tbl_user Create table tbl_ user(    User_id           varchar (20)    NOT NULL default '',    User_name         varchar (20)    NOT NULL default '', PRIMARY KEY (`User_id`) ) 5用户——角色 jnt_staff_manage Create table jnt_ staff_manage( User_id           varchar (20)    NOT NULL default '', role_id             int          NOT NULL default 6, User_company      varchar (100)  NOT NULL default '', User_ department varchar (100)  NOT NULL default '', ) 6.规则 tbl_rule       Create table tbl_ rule( Rule_name       varchar (50)    NOT NULL default '', Rule_value       varchar (50)    NOT NULL default '' ) 如: 固定资产净值管理(固定资产分5年分摊,每年递减最初的20%) 资产额度大于10000的出借需要办公室主任批准,其他出借只需资产管理员登记即可。 普通员工借用数量不能大于5件,中层管理人员不能大于10件,高层管理人员无限制。 7. 固定资产出借管理 jnt_goods_user Create table jnt_goods_ user ( Goods_id           varchar (20)   NOT NULL default '', User_id            varchar (20)    NOT NULL default '', Lend_time           datetime, Back_time          datetime, Plan_time           datetime,         //计划归还时间 Goods_number       int            default 1, Back_number        int ,           //default 0 Goods_status         int       //0:未归还 1:部分归还 2:全部归还    3 :等待办公室主任批准  ) * 此表作为记录 不能有删除动作! 7. 固定资产出借管理临时表 jnt_goods_user_wait 同jnt_goods_ user  ,作为临时 记录使用   8.物品登记 jnt_goods_status (?) Create table jnt_goods_status(  Goods_id           varchar (20)    NOT NULL default '', Goods_status        char           NOT NULL default '0'  // Goods_status //0 :没有借出  //1 :已全部借出 //2 :部分借出 ) 9.固定资产所属(公司、部门、个人) jnt_goods_belong Create table jnt_goods_belong( Goods_id          varchar (20)   NOT NULL default '', User_id           varchar (20), User_company      varchar (100), User_ department varchar (100), ) //固定资产属于(公司、部门、个人),以最小单位记, //如 个人优于部门,部门优于公司 10.部门tbl_department Create table tbl_department( //------联合主键 Department_name varchar (100) not null default ‘’,    Department_company varchar (100) not null default ‘’,    //other message ) 11.公司tbl_company Create table tbl_company( //------主键    Company_name varchar (100) not null default ‘’,    //other message )   4 业务逻辑 1.资产登记: 所有固定资产(包括电脑、桌椅、书籍等办公设备)分门别类进行登记管理 逻辑 目标 优先级 查看 详细信息 1 增加 信息 2 修改   2 删除 信息 3 2.固定资产出借管理: 固定资产出借管理(可以按公司、部门、个人查看), 逻辑 目标 优先级 租借信息查看 详细信息 1 借 信息 2 归还 数量 2 规则限制的租借处理   3       5 设计(以主要业务逻辑为例) 1.资产登记  以GoodsService为主线 逻辑 方法 增加 GoodsDao public void create(Goods goods) 创建一种物品(资产)信息 查看 GoodsDao GoodsTypeDao GoodsBelongDao Public Collection findAll( ) 查看所有种类物品(资产)信息 通过类型来查看此类物品(资产)中所有种类物品(资产)信息 public Collection findByType(GoodsType goodsType GoodsTypeService Public Goods getById(String id) : GoodsService Public Goods getById(String id) 查看一种物品(资产)信息 固定资产属于(公司、部门、个人) 多条件查询 public Collection findGoodsBelong(GoodsBelong goodsBelongGoodsBelongService Public Goods getById(String id) : GoodsService public User getById(String userid) UserService 修改 GoodsDao GoodsTypeDao Public void modifyById(Goods goods) 修改物品(资产)信息 public void modifyTypeById(GoodsType goodsType) : GoodsTypeService 修改物品(资产)所属的类型 删除 GoodsDao GoodsTypeDao GoodsBelongDao GoodsStatusDao (?) Public void remove(String id) 删除物品(资产)信息-- à 删除所属类型中的关系 固定资产所属 删除物品登记的记录 (?)         Service 1.         GoodsService 2.         GoodsTypeService 3.         GoodsBelongService 4.         UserService Dao 1.         GoodsDao 2.         GoodsTypeDao 3.         GoodsStatusDao (?) table 1.         tbl_goods 2.         jnt_goods_type 3.         jnt_goods_status (?) 4.         jnt_goods_belong 其他 *ConstantDefine   1.         jnt_goods_type . goods_type    定义物品分类的类别常量 2.         (?) goods_status //0 :没有借出 1 : 已借出 2 :部分借出 2.固定资产出借管理 以GoodsUserService为主线 逻辑 方法 查看 GoodsUserDao GoodsDao UserDao   根据条件查看所有物品(资产)的借用信息 ( 暂时先不支持时间的查询) public Collection findGoodsUser(GoodsUser goodsUser) :GoodsUserService public Goods getById(String id)GoodsService public User getById(String userid) UserService 借出(创建) GoodsUserDao RuleDao(?) StaffManageDao RoleDao(?) Public void createAction(GoodsUser goodsUser ) 创建借用行为:物品(资产)的借用记录 Lend_time = now() Plan_time 可以为null   固定资产净值管理(固定资产分5年分摊,每年递减最初的20%)   资产额度大于10000的出借需要办公室主任批准,其他出借只需资产管理员登记即可。 普通员工借用数量不能大于5件,中层管理人员不能大于10件,高层管理人员无限制。   归还(修改) GoodsUserDao Public void removeAction(GoodsUser goodsUser ) 取消借用行为:物品(资产)的归还记录 GoodsUser: goodsid + userid + backnumber 通过 goodsid + userid查找对应信息 Lend_time 不能改变 Plan_time 不能改变 Back_time = now() Back_number [1]+ Back_number[2]+….<=  Goods_number                     Service   Dao 1.         GoodsUserDao 2.         GoodsDao 3.         RuleDao 4.         StaffManageDao 5.         RoleDao table 1.         jnt_goods_status 2.         tbl_goods 3.         jnt_ staff_manage 4.         tbl_role 5.         tbl_ rule 其他 *ContextDefine  1.         tbl_role .role_name 定义角色的常量                  (1,'系统管理员 '),                  (2,’固定资产管理员 '),                  (3,'办公室主任 '),                  (4,'高层管理人员 '), (5,'中层管理人员 '), (6,'普通员工 ') 2.        tbl_ rule Rule_name , Rule_value // 定义规则常量 固定资产净值’, 0.2 (‘资产额度’, 10000 ) (‘普通员工’,5 ) (‘中层管理人员’,10 ) //   (‘高层管理人员’,0 ) ---- à 代表无穷大,即不做为限制规则 3. *其他 所有页面显示id 均由超链接处理 *日志 2007/1/8 数据库 项目框架 2007/1/9 行为 目标 Add 物品登记 table jnt_goods_status (?是否保留) Update 出借管理 table jnt_goods_ user + Back_number        int            //default 0 + Plan_time           datetime,         //计划归还时间 Add 固定资产所属table jnt_goods_belong Finish Goods                   Finish GoodsType Finish GoodsBelong Finish User             2007/1/10 行为 目标 update alter table jnt_goods_user drop column goods_number; alter table jnt_goods_user add column goods_number int default 1;             2007/1/11 update alter table tbl_goods drop column goods_date; alter table tbl_goods add column goods_date datetime; update alter table jnt_goods_ user drop column lend_time; error:alter table jnt_goods_ user add column lend_time datetime NULL default CURRENT_TIMESTAMP; alter table jnt_goods_ user add column lend_time datetime; update alter table tbl_goods drop column goods_number; alter table tbl_goods add column goods_number    int           default 1;             2007/1/12 update StaffManageDaoIbatis : getByUserId // 数据库设计的缺陷:有多个值只取第一个 //getSqlMapClientTemplate().queryForObject("StaffManage_getByUserId", userid);----old Collection col = getSqlMapClientTemplate().queryForList( "StaffManage_getByUserId" , userid);        if (col!= null ){            return (StaffManage)col.iterator().next();        }        return null ; update alter table jnt_goods_ user add column status int delete 物品登记 jnt_goods_status update alter table jnt_goods_ user drop column status; alter table jnt_goods_ user add column goods_status int Finish GoodsAction Finish GoodsUserAction Finish StaffAction             2007/1/15 2007/1/18 add Create table tbl_department( //------联合主键 Department_name varchar (100) not null default ‘’,    Department_company varchar (100) not null default ‘’,    //other message ) add Create table tbl_company( //------主键    Company_name varchar (100) not null default ‘’,    //other message )         2007/1/22 modify 注意数据库中关联表的设计 : 不能设置外界关系 , 当相关数据不存在时 , 作为已经有过的操作行为不应该被删除 !!         2007/1/24 add Table jnt_goods_ user_wait       *问题解决方案 数据库连接 资源文件的格式问题——ctrl+shift+f : 格式化时可能导致错误 粘贴或手写不要使用快速格式化 时间 1>数据库: Create table tbl_goods( /..         Goods_date      datetime, /.. )   2>Model Goods.java private Date goodsdate ;   3>goodsInsert.ftl <@ww.head/> <@ww.datepicker label= " ${action.getText( 'goods.date' )} " name="model.goodsdate" showstime="true"/>   4> java 文件中的测试 Calendar c = Calendar.getInstance(); Date init = goods.getGoodsdate(); Date now = c.getTime();   DateFormat df = DateFormat.getDateInstance(); Calendar c2 = df.getCalendar(); System. out .println( "time:" +c2.getTime().toString()+ "!" ); System. out .println( "time:" +df.format(now)+ "!" ); 中文编码问题 l         mysql     普通方式建立数据库时,采用的默认编码方式可能出现乱码 建立数据库和表时,设置编码方式 如: CREATE DATABASE goods     CHARACTER SET 'utf8' COLLATE 'utf8_swedish_ci' ;   CREATE TABLE tbl_user (  user_id varchar(20) NOT NULL default '',  user_name varchar(20) NOT NULL default '',  PRIMARY KEY (`user_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;     l          通过硬编码 中文编码 : 8859_1 中文编码方式 : GB2312 String newStr = new String(oldStr.getBytes("GB2312"),"8859_1"); // 存储  String newStr2=new String(oldStr.getBytes("8859_1"),"GB2312");  显示   l         jsp 1>        request.setCharacterEncoding("GB2312") 2>        <%@ page contentType="text/html;charset=gb2312" %> 3>        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">    l          java String name=java.net.URLEncoder.encode("21cn 游戏频道说明文档 .txt","utf-8") l          修改 web 服务器的配置,以 tomcat 为例。 如果要求 tomcat 服务器识别中文的 get 参数,则需要修改 conf/server.xml 的文件配置。如下  <C port="8080"               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"               enableLookups="false" redirectPort="8443" acceptCount="100"               debug="0" c                disableUploadTimeout="true" URIEncoding="GBK"/> 国际化 1、 webwork.properties 框架文件 定义: webwork.custom.i18n.resources= ApplicationContext   2 ApplicationContext 是资源文件 如: ApplicationContext.properties 定义: system.title            =   /u56FA/u5B9A/u8D44/u4EA7/u7BA1/u7406/u7CFB/u7EDF system.title= 固定资产管理系统)   3 、页面中取得 如:页面 login.ftl <h1>${action.getText( 'system.title' )}</h1><br/>   4 a> 资源文件 ApplicationContext.properties login = welcome {0} login in: b> 页面 login.ftl <@ww.text name="${action.getText('login')}"> <@ww.param> <fontcolor= "red" > ${model.username} </font> </@ww.param> /@ww.text c> 效果 welcome test_user login in: 错误提示 1、 webwork.properties 框架文件 定义: webwork.custom.i18n.resources= ApplicationContext   2 ApplicationContext 是资源文件 如: ApplicationContext.properties 定义: error.password             。。。。。 error.password = 。。。。 3 、应用如: 代码中:addActionError(getText("error.password")); 将错误信息放入错误栈中   4 、页面判断错误栈中是否有信息,有就显示 <#if action.getActionErrors().size() != 0>  <font color="red">      ${action.getAc tionErrors()}  </font> </#if>     /** *this .addActionError( "error: 没有分配的对象 !" ); * --this.addActionError(this.getText(aTextName));-- */ ibatis   配置文件中 sql模糊查询 e.g:   file [ Company.xml ] < select id = "Company_findCompany" parameterClass = "Company"        resultClass = "Company" >        <![CDATA[        select         company_name as companyname        from tbl_company        ]]>        < dynamic prepend = "where" >            < isNotEmpty prepend = "AND" property = "companyname" >               company_name like '%$companyname$%'            </ isNotEmpty >        </ dynamic >     </ select > url传递中文问题 通过其他方法解决!! 在url中通过传递中文name来删除company 和 department 的问题没有解决! 通过form表单提交的方式来处理 如: <input type="radion" name="model.companyname" value="${x.companyname?if_exists}"/> 注意:value="${x.companyname?if_exists}" 一定要带上“""” 不然会在返回值后自动加上“/”   在对department的删除过程中 <input type="radion" name="model.departmentcompany" value="${x.departmentcompany?if_exists}/${x.departmentname?if_exists}"/> 将departmentcompany和departmentname组装到一个变量中进行提交,然后拆分:代码如下 String tempCAndD = department.getDepartmentcompany(); if(tempCAndD!=null){  String[] CAndD = tempCAndD.split("/");  department.setDepartmentcompany(CAndD[0]);  department.setDepartmentname(CAndD[1]);  departmentService.remove(department); }else{  throw new Exception("notice the companyname and departmentname!!"); } Webwork 中session的使用 e.g: (代码中) java 文件中: UserAction.java Map session = ActionContext.getContext().getSession(); session.put( "username" , temp.getUsername());   ( 应用 ) 页面文件中: login.ftl <#if username?exists>            系统管理员         <@ww.text name="${action.getText('login')}">            <@ww.param><font color="red">${username}</font></@ww.param>         </@ww.text>         <#else>            ${action.getText('system.welcome')}        </#if> 注意: 在联合使用 freemaker (如上代码), [ login.ftl] username = [UserAction.java] session.get("username") Freemarker数字问题 Interpolation 有两种类型: 1.   通用 Interpolation ${expr} 2.   数字 Interpolation #{expr} #{expr; format} 注意: Interpolation 只能用于文本部分 n         通用 Interpolation 插入字符串值:直接输出表达式结果 插入数字值:根据缺省格式(由 #setting 指令设置)将表达式结果转换成文本输出;可以使用内建函数 string 格式化单个 Interpolation ,下面是一个例子: <#setting number_format="currency"/><#assign answer=42/>${answer}${answer?string} <#-- the same as ${answer} -->${answer?string.number}${answer?string.currency}${answer?string.percent} 输出结果是: $42.00$42.0042$42.004,200% 插入日期值:根据缺省格式(由 #setting 指令设置)将表达式结果转换成文本输出;可以使用内建函数 string 格式化单个 Interpolation ,下面是一个使用格式模式的例子: ${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}${lastUpdated?string("EEE, MMM d, ''yy")}${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}  输出的结果类似下面的格式: 2003-04-08 21:24:44 Pacific Daylight TimeTue, Apr 8, '03Tuesday, April 08, 2003, 09:24:44 PM (PDT) 插入布尔值:根据缺省格式(由 #setting 指令设置)将表达式结果转换成文本输出;可以使用内建函数 string 格式化单个 Interpolation ,下面是一个例子: <#assign foo=true/>${foo?string("yes", "no")} 输出结果是: yes ·       数字 Interpolation #{expr; format} 形式可以用来格式化数字, format 可以是: mX :小数部分最小 X MX :小数部分最大 X 例子: <#-- If the language is US English the output is: --><#assign x=2.582/><#assign y=4/>#{x; M2}   <#-- 2.58 -->#{y; M2}   <#-- 4    -->#{x; m1}   <#-- 2.6 -->#{y; m1}   <#-- 4.0 -->#{x; m1M2} <#-- 2.58 -->#{y; m1M2} <#-- 4.0 -->   *参考资料 1>MySQL中文参考手册       http://it.kuainiu.com/online/mysql/1/ 2>JDK_API_1_5_zh_CN 3>WebWork                 http://wiki.javascud.org/display/ww2cndoc/WebWork 4>        html语法                http://www.gzsums.edu.cn/webclass/html/html_design.html 5>        WADS 0.5 安装使用指南.txt 6>        WebWork Session    

    最新回复(0)