Using freemarker to generate custom labels in spring mvc project

In the spring mvc +freemarker project, we want to use custom tags on the html page to facilitate some of our functions. In addition to defining tags on the page, we can also use programs to define them.
First write a class UpperDirective.Java to implement the TemplateDirectiveModel interface,
code show as below:
[java] view plain copy
public class UpperDirective implements TemplateDirectiveModel  {  
  
    @Override  
    public void execute(Environment env, Map map, TemplateModel[] templateModels,  
            TemplateDirectiveBody body) throws TemplateException, IOException {  
        if(!map.isEmpty()){  
            throw new TemplateModelException("this directive doesn't allow parameters");  
        }  
        if(templateModels.length!=0){  
            throw new TemplateModelException("this directive doesn't allow variables");  
        }  
        if(body!=null){  
            body.render(new UpperCaseFilterWriter(env.getOut()));  
        }else{  
            throw new RuntimeException("missing body");  
        }  
    }  
  
      
  
}  
class UpperCaseFilterWriter extends Writer{  
  
    private final Writer out;  
      
    public UpperCaseFilterWriter(Writer out) {  
        this.out=out;  
    }  
    @Override  
    public void write(char[] cbuf, int off, int len) throws IOException {  
        char[] transformedCbuf = new char[len];  
        for(int i=0;i<len;i++){  
            transformedCbuf[i]=Character.toUpperCase(cbuf[i+off]);  
        }  
        out.write(transformedCbuf);  
    }  
  
    @Override  
    public void flush() throws IOException {  
        out.flush();  
    }  
  
    @Override  
    public void close() throws IOException {  
        out.close();  
          
    }  
      
}  
Then configure it in the configuration file:
[html] view plain copy
<bean id="freemarkerConfig"   
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
        <property name="templateLoaderPath">  
              <value>/ftl/</value>  
        </property>  
        <property name="freemarkerSettings">  
            <props>  
                <prop key="locale">zh_CN</prop>  
                <prop key="defaultEncoding">UTF-8</prop>  
                <prop key="date_format">yyyy-MM-dd</prop>  
                <prop key="time_format">HH:mm:ss</prop>  
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  
                <prop key="number_format">0.################</prop>  
            </props>  
        </property>  
        <property name="freemarkerVariables">  
            <map>  
                <entry key="upper" value-ref="upperDirective"></entry>  
            </map>  
        </property>    
    </bean>  
      
     <bean id="upperDirective" class="com.nbsh.cms.util.UpperDirective"></bean>   

The label output on the page: <@upper>hh</@upper> can output uppercase: HH.

 http://blog.csdn.net/uk8692/article/details/38176263

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327087177&siteId=291194637