velocity中自定义指令的用法

velocity是类似于jsp并且比jsp更为强大的引擎,用法也很简单

和jsp的配置差不多
1.在mvc-servlet.xml中配置velocity的引擎、视图解析器、配置文件路径等配置;
2.视图层使用**.vm格式即可

基本用法可自行百度 这里说自定义指令的做法
1. 创建一个类继承自Directive类 (这是velocity定义指令必须使用的类)
2. 实现Directive类里定义的三个抽象方法,

1.getName()此方法用来设置指令的名称
2.getType()此方法用来设置是否需要结束语句
3.render(InternalContextAdapter context, Writer writer, Node node) 此方法中写指令需要执行的内容,其中context 是传入的参数的内容,writer用来输出结果,node此对象如果用多个参数使用jjtGetChild(index)来获取参数位置通过.value(context)获取当前位置参数的值

3.需要在velocity的配置文件中配置此节点的位置,以便于当前自定义指令的初始化
4. 都完成之后可以在vm文件中 通过配置的getName名称进行调用 例如 假设指令名称配置为name #name(“参数1”,”参数2”);即可!


简单的代码如下:
1.mvn-servlet.xml配置

 <!--velocity配置-->
    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    //配置视图层的路径
        <property name="resourceLoaderPath" value="/WEB-INF/views"/>
        //velocity配置文件的位置
        <property name="configLocation" value="classpath:velocity.properties"/>
        //字符编码
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">utf-8</prop>
                <prop key="output.encoding">utf-8</prop>
            </props>
        </property>
    </bean>
    <!--配置velocity试图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    //解析字符串增加后缀
        <property name="suffix" value=".vm"/>
        //前缀
        <property name="prefix" value=""/>
        //返回的类型
        <property name="contentType" value="text/html;charset=utf-8"/>
    </bean>

自定的directive代码如下:

public class DemoDirective  extends Directive{
    @Override
    public String getName() {
        return "demo";//自定义指令的名称
    }

    @Override
    public int getType() {
        return 2;//配置不带结束符
    }

     @Override
    public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {

        //获取参数的值
        String str1 = node.jjtGetChild(0).value(context).toString();//第一个参数的值
        String str2 = node.jjtGetChild(1).value(context).toString();//第二个参数的值
        StringBuffer str = new StringBuffer();//创建字符串对象
        if(str1 !=null){
            str =str.append("<p>测试结果1:"+str1+"</p>");//显示一段html
        }
        if(str2 !=null){
            str =str.append("<p>测试结果2:"+str2+"</p>");
        }
        writer.write(str.toString());//输出结果
        return true;
    }
}

配置文件配置代码:

resource.loader = file
file.resource.loader.description = Velocity File Resource Loader
#encoding
input.encoding=UTF-8
output.encoding=UTF-8

#autoreload when vm changed
file.resource.loader.cache=false
file.resource.loader.modificationCheckInterval=2
velocimacro.library.autoreload=false
#自定义指令############################ 配置自定义指令的绝对路径
userdirective = com.mixa.util.DemoDirective 
###########################################

视图层代码:

<!DOCTYPE html>
<html>
<head>

</head>
<body>

//测试指令
  #demo("参数1","参数2");
</body>

</html>

输出结果:

测试结果1:参数1
测试结果2:参数2

这是最简单的一个测试类 大家可以根据需要在render中书写相应的业务逻辑以获取到自己想要的htmL对象结果返回到vm显示

多少不凡、只因不甘!
一个初出茅庐的程序员 希望成长 希望进步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章