struts2中使用freemarker 生成静态页面 (一)

2014-11-24 11:01:04 · 作者: · 浏览: 2

按一下步骤走:

1.创建项目

2.导入struts2的相关jar文件

3.在web.xml中配置如下:


[html]
< xml version="1.0" encoding="UTF-8" >
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">


struts2
org.apache.struts2.dispatcher.FilterDispatcher


struts2
/*




index.jsp

< xml version="1.0" encoding="UTF-8" >
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">


struts2
org.apache.struts2.dispatcher.FilterDispatcher


struts2
/*



index.jsp


4.创建struts.xml文件,具体内容如下:

在配置视图类型时,也可以直接用type="freemarker"这个访问指定的模板,在这里我用的是动态访问生成的html页面

[html]
< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">




${url}


< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">




${url}



5,创建javaBean User.java


[java]
package org;

public class User {
private String name;
private int id;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}


}

package org;

public class User {
private String name;
private int id;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}


}
6.然后创建CreatHtml.java,执行时调用模板类生成指定的页面


[java]
package util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class CreatHtml {
public void init(String ftl, String htmlName, Map map) throws IOException,TemplateException {
// 创建Configuration对象
Configuration cfg = new Configuration();
// 设置FreeMarker的模版文件位置
cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "templates");
cfg.setEncoding(Locale.getDefault(), "utf-8");


// 创建Template对象
Template template = cfg.getTemplate(ftl);
template.setEncoding("utf-8");


// 生成静态页面
String path = ServletActionContext.getServletContext().getRealPath("/");
File fileName = new java.io.File(path + htmlName);
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
template.process(map, writer);
writer.flush()