博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC框架视图及页面跳转分析-模板技术
阅读量:4192 次
发布时间:2019-05-26

本文共 4572 字,大约阅读时间需要 15 分钟。

 
模板技术是一项非常强大的技术,较之于传统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 2007 p>
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

你可能感兴趣的文章
索尼发布Xperia 8手机:采用骁龙630处理器
查看>>
传音控股:华为起诉公司及子公司 已立案尚未开庭
查看>>
新AirPods渲染图曝光:采用黑白灰金四种配色
查看>>
再见 iTunes!苹果macOS Catalina 10.15正式版更新
查看>>
“滚!”央视、腾讯暂停NBA季前赛转播,NBA被中国市场“下架”!
查看>>
十一假期国人消费力爆棚,国内旅游收入超6497亿,你花了多少?
查看>>
奥斯卡“最佳国际电影”奖,《哪吒之魔童降世》申请出战!
查看>>
卢伟冰宣布Redmi新机即将发布 疑为Redmi 8A
查看>>
李彦宏卸任百度云计算技术公司执行董事 崔珊珊接任
查看>>
美团点评成中国第三大互联网公司!
查看>>
看呆了!日本男粉丝凭瞳孔倒影找到偶像住所 实施猥亵...
查看>>
点评锤子新机外观被前下属骂“厚颜无耻” 罗永浩道歉:希望还来得及补偿
查看>>
安兔兔发布9月份Android性能榜:855霸榜,华为竟垫底
查看>>
iOS 13新增防骚扰功能,但开启后用户吐槽声一片
查看>>
任正非:不要过度消费客户及民众对我们的同情与支持
查看>>
10月15日发布!谷歌Pixel 4系列或将全系支持5G
查看>>
高通首席技术官:5G+AI是至关重要的组合
查看>>
国庆档票房近50亿创纪录 背后影视股却遭遇集体下跌
查看>>
来了!索尼官宣PS5游戏主机:比预计的时间稍晚一些
查看>>
英雄互娱:中止代理的《NBA LIVE》在中国大陆地区所有推广活动
查看>>