1.示例需求定义我们定义一个演示用例的需求。在入门文档中,我们会完成这个用例的一些简单需求。在参考手册中,随着各部分内容的深入介绍,我们会完善这个用例。 演示用例的主要内容是好梦一日游公司的休假申请管理。我们定义好梦一日游公司有两个部门,"忽悠部",和"善后部"。定义公司的老大为尤优。两个部门各有职员若干。定义系统中有两种角色:领导和职员。职员可以提出休假申请,领导可以审批休假申请。 根据需求,我们需要为休假申请建立一个模型。一个简单的实现包括了提出人、请假理由等属性。毫无疑问的,这个模型将会被保存到数据库中的一张表。业务上,我们可以用下图来描述需求: ![]() 我们可以预想的到,在列表、查看、审批、编辑这些操作过程中,针对休假申请模型,操作者所能够查看、修改的属性是各不相同的。例如在审批操作中,操作者应该只能查看"请假理由",而不能修改它,类似的可见与不可见,可编辑遇不可编辑的控制将会在操作定义中配置。 2.建立项目拿到Metawork之后,你需要新建一个项目,可以使用如下的命令来创建项目: mvn archetype:create \ -DgroupId=com.mycompany \ -DartifactId=vocation \ -DarchetypeGroupId=net.lingdot.metawork \ -DarchetypeArtifactId=metawork-archetype-webapp \ -DarchetypeVersion=1.0 3.编写模型类根据需求,我们已经确定要建立一个休假申请模型。写好的类如下面的代码所示: package com.mycompany.vocation.entity; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import net.lingdot.metawork.core.entity.actor.User; import net.lingdot.metawork.core.entity.meta.annotation.DomainModel; import net.lingdot.metawork.core.entity.meta.annotation.Property; @Entity @DomainModel(name="休假申请",folder="vocation",category="休假申请") public class Application { private Long id; private User user; private User checker; private String description; private String date; private Date addDate = new Date(); private String checkResult = "待审核"; private String checkDescription; /** * @return the id */ @Id @GeneratedValue(strategy=GenerationType.AUTO) @Property(name="编号") public Long getId() { return id; } /** * @param id the id to set */ public void setId(Long id) { this.id = id; } /** * @return the user */ @ManyToOne(cascade={}) @Property(name="提出人") public User getUser() { return user; } /** * @param user the user to set */ public void setUser(User user) { this.user = user; } /** * @return the checker */ @ManyToOne(cascade={}) @Property(name="审批人") public User getChecker() { return checker; } /** * @param checker the checker to set */ public void setChecker(User checker) { this.checker = checker; } /** * @return the description */ @Property(name="请假缘由") @Column(length=1000) public String getDescription() { return description; } /** * @param description the description to set */ public void setDescription(String description) { this.description = description; } /** * @return the date */ @Property(name="请假时间") public String getDate() { return date; } /** * @param date the date to set */ public void setDate(String date) { this.date = date; } /** * @return the addDate */ @Property(name="提出时间") public Date getAddDate() { return addDate; } /** * @param addDate the addDate to set */ public void setAddDate(Date addDate) { this.addDate = addDate; } /** * @return the checkResult */ @Property(name="审批结果") public String getCheckResult() { return checkResult; } /** * @param checkResult the checkResult to set */ public void setCheckResult(String checkResult) { this.checkResult = checkResult; } /** * @return the checkDescription */ @Property(name="审批说明") public String getCheckDescription() { return checkDescription; } /** * @param checkDescription the checkDescription to set */ public void setCheckDescription(String checkDescription) { this.checkDescription = checkDescription; } } 在上面的代码中,当前实体类和User类有一个多对一的关联。这个User类是Metawork提供的用户模型实现。 然后创建一个Hibernate配置文件,告诉Hibernate你要将Application类持久化到数据库。在src/main/webapp/WEB-INF/config目录中创建一个目录vocation用于存放本项目的配置,然后在其中创建hibernate.cfg.xml。 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <mapping class="com.mycompany.vocation.entity.Application"/> </session-factory> </hibernate-configuration> 4.编写表单页面根据模型类上面的folder="vocation"配置,我们在webapp目录下面建立vocation目录,并建立表单页面application-edit.jsp。表单页面代码如下: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/metawork/common/taglibs.jsp"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <%@ include file="/metawork/common/meta.jsp"%> <title>Insert title here</title> <script type="text/javascript" src="${ctx}/metawork/common/script/common.js"></script> <script type="text/javascript" src="${ctx}/metawork/common/script/form.js"></script> <link rel="stylesheet" type="text/css" href="${ctx}/metawork/style/form.css"/> <s:head/> <script> function init(){ if(metawork.operationAlias=="view") $("saveButton").style.display="none"; } </script> </head> <body onload="init()"> <div id="formDiv"> <s:form id="vocationForm" validate="true"> <s:hidden name="id"/> <s:hidden name="entity.user.id"/> <s:textfield name="entity.user.name" label="申请人" disabled="true"/> <s:textfield name="entity.date" label="休假时间"/> <s:textfield name="entity.addDate" label="提出时间" disabled="true"/> <s:select name="entity.checker.id" emptyOption="true" listKey="id" listValue="name" list="viewData.checkers" label="审核人"></s:select> <s:textarea name="entity.description" label="请假理由" rows="5" cols="50"/> <s:select name="entity.checkResult" emptyOption="true" list="#{'待审核':'待审核','通过':'通过','不通过':'不通过'}" label="审批结果"></s:select> <s:textarea name="entity.checkDescription" label="审批说明" rows="5" cols="50"/> <s:submit id="saveButton" value="保存" onclick="saveEntity('vocationForm');return false;"></s:submit> </s:form> </div> </body> </html> 页面中使用了一些struts2的标签,另外,引入了几个文件。值得一提的是第一个下拉框的list属性指定使用一个viewData.checkers集合作为其数据源,这里设计到metawork中另外一个概念叫做"视图数据",在文档的后面会说道如何配置。暂且不要关心上面的js方法的作用。我们继续向前。 5.构建、运行然后在命令行中执行mvn package,把项目构建成为一个war包。然后放到Tonmcat6的webapps目录下,然后启动之。 系统启动之后,需要进行一系列的配置工作。请继续阅读休假申请(下) |
导航 团队
| ||||||||||||
休假申请(上)
(没有)
