81,094
社区成员
发帖
与我相关
我的任务
分享
Configuration freemarkerCfg = new Configuration();
freemarkerCfg.setClassForTemplateLoading(this.getClass(), "/");
freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");
Template template;
try {
template = freemarkerCfg.getTemplate("Hello.ftl");
template.setEncoding("UTF-8");
File htmlFile = new File("webapp/jsp/t.html");
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(htmlFile), "UTF-8"));
HashMap propMap = new HashMap();
propMap.put("title", "文章标题"); //数据模型替换
template.process(propMap, out);
out.flush();
}
<title>查看---文章: ${title} </title>
<div class="list_content">
[@cms.ArtiList count='20' sysContent='1-1' titLen='50' lineHeight='7' dateFormat='1' datePosition='2' headMark='4' bottomLine='1' sysPage='3' isPage='1' pageClass='pagination'/]
</div>
String templeHtml = "<ul>{Tag:type=体育&pagesize=5}</ul><ul>{Tag:type=娱乐&pagesize=5}</ul>";
String htmlCode = "";
String beginTag = "{Tag:";
String endTag = "}";
boolean isFind = false;
int myPos = 0;
int myPosE = 0;
int StartPos = 0; //'在循环中记录当前已经处理到的位置
String tagName = "";
while (!isFind) {
myPos = templeHtml.indexOf(beginTag,StartPos);
if (myPos ==-1 ) {
htmlCode += templeHtml.substring(StartPos);
isFind = true;
} else {
//如果找到了标记起点
//先将这个标记起点和上一个标记终点之间的代码保存到HtmlCode
htmlCode += templeHtml.substring(StartPos,myPos);
myPosE = templeHtml.indexOf(endTag,myPos);
tagName = templeHtml.substring(myPos+5, myPosE);
// tagName = templeHtml.substring(myPos, myPosE+1);
String[] arrayTagName = tagName.split("&");
String context = "";
for (int i = 0; i < arrayTagName.length;i++) {
String[] arrayArgs = arrayTagName[i].split("=");
String argsName = arrayArgs[0]; //参数名称
String argsValue = arrayArgs[1]; //参数值
if (argsName.equals("type")) {
if (argsValue.equals("体育"))
context = "<li>李宁冠军</li>";
else
context = "<li>刘德华演唱会</li>";
}
if (argsName.equals("pagesize")) {
for ( int k = 0; k < Integer.valueOf(argsValue); k++) {
htmlCode += context;
}
}
}
String ss = templeHtml.substring(myPosE);
StartPos = (myPosE+1);
}
System.out.println("---------->"+htmlCode);
}
}
public void doExecute(String location, ActionInvocation invocation) throws IOException, TemplateException {
this.location = location;
this.invocation = invocation;
this.configuration = getConfiguration();
this.wrapper = getObjectWrapper();
this.fileName = (String) conditionalParse(fileName, invocation);
this.staticTemplate = (String) conditionalParse(staticTemplate, invocation);
this.filePath = ((String) conditionalParse(filePath, invocation)) == null ?"":((String) conditionalParse(filePath, invocation));
if (!location.startsWith("/")) {
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest req = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
String base = ResourceUtil.getResourceBase(req);
location = base + "/" + location;
}
//跳转页面的模板类
Template template = configuration.getTemplate(location, deduceLocale());
//转换静态页面的的模板类
Template staticTemplate = configuration.getTemplate(this.staticTemplate,deduceLocale());
TemplateModel model = createModel();
String path = ServletActionContext.getServletContext().getRealPath(filePath)+ File.separator;
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path + fileName)));
if (preTemplateProcess(template, model)) {
try {
//在指定路径生成静态页面
staticTemplate.process(model, out);
//生成跳转的页面
template.process(model, getWriter());
} finally {
postTemplateProcess(template, model);
postTemplateProcess(staticTemplate, model);
}
}
}