关于freemarker自定义标签作为模板在struts中生成静态html的问题
这个问题其实之前也已经有人问过了类似的:http://bbs.csdn.net/topics/320155882
,但是我还是找不到满意的答案。
详细描述如下:
自定义标签 [@vcms_entity][/@vcms_entity],这个标签只有三个参数,entity,package,id,传入这三个参数后可到数据库取出相应的对象
/**
* 通用取数据对象模板(entity:类名,需与实际名同,可指定包名package;其他属性名需与类属性名同,因为要调用相应的getter)
*
* @example <@vcms_entity entity="Channel" package="com.vcms.cms.entity" id="1">
* </@vcms_entity>
*
* @param entity
* 类名 必填 该类名大小写正确,例如有一个类是UserInfo,如果写成userInfo或Userinfo都是错误
*
* @param package 包名 选填 默认是com.vcms.cms.entity
*
* @param id
* 对象对应的id 必填
*
* @author vick
*
*/
@Component("vcms_entity")
public class VcmsEntityDirective implements TemplateDirectiveModel {
/**
* 结果集合
*/
private static final String RESULT = "obj";
private int id;
private BasicService basicService;
@SuppressWarnings("rawtypes")
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
Class<?> clazz = null; // 查找对象
TemplateModel className = null;
String packageName = null;
// 遍历Map
Iterator paramIter = params.entrySet().iterator();
Map.Entry ent;
String paramName;
TemplateModel paramValue;
while (paramIter.hasNext()) {
ent = (Map.Entry) paramIter.next();
paramName = (String) ent.getKey();
paramValue = (TemplateModel) ent.getValue();
// 处理"entity"
if ("entity".equals(paramName)) {
className = paramValue;
}
// 处理"package"
if ("package".equals(paramName)) {
packageName = paramValue.toString().trim();
}
// 处理Id
if ("id".equals(paramName)) {
id = FreeMarkertUtil.getNumberValue(paramName, paramValue)
.intValue();
}
}
/**
* 设置类
*/
if (null != packageName && !"".equals(packageName)) {
clazz = FreeMarkertUtil.findOutClass(className, packageName);
} else {
clazz = FreeMarkertUtil.findOutClass(className);
}
// 检查id
if (id < 1) {
throw new TemplateModelException(
"The \"id\" parameter is required.And the value of it should be larger than 0");
}
// 查询结果
Object obj = basicService.findById(clazz, id);
// // 找不到对象时,创建空对象
// if (null == obj) {
// try {
// obj = clazz.newInstance();
// } catch (Exception e) {
// throw new TemplateModelException(
// "Found no object with the parameter \"id\"=\"" + id+
// "\",error occured while trying to get newInstance of clazz:\""
// + className + "\"");}
// }
// 保存结果集
env.setVariable(RESULT, ObjectWrapper.DEFAULT_WRAPPER.wrap(obj));
if (body != null) {
body.render(env.getOut());
}
}
@Autowired
public void setBasicService(BasicService basicService) {
this.basicService = basicService;
}
}
如上定义,我可以在
a.ftl中使用<@vcms_entity entity="Channel" package="com.vcms.cms.entity" id="1"></@vcms_entity>
这样的方式取得数据,使用方法是访问相应的struts action,然后返回类型<result name="success" type="freemarker">a.ftl</result>
但是,目前我希望采用生成html的方式处理,即:
读入a.ftl模板,生成successPath="a.html"(或者其他名字),然后<result name="html">${successPath}</result>
问题就出现了:
在StaticFreemarker.java中读入的a.ftl,没法执行其中的自定义标签,导致读入模板报错。(执行到[color=#800000]template.process(map, out);这句的时候报错
)[/color]
StaticFreemarker.java
@Component
public class StaticFreemarker {
/**
* 初始化模板引擎
*
* @param ftl
* 模板名称
* @param htmlName
* 需要生成html页面的名称
* @param map
* 模板中需要的参数集合
* @param relativePath
* 模板相对于根路径的相对路径, 例如:/WebRoot/module/test.ftl,则这里传入的参数是module
* @throws IOException
* @throws TemplateException
*/
@SuppressWarnings("rawtypes")
public void init(String ftl, String htmlName, Map map, String relativePath)
throws IOException, TemplateException {
// 获取模板
Configuration cfg = new Configuration();
cfg.setServletContextForTemplateLoading(
ServletActionContext.getServletContext(), "/" + relativePath);
cfg.setEncoding(Locale.getDefault(), "utf-8");
Template template = cfg.getTemplate(ftl);
// 设置模板编码
template.setEncoding("utf-8");
String path = ServletActionContext.getServletContext().getRealPath("/");
File htmlFile = new File(path + htmlName);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(htmlFile), "utf-8"));
template.process(map, out);
out.flush();
out.close();
}
}
报的错误是:
freemarker.core.InvalidReferenceException: Expression user is undefined on line 6, column 19 in a.ftl.
附上a.ftl
[@vcms_list entity="User" package="com.vcms.cms.entity" size=20 page=2 snames="birthday,registerTime"
svalues="2013-02-11,2013-02-13 23:08:32"]
[#list resultset.list as user]
<hr>
${user.userId}
<br>
${user.userName}
<br>
${user.registerTime}
<br>
${user.birthday}
<br>
[/#list]
[/@vcms_list]
请各位大神指教。
以上说了一通,我总结一下:
我希望使用struts+freemarker及自定义freemarker标签生成html,在读入自定义标签模板的时候,报错。断点自定义标签的directive,并没有进入。
...全文
相关推荐
loans
loans
Freemarker教程:模板引擎与Servlet集成实战
本文还有配套的精品资源,点击获取 简介:Freemarker是一个功能强大的模板引擎,用于在Web开发中动态生成文本内容。本教程详细介绍如何在Java Web环境中使用Freemarker,包括其与Servlet和Struts2框架的集成。教程涵盖了Freemarker的基本语法、环境配置以及在Servlet中的应用,旨在帮助学习者掌握Freemarker在视图层设计中的实...
Beetl2.7.14中文文档.pdf
Beetl目前版本是2.7.14,相对于其他java模板引擎,具有功能齐全,语法直观,性能超高,以及编写的模板 容易维护等特点。使得开发和维护模板有很好的体验。是新一代的模板引擎。
总结Java部分的框架和开源项目.docx
总结Java部分的框架和开源项目.docx
Struts2整合Freemarker生成静态页面
NULL 博文链接:https://depravedangel.iteye.com/blog/443575


Web 开发
