struts2+freemarker 生成静态…
来源:互联网 发布:数据集中管理 编辑:程序博客网 时间:2024/06/07 07:34
struts2+freemarker 生成静态页面
1.创建项目
2.导入struts2的相关jar文件
3.在web.xml中配置如下:
<<span style="color: rgb(128, 128, 0);">?xml version="1.0" encoding= "UTF-8"?>
<<span style="color: rgb(128, 128,0);">web-appid="WebApp_ID" version= "2.4" xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<<span style="color: rgb(128, 128,0);">display-name>MyFreemark<<span style="color:rgb(128, 128, 0);">/display-name>
<<span style="color:rgb(128, 128, 0);">filter>
<<spanstyle="color: rgb(128, 128,0);">filter-name>struts<<span style="color: rgb(128,128, 0);">/filter-name>
<<spanstyle="color: rgb(128, 128,0);">filter-class>org.apache.struts2.dispatcher.FilterDispatcher<<spanstyle="color: rgb(128, 128,0);">/filter-class>
<<spanstyle="color: rgb(128, 128, 0);">/filter>
<<spanstyle="color: rgb(128, 128, 0);">filter-mapping>
<<spanstyle="color: rgb(128, 128,0);">filter-name>struts<<span style="color: rgb(128,128, 0);">/filter-name>
<<spanstyle="color: rgb(128, 128, 0);">url-pattern>
public void init(String ftl, String htmlName, Map map, String relaPath) throws IOException, TemplateException {
//创建Configuration对象
Configuration cfg = new Configuration();
cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "/");
cfg.setEncoding(Locale.getDefault(), "gbk");
//创建Template对象
Template template = cfg.getTemplate(ftl);
template.setEncoding("gbk");
//生成静态页面
String path = ServletActionContext.getServletContext().getRealPath("/");
File fileName = new File(path + htmlName);
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "gbk"));
template.process(map, out);
out.flush();
out.close();
}
}
<<span style="color: rgb(128, 128,0);">web-app
}
6.创建Action文件ListAction.java,如下:
package com.zsw.action;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importcom.opensymphony.xwork2.ActionSupport;
importcom.zsw.util.CreateHtml;
importcom.zsw.vo.Person;
importfreemarker.template.TemplateException;
publicclass ListAction extends ActionSupport {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String execute() {
CreateHtml createHtml = new CreateHtml();
List persons = new ArrayList();
for (int i = 0; i < 5; i++) {
Person p = new Person();
p.setId(i);
p.setName("a" + "1");
p.setAge("5");
p.setSex("man");
persons.add(p);
}
Map<<span style="color:rgb(128, 128,0);">String, List> map = new HashMap<<spanstyle="color: rgb(128, 128,0);">String, List>();
map.put("personlist", persons);
String htmlName = "personlist.html";
String ftl = "person.ftl";
String relaPath = "index.jsp";
try {
createHtml.init(ftl, htmlName, map, relaPath);
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
this.msg = htmlName;
return SUCCESS;
}
}
import
import
import
import
import
import
import
import
import
public
}
7.创建模板文件person.ftl
<<span style="color: rgb(128, 128, 0);">table style="text-align:center;FONT-SIZE: 11pt; WIDTH: 600px; FONT-FAMILY: 宋体; BORDER-COLLAPSE: collapse" borderColor=#3399ff cellSpacing=0 cellPadding=0 align=center border=1 >
<<spanstyle="color: rgb(128, 128, 0);">tr>
<<spanstyle="color: rgb(128, 128, 0);">td><<span style="color:rgb(128, 128, 0);">b>编号<<span style="color: rgb(128,128, 0);">/b><<span style="color: rgb(128, 128,0);">/td>
<<spanstyle="color: rgb(128, 128, 0);">td><<span style="color:rgb(128, 128, 0);">b>用户名<<span style="color: rgb(128,128, 0);">/b><<span style="color: rgb(128, 128,0);">/td>
<<spanstyle="color: rgb(128, 128, 0);">td><<span style="color:rgb(128, 128, 0);">b>密码<<span style="color: rgb(128,128, 0);">/b><<span style="color: rgb(128, 128,0);">/td>
<<spanstyle="color: rgb(128, 128, 0);">td><<span style="color:rgb(128, 128, 0);">b>性别<<span style="color: rgb(128,128, 0);">/b><<span style="color: rgb(128, 128,0);">/td>
<<spanstyle="color: rgb(128, 128, 0);">td><<span style="color:rgb(128, 128, 0);">b>年龄<<span style="color: rgb(128,128, 0);">/b><<span style="color: rgb(128, 128,0);">/td>
<<spanstyle="color: rgb(128, 128, 0);">/tr>
<<spanstyle="color: rgb(128, 128,0);">#list personlist as person>
<<spanstyle="color: rgb(128, 128, 0);">tr>
<<spanstyle="color: rgb(128, 128, 0);">td>${person.id}<<spanstyle="color: rgb(128, 128, 0);">/td>
<<spanstyle="color: rgb(128, 128, 0);">td>${person.name}<<spanstyle="color: rgb(128, 128, 0);">/td>
<<spanstyle="color: rgb(128, 128, 0);">td>${person.sex}<<spanstyle="color: rgb(128, 128, 0);">/td>
<<spanstyle="color: rgb(128, 128, 0);">td>${person.age}<<spanstyle="color: rgb(128, 128, 0);">/td>
<<spanstyle="color: rgb(128, 128, 0);">/tr>
<<span style="color: rgb(128, 128,0);">/#list>
<<span style="color: rgb(128, 128, 0);">/table>
<<span style="color: rgb(128, 128, 0);">/table>
8.启动服务器,访问地址:http://localhost:8080/MyFreemark/list
效果图如下:
9.以上例子下载地址如下:
http://download.csdn.net/source/3490291
转:http://blog.csdn.net/sz_bdqn/article/details/6655564
0 0
- struts2+freemarker 生成静态…
- struts2+freemarker 生成静态页面
- struts2+freemarker 生成静态页面
- Struts2整合Freemarker生成静态页面
- struts2和freemarker生成静态页面
- struts2中使用freemarker 生成静态页面
- struts2中使用freemarker 生成静态页面
- struts2中使用freemarker 生成静态页面
- FreeMarker & Struts2
- freemarker 生成静态面
- Freemarker 生成静态页
- Freemarker生成静态页面
- freemarker生成静态页面
- freemarker 生成静态文件
- FreeMarker生成静态页
- freemarker生成静态页面
- FreeMarker生成静态页面
- FreeMarker生成静态页
- iBatis整理——iBatis批处理实现(Sp…
- ibatis解决sql注入问题
- cmd模式下的adb命令不能用
- android访问网络和SD卡必须配置
- SELECT @@IDENTITY as&n…
- struts2+freemarker 生成静态…
- Spring管理hibernate
- ibatis的一些小知识
- 后台封装数据成XML
- 两个ViewController间传值--回传
- 解析后台返回的XML
- 创建第一个NDK程序
- android开发小问题(不时更新)
- Sax解析XML,localName和qName的区…