您的位置:首页 > 移动开发 > Objective-C

用easyjweb快速实现增删查改

2010-08-08 16:13 267 查看
 

 

新建一个名为customer的web工程,我们要实现的对customer进行简单的增删查改功能,而其他功能,如验证暂不实现。话不多说,马上开始吧。
1、业务层简单开发
·模型设计
       在myapp.domain包下新建Customer.java代表Customer模型,该模型包含属性有用户编号,姓名,性别,年龄,住址,邮件等信息,年龄用枚举类Gender表示。代码如下:

package myapp.domain;
public class Customer {
private Long id;
private String name;
private int age;
private Gender gender;
private String address;
private String email;

// getter and setter方法
}

 
·DAO层设计
应用的持久化功能由DAO负责,实现对实体的增删查改功能。本例中仅作演示用,故不对实体进行持久化操作,而是用Java容器来代替,详见代码。

public interface ICustomerDao {
Long addEntity(Customer instance);
boolean delEntity(Long id);
Customer getEntity(Long id);
boolean updateEntity(Long id, Customer instance);
Collection<Customer> getEntityList();
List<Customer> findEntityListByName(String name);
}

 

 

 

 

 

 
·Service层设计
    Dao封装了对底层的操作,service层获得Dao层的对象来实现上层相应的服务。Easyjweb框架有一个内置的IOC容器,可以通过该容器将IcustomerDao接口对象注入到ICustomerService,降低了应用的耦合度,并能方便实现customer的添删改查。IOC容器的实现见配置文件部分。

public interface ICustomerService {
Long addCustomer(Customer instance);
Customer getCustomer(Long id);
boolean delCustomer(Long id);
boolean updateCustomer(Long id,Customer instance);
Collection<Customer> getCustomerList();
List<Customer> findCustomer(String name);
}

 

 

 

 

 
2、控制层简单开发
·Controller设计
       Easyjweb应用的控制器是一个Action,每个Action必须实现IwebAction接口并实现execute方法,如Hello示例中所示。Easyjweb框架还为我们提供了许多IWebAction接口的实现类,能够提供许多强大的功能,比如AbstractCmdAction,AbstractCrudAction等。
       先简单介绍这两个类。AbstractCmdAction是IWebAction接口的实现类,在请求中我们可以通过使用cmd传递的参数值作为请求执行的方法名的构成。构成的规则为doXxxx()。其中Xxxx表示的是首字母大写后的该值。比如请求的是xxx.ejf?cmd=edit,则会调用doEdit或者edit方法并返回适当的页面,方便快速。相见AbstractPageCmdAction部分介绍。AbstractCrudAction类间接继承了AbstractCmdAction,是处理普通数据表的添删改查(CRUD)处理的抽象类,用户只需继承该Action,并根据自身的情况实现其中的模板方法即可。具体见AbstractCrudAction部分。
所以,我们创建的控制器CustomerAction就继承AbstractCrudAction,只要实现父类的抽象方法即可快速实现增删改查的功能。另外,在CustomerAction中我们再次利用easyjweb框架内置的IOC容器,往Action中注入IcustomerService对象,实现增删改查服务。由于本程序添加了根据姓名查找实体功能,故增加了一个doFind方法,查找所有同名的customer。
 

@Action
public class CustomerAction extends AbstractCrudAction {
@Inject
private ICustomerService service;

public void setService(ICustomerService service) {
this.service = service;
}

protected Object findEntityObject(Serializable id) {
return service.getCustomer((Long) id);
}

protected void removeEntity(Serializable id) {
service.delCustomer((Long) id);
}

protected void saveEntity(Object object) {
service.addCustomer((Customer)object);
}

protected void updateEntity(Object object) {
service.updateCustomer(((Customer)object).getId(), (Customer)object);
}

protected IPageList queryEntity(IQueryObject queryObject) {
Collection<Customer> collection = service.getCustomerList();
IQuery query = new ListQuery(toList(collection));
IPageList pageList = new PageList(query);
pageList.doList(10, 1, "", "");
return pageList;
}
// 添加一个按姓名查找方法
public void doFind(WebForm form) {
String name = CommUtil.null2String(form.get("name"));
List<Customer> list = service.findCustomer(name);
form.addResult("list", list);
page("list");
}
// 其他代码
}

 
 

 
3、视图层简单开发
·视图设计
easyjweb集成了velocity页面显示技术,可以选择使用velocity开发试图页面。Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言来引用由java代码定义的对象。接下来简单介绍velocity的基本语法,详细可以参考相关文档。
赋值 #set
#set($a = ‘Velocity”),这样就可以在页面中利用$a表示‘Velocity”字串
条件判断 #if, # elseif, #end。
  #if($a)     #end
循环 #foreach
#foreach($customer in #list)
  $customer.name
#end
 
easyjweb的默认视图路径为WEB-INF/views,在该目录下新建customer目录,创建list.tm和edit.html。详细代码。
 
4、 配置文件
web.xml文件的配置跟hello示例相同,还需要配置WEB-INF/easyjf-web.xml文件,需要两点配置:1、框架需要扫描的包,即你设置的Action所在包。2、easyjweb的IOC容器,在CustomerServiceImpl实现类中注入CustomerDaoImpl对象。

<?xml version="1.0" encoding="utf-8"?>
<easyjf-web>
<framework-setting>
<property name="com.easyjweb.defaultActionPackages">
myapp.action
</property>
</framework-setting>
<beans>
<bean name="customerService" class="myapp.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao" />
</bean>
<bean name="customerDao" class="myapp.dao.impl.CustomerDaoImpl"/>
</beans>
</easyjf-web>

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息