MVC框架视图及页面跳转分析-模板技术

来源:互联网 发布:mysql 取不重复数据 编辑:程序博客网 时间:2024/05/21 19:38
 
模板技术是一项非常强大的技术,较之于传统Struts等以推JSP为主的框架,模板技术更加灵活,应用范围更加广泛,你不仅仅可以用来生成Web页面,凡是文本性质的东西,都可以通过模板机制来处理,比如生成Java源代码、配置文件等。模板技术在在其它一些动态语言如PHP、Ruby等得到大量应用,而近年来备受热棒的Rails等框架的页面处理也是基于模板机制,另外我们每天使用的Eclipse工具中,也大量使用到了模板技术,比如自定义代码块等功能。EasyJF开源的Web MVC框架EasyJWeb中的页面输出正是采用模板机制。
模板机制的原理其实比较简单,其实就是准备好一个模板,在模板中添加一些用于可替换的特殊标志,然后在用户使用通过模板引擎,把准备好的数据与模板进行合并处理,就能生成得到我们所期望的内容。
比如,我们有一个html模板内容如下,其中黑体部分是模板的特殊标志,用来作数据替换的:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>html模板title>
head>
<body>
<h1>${title}h1>
<p>${date}p>
body>
html>
当模板引擎在处理的时候,他就会把特殊标志的部分换成具体的数据内容,比如我们如果给title及date分别如下的值:
title="新闻标题"
Date=new Date()
则就会输出类似下面的内容:

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>html模板title>
head>

<body>
<h1>新闻标题h1>
<p>Thu Nov 29 20:45:38 CST 2007p>
body>
html>
通过使用模板技术,你可以用EasyJWeb来生成java代码,比如在EasyJWeb代码生成引擎中的一个生成业务接口的模板内容如下:

 

public class $!{domainName}ServiceImpl implements I$!{domainName}Service{
    
    
private I$!{domainName}DAO $!{domain}Dao;
    
    
public void set#upperCase($!{domain})Dao(I$!{domainName}DAO $!{domain}Dao){
        
this.$!{domain}Dao=$!{domain}Dao;
    }

    
    
public Long add$!{domainName}($!{domainName} $!{domain}{    
        
this.$!{domain}Dao.save($!{domain});
        
if ($!{domain} != null && $!{domain}.get$!{id}() != null{
            
return $!{domain}.get$!{id}();
        }

        
return null;
    }

    
    
public $!{domainName} get$!{domainName}(Long id) {
        $
!{domainName} $!{domain} = this.$!{domain}Dao.get(id);
        
return $!{domain};
        }

    
    
public boolean del$!{domainName}(Long id) {    
            $
!{domainName} $!{domain} = this.get$!{domainName}(id);
            
if ($!{domain} != null{
                
this.$!{domain}Dao.remove(id);
                
return true;
            }
            
            
return false;    
    }

    
    
public boolean batchDel$!{domainName}s(List<Serializable> $!{domain}Ids) {
        
        
for (Serializable id : $!{domain}Ids) {
            del$
!{domainName}((Long) id);
        }

        
return true;
    }

    
    
public IPageList get$!{domainName}By(IQueryObject queryObject) {    
        
return QueryUtil.query(queryObject, $!{domainName}.class,this.$!{domain}Dao);        
    }

    
    
public boolean update$!{domainName}(Long id, $!{domainName} $!{domain}{
        
if (id != null{
            $
!{domain}.set$!{id}(id);
        }
 else {
            
return false;
        }

        
this.$!{domain}Dao.update($!{domain});
        
return true;
    }
    
    
}

 

你只需传送一个具有id的域对象作为参数,添加到结果集Result中,比如我们传送一个名为Orders的域模型类,就会得到如下的输出:

 

public class OrdersServiceImpl implements IOrdersService {

    
private IOrdersDAO ordersDao;

    
public void setOrdersDao(IOrdersDAO ordersDao) {
        
this.ordersDao = ordersDao;
    }


    
public Long addOrders(Orders orders) {
        
this.ordersDao.save(orders);
        
if (orders != null && orders.getId() != null{
            
return orders.getId();
        }

        
return null;
    }


    
public Orders getOrders(Long id) {
        Orders orders 
= this.ordersDao.get(id);
        
if (orders != null{
            
return orders;
        }

        
return null;
    }


    
public boolean delOrders(Long id) {
        Orders orders 
= this.getOrders(id);
        
if (orders != null{
            
this.ordersDao.remove(id);
            
return true;
        }

        
return false;
    }


    
public boolean batchDelOrderss(List<Serializable> ordersIds) {

        
for (Serializable id : ordersIds) {
            delOrders((Long) id);
        }

        
return true;
    }


    
public boolean removeOrders(Long id) {
        Orders orders 
= this.getOrders(id);
        
if (orders != null{
            
this.ordersDao.remove(id);
            
return true;
        }

        
return false;
    }


    
public boolean batchRemoveOrderses(List<Serializable> ordersIds) {

        
for (Serializable id : ordersIds) {
            delOrders((Long) id);
        }

        
return true;
    }


    
public boolean recoverOrders(Long id) {
        
if (id == null{
            
return false;
        }

        Orders obj 
= this.getOrders(id);
        
if (obj == null{
            
return false;
        }

        obj.revert();
        
return true;
    }


    
public boolean batchRecoverOrderses(List<Serializable> ids) {
        
for (Serializable id : ids) {
            recoverOrders((Long) id);
        }

        
return true;
    }


    
public IPageList getOrdersBy(IQueryObject queryObject) {
        
return QueryUtil.query(queryObject, Orders.classthis.ordersDao);
    }


    
public boolean updateOrders(Long id, Orders orders) {
        
if (id != null{
            orders.setId(id);
        }
 else {
            
return false;
        }

        
this.ordersDao.update(orders);
        
return true;
    }


}

在Java领域,比较强大模板引擎比较多,在众多的模板引擎中,最为优秀的当数Apache 的Velocity,Velocity是一个非常优秀的模板引擎,但由于各种原因一直被JSP的光环所压住,直到这一两年来JSP的缺点越来越明显,随着在EasyJWeb等框架中的大量推广及应用,直到去年这颗被没多年的金子才逐渐闪耀光芒,一耀成为Apache的一级项目,享受到明星待遇.相信在以后会越得到更广泛的使用。EasyJWeb中的首推模板引擎就是Velocity,当然也可以选择使用其它的模板引擎如FreeMarker或者使用自己开发的模板引擎,所需要做的就是在EasyJWeb中添加一个配置即可。


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1908011