想找一‍家品‍牌麻‍辣‍麻辣烫加盟‍盟,选哪‍家比较好做?党。

velocity自定义标签和指令(转:zwj)
velocity本身支持自定义标签和指令的扩展,我们看看扩展指令的步骤及searchweb2的应用场景,
1.使用方法
在 Velocity 模板语言的语法中,以美元符 $ 开头的为变量的声明或者引用,而以井号 # 开头的语句则为 Velocity 的指令(Directive)。
velocity支持的指令有:#set,#foreach,#if #else
#end,#parse,#include,#evaluate,#define,#macro,
在velocity的jar包中的directive.properties中定义了这些实现:
directive.1=org.apache.velocity.runtime.directive.Foreach
directive.2=org.apache.velocity.runtime.directive.Include
directive.3=org.apache.velocity.runtime.directive.Parse
directive.4=org.apache.velocity.runtime.directive.Macro
directive.5=org.apache.velocity.runtime.directive.Literal
directive.6=org.apache.velocity.runtime.directive.Evaluate
directive.7=org.apache.velocity.runtime.directive.Break
directive.8=org.apache.velocity.runtime.directive.Define
自定义标签和指定,比如我们定义了下面的remoteVelocity指令
"-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&
"http://www.w3.org/1999/xhtml"&
"Content-Type" content="text/
charset=gbk" /&
& This app runs well
&&#set($monkey=
{"banana" : "good", "roast beef" : "bad"})
& #remoteVelocity("namespace","velocityname",$monkey)
要对这个指令的实现要继承Directive这个类,这个宏我们可以从其他服务获取vm的内容,动态渲染,这种方式可以统一管理公共模板,
import com.alibaba.citrus.service.template.TemplateS
import com.alibaba.click.util.HostU
public class RemoteVelocity extends Directive{
@Autowired
TemplateService templateService;
private static final VelocityEngine velocityEngine =
new VelocityEngine();
public String getName() {
return "remoteVelocity";
public int getType()
return LINE;
public boolean render(InternalContextAdapter context, Writer
&&&&&&&&&&&
Node node) throws IOException,
ResourceNotFoundException,
&&&&&&&&&&&
ParseErrorException, MethodInvocationException
SimpleNode sn_region = (SimpleNode)
node.jjtGetChild(0);&&
&&&&&&&&String
(String)sn_region.value(context);&&
&&&&&&&&SimpleNode
sn_key = (SimpleNode)
node.jjtGetChild(1);&&
&&&&&&&&Serializable
(Serializable)sn_key.value(context);&&
SimpleNode sn_data = (SimpleNode)
node.jjtGetChild(2);
Object data =
sn_data.value(context);&&
Map map = new HashMap();
map.put("data",
String vel =
HostUtil.getResponseText("http://127.0.0.1/index.html");
String vel="#foreach($member in $data.entrySet())
$member.key - $member.value
writer.write(renderTemplate(map,vel));
return true;
public static String renderTemplate(Map
params,String vimStr){
VelocityContext context = new VelocityContext(params);
StringWriter writer = new StringWriter();
&&&&&&&&&&&
velocityEngine.evaluate(context, writer, "",
} catch (ParseErrorException e) {
&&&&&&&&&&&
// TODO Auto-generated catch block
&&&&&&&&&&&
e.printStackTrace();
} catch (MethodInvocationException e) {
&&&&&&&&&&&
// TODO Auto-generated catch block
&&&&&&&&&&&
e.printStackTrace();
} catch (ResourceNotFoundException e) {
&&&&&&&&&&&
// TODO Auto-generated catch block
&&&&&&&&&&&
e.printStackTrace();
} catch (IOException e) {
&&&&&&&&&&&
// TODO Auto-generated catch block
&&&&&&&&&&&
e.printStackTrace();
}//渲染模板
return writer.toString();
node.jjtGetChild(2) 这个方法可以获取对应指令的参数,下标从0开始,
在web工程的WEB-INF下面定义velocity.properties这个配置文件,用户扩展的指令最好放到这个文件里面,velocity的jar包里面提供了默认实现,我们可以覆盖重新定义自己的扩展,类就是对应自己的扩展类的类名
&#自定义标签
userdirective=com.alibaba.click.test.RemoteVelocity&&
这样启动后就可以正常使用了。
Directive的三个方法:
getName:指令的名称
getType:当前有LINE,BLOCK两个值,line行指令,不要end结束符,block块指令,需要end结束符
public boolean
render(InternalContextAdapter context, Writer writer,
&Node node) 具体处理过程
2 应用场景:
现在searchweb2.0使用这种方式来实现页面各个control模板区块的面向对象模式来实现页面区块的覆盖
比如下面几个指令:
public static finalString FUNCTION = "function";public static finalString OVERRIDE = "override";public static finalString SUPER = "super";public static finalString EXTENDS = "extends";
我们在大市场页面有主数据区的图片区块渲染内容如下:
##图片等主要内容#function("mainPictures")&&/span&li class="sw-mod-offer-block sw-block-photo"&$control.setTemplate("offerresult,img,blocks,picture,promotionMainPictures.vm").setParameter("offer", $offer).setParameter("velocityCount", $velocityCount).setParameter("keywords", $!{vo.keywords}).setParameter("hasInsideCooperateId",$vo.hasInsideCooperateId).setParameter("picSize", "100x100")&/&/span&li&#end
如果在某个行业图片展示区块不一致,则我们可以使用下面的方式覆盖掉这个区块,其他如果一样的区块都不用修改
#override("mainPictures")&&/span&li class="sw-mod-offer-block sw-block-photo"&$control.setTemplate("offerresult,img,blocks,picture,salesPictures.vm").setParameter("offer", $offer).setParameter("velocityCount", $velocityCount).setParameter("keywords", $!{vo.keywords}).setParameter("hasInsideCooperateId",$vo.hasInsideCooperateId)&/&/span&li&
来实现类似面向对象的重写功能。
我们很多小行业的搜索展示,只是主数据区增加一点展示字段或修改一点展示字段,以前一方面切分粒度不够另一方面不支持继承扩展,导致几乎差不多内容,也搞多个模板
扩展velocity的指令extends,做到像java类的继承复用模式,只对要改动的block进行override即可,其它全部会继承复用,大大提高模板复用率
模板复用率更高,理解维护更方便
大家有兴趣的可以看看searchweb2的代码。http://svn./repos/ali_china/olps/searchweb2/trunk
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。【图文】2014.3政府的责任:对人民负责zwj_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
2014.3政府的责任:对人民负责zwj
上传于||暂无简介
大小:2.54MB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢

我要回帖

更多关于 晓富酸辣粉加盟总部 的文章

 

随机推荐