freemark

templtUtil:
public static final String P_TEMPLATE = "p.ftl";
public static final String TEMPLATE = "pr.ftl";
public static final String TEMPLATE_PATH = "/vm";
public static final String PREVIEW_HTML = "/pr.html";

public static Template configTemplate(DynamoHttpServletRequest request, String temp) throws IOException {
Configuration config = new Configuration();
ServletContext sc = request.getSession().getServletContext();
config.setDirectoryForTemplateLoading(new File(sc.getRealPath(TEMPLATE_PATH)));
config.setObjectWrapper(new DefaultObjectWrapper());
Template template = config.getTemplate(temp, "UTF-8");
return template;
}

public static void toPreview(DynamoHttpServletRequest request, String temp, Map<?, ?> root){
try {
String previewPath = request.getSession().getServletContext().getRealPath("")+PREVIEW_HTML;
Template template = configTemplate(request, temp);
FileOutputStream fos = new FileOutputStream(previewPath);
Writer out = new OutputStreamWriter(fos, "UTF-8");
template.process(root, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

servlet代码:
public boolean promotionPicPreview(DynamoHttpServletRequest request,DynamoHttpServletResponse response) {
if (promotionPicRequest(request, response)) {
try {
response.setContentType("text/plain;charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
response.sendRedirect(this.PREVIEW);
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}

public boolean promotionPicSave(DynamoHttpServletRequest request,DynamoHttpServletResponse response) {
try {
if (promotionPicRequest(request, response)) {
File previewFile = new File(request.getSession().getServletContext().getRealPath(CreateHtmlUtil.PREVIEW_HTML));
InputStream is = new FileInputStream(previewFile);
response.reset();
response.setContentType("application/x-msdownload;charset=UTF-8");
response.addHeader("Content-Disposition","attachment; filename=\"" + PROMOTION_HTML + "\"");
byte[] b = new byte[1024];
int len;
while ((len=is.read(b)) >0) {
response.getOutputStream().write(b,0,len);
}
is.close();
response.getOutputStream().flush();
response.getOutputStream().close();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}

public boolean promotionPicRequest(DynamoHttpServletRequest request,DynamoHttpServletResponse response){
String topBanner = request.getParameter(TOP_BANNER);
String backColor = request.getParameter(BACK_COLOR);
String moreActivity = request.getParameter(MORE_ACTIVITY);
String[] banner = request.getParameterValues(BANNER);
String[] pic1 = request.getParameterValues(PIC_1);
String[] picLink1 = request.getParameterValues(PIC_LINK_1);
String[] pic2 = request.getParameterValues(PIC_2);
String[] picLink2 = request.getParameterValues(PIC_LINK_2);
String[] pic3 = request.getParameterValues(PIC_3);
String[] picLink3 = request.getParameterValues(PIC_LINK_3);
String[] pic4 = request.getParameterValues(PIC_4);
String[] picLink4 = request.getParameterValues(PIC_LINK_4);
try {
Map<String, Object> root = new HashMap<String, Object>();
List<Map<String, Object>> lines = new ArrayList<Map<String, Object>>();
for (int i=0; i < pic1.length; i++) {
String ban = banner[i];
Map<String, Object> line = new HashMap<String, Object>();
List<Map> picsList = new ArrayList<Map>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("pic1", pic1[i]);
map.put("pic2", pic2[i]);
map.put("pic3", pic3[i]);
map.put("pic4", pic4[i]);
map.put("picLink1",picLink1[i]);
map.put("picLink2", picLink2[i]);
map.put("picLink3", picLink3[i]);
map.put("picLink4", picLink4[i]);
picsList.add(map);
line.put("pics", picsList);
line.put("banner", ban);
lines.add(line);
}
root.put("top_banner", topBanner);
root.put("back_color", backColor);
root.put("more_activity", moreActivity);
root.put("lines", lines);

CreateHtmlUtil.toPreview(request, CreateHtmlUtil.PIC_TEMPLATE, root);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

模板代码:
<#list lines as line>
<#if line.xxx != "" >

</#if>
<#list line.ps as p>
${p.xxx}
</#list>
</#list>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值