package com.yuan.action;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yuan.util.FreeMarkerUtil;
public class ReleaseNewsAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger(ReleaseNewsAction.class);
private String newsId;
private String title;
private String author;
private String content;
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getNewsId() {
return newsId;
}
public void setNewsId(String newsId) {
this.newsId = newsId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String doRelease() throws Exception {
logger.info("Prepare releasing news...");
ReleaseNewsAction news = new ReleaseNewsAction();
news.setNewsId(newsId);
news.setTitle(title);
news.setAuthor(author);
news.setContent(content);
Map<String, ReleaseNewsAction> root = new HashMap<String, ReleaseNewsAction>();
root.put("releaseNews", news);
String genFilePath = "/news/";
HttpServletRequest request = ServletActionContext.getRequest();
String rootPath = request.getSession().getServletContext().getRealPath(
"/");
File file = new File(rootPath + genFilePath);
if (file.isDirectory()) {
logger.info("存放html文件的文件夹目录是:" + file.getAbsolutePath());
} else {
logger.info(rootPath + genFilePath + "不是可用目录");
// file.mkdirs();
}
String htmlFileName = news.getNewsId() + ".html";
boolean flag = FreeMarkerUtil.getInstance().genHtmlFile(rootPath,
"/news.ftl", root, genFilePath, htmlFileName);
news.setUrl(request.getContextPath() + genFilePath + htmlFileName);
if (flag == true) {
logger.info("生成文件:" + genFilePath + htmlFileName + "成功!");
} else {
return ERROR;
}
request.setAttribute("news", news);
return SUCCESS;
}
}
freemarker Demo 适用于freemarker初学



Freemarker是一个强大的模板引擎,常用于JavaEE应用中的视图层处理,尤其与Struts2等MVC框架配合使用,能实现灵活的动态页面渲染。这个"freemarker Demo"是一个适合初学者的示例项目,旨在帮助新接触Freemarker的开发者快速理解和上手。 1. **Freemarker简介** Freemarker是一个基于模板的开源模板语言,它与Java代码分离,使得开发者可以专注于业务逻辑,而设计师则可以专注于页面设计。它不依赖于任何特定的Web框架,可以在各种Java应用中使用。 2. **模板语法** Freemarker模板主要由文本和控制结构组成,如`<#if>`, `<#foreach>`, `<#assign>`等。例如,`<#if user??>`用来检查用户对象是否存在,`<#foreach item in items>`用于遍历列表,`<#assign name="John">`用于赋值。 3. **数据模型和视图** 在Java应用中,Freemarker通过ModelAndView或者Map对象将数据传递到视图层。开发者可以在Java后端准备数据模型,然后传递给Freemarker模板,模板根据模型中的数据生成HTML输出。 4. **与Struts2集成** Struts2是一个流行的MVC框架,它允许开发者选择多种视图技术,包括Freemarker。在Struts2中配置Freemarker,需要添加相应的插件,并指定Action返回的结果类型为"freemarker"。模板文件通常放在`src/main/resources/WEB-INF/freemarker`目录下。 5. **模板配置** 在`struts.xml`或`struts.properties`中,需要设置Freemarker的相关配置,比如模板的根路径、默认编码等。 6. **模板实例化** 在Struts2的Action类中,你可以创建一个Model对象,填充数据,然后在返回结果时,指定对应的Freemarker模板,如`return "success.ftl"`。 7. **模板变量和表达式** Freemarker支持丰富的表达式,如`${user.name}`用来获取user对象的name属性,`<@util.formatDate date='yyyy-MM-dd'/>`调用自定义宏进行日期格式化。 8. **控制结构** Freemarker提供了条件语句(`<#if>`, `<#else>`, `<#elseif>`)和循环语句(`<#foreach>`),可以实现复杂的逻辑控制。 9. **内置指令和函数** Freemarker有许多内置的指令,如`include`用于包含其他模板,`import`用于导入宏库,`list`用于遍历集合等。同时,还可以自定义函数和宏来扩展功能。 10. **模板调试** Freemarker提供了一个调试模式,可以帮助开发者找出模板中的错误,比如未定义的变量、语法错误等。 这个"freemarker Demo"项目应该包含了基本的模板文件、Struts2配置以及相关的Java代码,通过实际运行和修改这些文件,初学者可以更好地理解Freemarker的工作原理和使用方法。在学习过程中,可以逐步尝试不同的表达式、控制结构和内置功能,加深对Freemarker的理解。



































































- 1

- 粉丝: 11
- 资源: 8
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 代码调试神器:VisualStudio中90%开发者不知道的10个调试技巧.pdf
- 单元测试完全指南:用xUnit为C#代码构建安全防护网.pdf
- 颠覆认知!C#面向对象编程竟能如此简单:类与继承的极简指南.pdf
- 二分查找实战:用C#实现高效数据搜索的5种场景.pdf
- 二维数组取最大值的3种方法:从暴力遍历到LINQ优雅解决.pdf
- 反射与动态编程:C#中那些让你代码更灵活的黑科技.pdf
- 反射与动态编程:用C#实现运行时类型操作的5大应用场景.pdf
- Pear OS镜像文件.zip
- 10个C#初学者必踩的坑:从空引用异常到异步编程误区全解析.pdf
- 5个常见C#编译错误及解决方案:从dll加载到语法纠错.pdf
- 10个C#初学者必练项目:从计算器到文件管理器全解析.pdf
- 10个C#基础语法陷阱:90%新手踩过的坑与避坑指南.pdf
- 10个C#新手必知的语法陷阱,避开这些代码效率翻倍!.pdf
- 10个C#新手必犯的错误及解决方法:从语法陷阱到内存管理全解析.pdf
- 10个必知的C#集合操作技巧:List与Dictionary实战应用.pdf
- 99%初学者都会犯的数据库操作错误,你避开了吗?.pdf



- 1
- 2
- 3
- 4
- 5
- 6
前往页