package vm2html.struts.action;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
/**
* @author root
*
*/
public abstract class BaseAction extends Action {
public String outPath = "";
public static final String REQUEST = "req";
public static final String RESPONSE = "res";
public static final String CONTENT_TYPE = "default.contentType";
public static final String DEFAULT_CONTENT_TYPE = "text/html";
public static final String FILE_RESULT_OUT_PATH = "file.result.out.path";
public static final String DEFAULT_OUTPUT_ENCODING = "UTF-8";
protected static final String INIT_PROPS_KEY = "org.apache.velocity.properties";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ServletConfig config = this.servlet.getServletConfig();
Velocity.init(loadConfiguration(config));
VelocityContext context = new VelocityContext();
setDefaultContext(context, request, response);
return this.doForward(mapping, form, request, response, context);
}
protected abstract ActionForward doForward(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response, VelocityContext context)
throws Exception;
/**
* @注释 生成Html文件
* @param context
* @param templateFile
* @param outPutFile
* @return
* @throws Exception
* @throws ParseErrorException
* @throws ResourceNotFoundException
*/
public Template doTemplateToFile(VelocityContext context,
String templateFile, String outPutFile)
throws ResourceNotFoundException, ParseErrorException, Exception {
Template template = Velocity.getTemplate(templateFile);
String outFilePath = outPath + outPutFile;
this.makeDir(outFilePath);
PrintWriter dw = new PrintWriter(outFilePath);
template.merge(context, dw);
dw.close();
return template;
}
/**
* @注释 创建文件夹
* @param dir
*/
public void makeDir(String dir) {
java.io.File myFilePath = new java.io.File(dir);
if(!myFilePath.getPath().endsWith("\\")){
int index = myFilePath.getPath().lastIndexOf("\\");
myFilePath = new java.io.File(myFilePath.getPath().substring(0, index));
}
if (!myFilePath.exists())
myFilePath.mkdirs();
}
/**
* @注释:设置默认context值
* @param context
* @param request
* @param response
* @return
*/
private VelocityContext setDefaultContext(VelocityContext context,
HttpServletRequest request, HttpServletResponse response) {
context.put("request", request);
context.put("response", response);
context.put("req", request);
context.put("res", response);
return context;
}
/**
* @注释:解析velocity.Properties
* @param config
* @return Properties
* @throws IOException
* @throws FileNotFoundException
*/
protected Properties loadConfiguration(ServletConfig config)
throws IOException, FileNotFoundException {
String propsFileName = config.getInitParameter(INIT_PROPS_KEY);
if (propsFileName == null) {
propsFileName = "/WEB-INF/velocity.properties";
}
Properties props = new Properties();
if (propsFileName != null) {
String realPath = config.getServletContext().getRealPath(
propsFileName);
if (realPath != null) {
propsFileName = realPath;
}
props.load(new FileInputStream(propsFileName)); // 加载属性配置文件
}
// 将日志文件所在路径加入到Velocity上下文中
String logFileName = props.getProperty(Velocity.RUNTIME_LOG);
if (logFileName != null) {
String log = config.getServletContext().getRealPath(logFileName);
if (log != null) {
props.setProperty(Velocity.RUNTIME_LOG, log);
}
}
// 不使用VM_global_library模板,加入存在则删除Velocity上下文(Context)中
String libraryName = props.getProperty(Velocity.VM_LIBRARY);
if (libraryName != null) {
props.setProperty(Velocity.VM_LIBRARY, "");
}
// 生成文件输出路径
outPath = props.getProperty(FILE_RESULT_OUT_PATH);
outPath = config.getServletContext().getRealPath(
outPath = outPath == null ? "/" : outPath);
outPath = outPath.endsWith("/") ? outPath : outPath + "/";
if (libraryName != null) {
props.setProperty(Velocity.VM_LIBRARY, "");
}
// 将Velocity模板加入到Velocity上下文(Context)中
String path = props.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH);
if (path != null) {
String realPath = config.getServletContext().getRealPath(path);
if (realPath != null) {
props.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, realPath);
}
} else {
String realPath = config.getServletContext().getRealPath("/");
if (realPath != null) {
props.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, realPath);
}
}
// System.out.println("props = " + props);
return props;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Velocity+Struts生成html
共43个文件
jar:14个
xml:7个
tld:6个
5星 · 超过95%的资源 需积分: 9 132 下载量 130 浏览量
2008-12-12
23:42:46
上传
评论
收藏 2.55MB RAR 举报
温馨提示
Velocity+Struts生成html 动态生成html页面,文件夹,新闻发布系统基本原型 有什么不明白的 可以加我QQ:87935707
资源推荐
资源详情
资源评论
收起资源包目录
vm2html.rar (43个子文件)
.project 1KB
.mymetadata 294B
WebRoot
css
_notes
dwsync.xml 202B
common.css 67B
WEB-INF
tools.xml 1KB
web.xml 1KB
struts-config.xml 740B
tld
struts-tiles.tld 32KB
sslext.tld 8KB
struts-bean.tld 45KB
struts-nested.tld 151KB
struts-html.tld 335KB
struts-logic.tld 73KB
template
example1.vm 618B
_notes
dwsync.xml 203B
example.vm 1KB
velocity.properties 1015B
toolbox.xml 2KB
lib
struts-taglib-1.3.8.jar 246KB
commons-logging-1.1.jar 52KB
velocity-tools-2.0-beta3.jar 303KB
commons-collections-3.2.jar 558KB
velocity-1.5.jar 383KB
sslext-1.2-0.jar 26KB
commons-chain-1.1.jar 88KB
oro-2.0.8.jar 64KB
commons-digester-1.8.jar 140KB
commons-beanutils-1.7.0.jar 184KB
commons-lang-2.2.jar 237KB
struts-core-1.3.8.jar 321KB
struts-tiles-1.3.8.jar 117KB
commons-validator-1.3.1.jar 136KB
classes
vm2html
struts
action
BaseAction.class 5KB
Vm2HtmlAction.class 2KB
VM_global_library.vm 2KB
META-INF
MANIFEST.MF 39B
news
test.html 589B
_notes
dwsync.xml 201B
index.vm 562B
.myeclipse
教程.doc 0B
src
vm2html
struts
action
BaseAction.java 5KB
Vm2HtmlAction.java 974B
.classpath 1KB
共 43 条
- 1
jspblog
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- SI4947ADY-T1-E3-VB一款SOP8封装2个P-Channel场效应MOS管
- TeeChart ProFS 2023.38 源码版, 本人一直在用,稳定可靠
- python程序设计:数字类型 转换 运算
- doubleball1.m
- 二层半独栋别墅-12.00&10.80米-施工图.dwg
- SI4940DY-T1-E3-VB一款SOP8封装2个N-Channel场效应MOS管
- 端午节相关庆祝代码案例.zip
- SaaS 短链接系统,承载高并发和海量存储等场景难题 专为实习、校招以及社招而出的最新项目,项目质量不亚于12306铁路购票项目
- TeeChart ProFS 2023.38 VCL 试过各种版本,这个应该是最新最全源码的,本人一直在用 稳定运行
- 嵌入式产品开发.xmind
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页