81,092
社区成员
发帖
与我相关
我的任务
分享
/**
* 通用取数据对象模板(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;
}
}
@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();
}
}
[@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]
[@vcms_entity entity="Site" id=1]
${obj.siteId}-${obj.siteName}-${obj.sitePath}-${obj.finalStep}-${obj.protocol}
[/@vcms_entity]
所以第5行就是 ${obj.siteId}-${obj.siteName}-${obj.sitePath}-${obj.finalStep}-${obj.protocol}
protected Configuration updateConfiguration(Configuration configuration)
throws TemplateException {
// 设置标签类型([]、<>),[]这种标记解析要快些
configuration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
// 设置允许属性为空
configuration.setClassicCompatible(true);
// 取出上下文
ApplicationContext applicationContext = WebApplicationContextUtils
.getRequiredWebApplicationContext(ServletActionContext
.getServletContext());
// 获取实现TemplateDirectiveModel的bean
Map<String, TemplateDirectiveModel> beans = applicationContext
.getBeansOfType(TemplateDirectiveModel.class);
for (String key : beans.keySet()) {
Object obj = beans.get(key);
if (obj != null && obj instanceof TemplateDirectiveModel) {
configuration.setSharedVariable(key, obj);
}
}
return configuration;
}
在 Template template = cfg.getTemplate(ftl);加cfg=updateConfiguration(cfg);
OK~
[@vcms_entity entity="Site" id=1]
${obj.siteId}-${obj.siteName}-${obj.sitePath}-${obj.finalStep}-${obj.protocol}
[/@vcms_entity]
其对应报错是
freemarker.core.InvalidReferenceException: Expression obj is undefined on line 5, column 11 in a.ftl.
public String testHtml() throws IOException, TemplateException {
successPath = "fuckyou.html";
staticFreemarker.init("a.ftl", successPath, null, "test");
return "html";
}
和对应的struts.xml
<package name="test" extends="json-default" namespace="/test">
<action name="*_*" class="{1}Action" method="{2}">
<result name="success" type="freemarker" >/test/test.ftl</result>
<result name="html" >/${successPath}</result>
</action>
</package>