自定义类继承FreemarkerManager类,重写protected Configuration createConfiguration(ServletContext servletContext)throws TemplateException方法定义有哪些TemplateDirectiveModel类与它的别名[自定义标签名称],通过Spring来取:
import java.util.Map; import javax.servlet.ServletContext; import org.apache.struts2.views.freemarker.FreemarkerManager; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import freemarker.template.Configuration; import freemarker.template.TemplateDirectiveModel; import freemarker.template.TemplateException; /** * 从spring取得所有的TemplateDirectiveModel类的对象,全部取出来,交给struts管理 * @author Administrator * */ public class DirectiveFreemarkerManager extends FreemarkerManager{ @Override protected Configuration createConfiguration(ServletContext servletContext) throws TemplateException { //调用你父类操作 Configuration configuration=super.createConfiguration(servletContext); //设定在页面使用的标签的类型 (([]、<>),[]这种标记解析要快些) [] SQUARE_BRACKET_TAG_SYNTAX , <>ANGLE_BRACKET_TAG_SYNTAX configuration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX); //取得spring所管理所有的对象 ClassPathXmlApplication, WebAplicationContext ApplicationContext appContext=WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); // 获取实现TemplateDirectiveModel的bean Map<String, TemplateDirectiveModel> beans= appContext.getBeansOfType(TemplateDirectiveModel.class); //再把这些类,全部交给struts的freemarker来管理 //Component("upper") public class UpperDirective implements TemplateDirectiveModel for(String key : beans.keySet()){ Object obj=beans.get(key); //TemplateDirectiveModel if(obj!=null && obj instanceof TemplateDirectiveModel){ configuration.setSharedVariable(key, obj); } } return configuration; } }
在struts.xml中添加:
<!-- 让struts来管理freemarker自定义标签类 struts.freemarker.manager.classname=org.apache.struts2.views.freemarker.FreemarkerManager --> <constant name="struts.freemarker.manager.classname" value="com.xxxx.struts.DirectiveFreemarkerManager"></constant>
编写TemplateDirectiveModel,并交给Spring管理:
import java.io.IOException; import java.io.Writer; import java.util.Map; import org.springframework.stereotype.Component; import freemarker.core.Environment; import freemarker.template.TemplateDirectiveBody; import freemarker.template.TemplateDirectiveModel; import freemarker.template.TemplateException; import freemarker.template.TemplateModel; import freemarker.template.TemplateModelException; /**@author Administrator
自定义指定,作用,把嵌套的内容中的小写全部转换为大写字母
/
@Component("upper")
public class UpperDirective implements TemplateDirectiveModel {
//evn 环境输出
//params 参数
//loopVars 循环
//body 内容
@Override
public void execute(Environment env, Map params,
TemplateModel[] loopVars, TemplateDirectiveBody body)
throws TemplateException, IOException {
//如果有参数 应该不支持参数
if(!params.isEmpty()){
throw new TemplateModelException(
"这个指令不允许使用参数.");
}
if(loopVars.length!=0){
throw new TemplateModelException(
"这个指令不允许使用循环参数.");
}
// 如果嵌套内容不为空的
if(body!=null){
//如果有内容,自己去重写输出流
body.render( new UpperCaseFilterWriter(env.getOut()));}</span><span style="color: #0000ff">else</span><span style="color: #000000">{</br> </span><span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> RuntimeException("无内容异常"<span style="color: #000000">);</br> }</br>
}
//内部类
private static class UpperCaseFilterWriter extends Writer{
private final Writer out; //接收网页中的输出 流对象
public UpperCaseFilterWriter(Writer out){
this.out=out;
}
//cbuf 文字内容, off位移,len 文字长度
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
char[] transformedChar=new char[len];
for(int i=0;i<len;i++){
transformedChar[i]=Character.toUpperCase(cbuf[i+off]);
}
out.write(transformedChar);
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
out.close();
}
}
}
编写action,result—>字符串,type=”freemarker”:
<!-- 约定大于配置 /admin/Flinktype_list.action --> <package name="freemarkerPackage" namespace="/admin" extends="commonPackage" > <action name="*_*" class="{1}Action" method="{2}"> <result name="{2}" type="freemarker">/admin/template/{1}/{2}.ftl</result> </action> </package>
模板/template/upper.ftl:
<!DOCTYPE html> <html lang="ch-ZN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <title>freemarker自定义标签</title> </head> <body><span style="color: #0000ff"><</span><span style="color: #800000">@upper</span><span style="color: #0000ff">></span><span style="color: #000000"></br> bar </span><span style="color: #0000ff"><</span><span style="color: #800000">#-- </span><span style="color: #ff0000">All kind of FTL is allowed here --</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">#list </span><span style="color: #ff0000">["red", "green", "blue"] as color</span><span style="color: #0000ff">></span><span style="color: #000000"></br> ${color}</br> </span><span style="color: #0000ff"></</span><span style="color: #800000">#list</span><span style="color: #0000ff">></span><span style="color: #000000"></br> baaz</br> </span><span style="color: #0000ff"></</span><span style="color: #800000">@upper</span><span style="color: #0000ff">></span></br></br></br> <span style="color: #0000ff"><</span><span style="color: #800000">hr</span><span style="color: #0000ff">/></span></br></br> <span style="color: #0000ff"><</span><span style="color: #800000">@cms_linktype </span><span style="color: #ff0000">limit</span><span style="color: #0000ff">="0,10"</span> <span style="color: #0000ff">></span><span style="color: #000000"></br> 请选择分类: </span><span style="color: #0000ff"><</span><span style="color: #800000">select</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">#list </span><span style="color: #ff0000">arrTypes as x</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">option </span><span style="color: #ff0000">value</span><span style="color: #0000ff">="${x.id}"</span><span style="color: #0000ff">></span>${x.typeName}<span style="color: #0000ff"></</span><span style="color: #800000">option</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">#list</span><span style="color: #0000ff">></span> </br> <span style="color: #0000ff"></</span><span style="color: #800000">select</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">@cms_linktype</span><span style="color: #0000ff">></span></br>
</body>