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-app id="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();
  
 }
}

6.创建Action文件ListAction.java,如下:

package com.zsw.action;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.zsw.util.CreateHtml;
import com.zsw.vo.Person;
import freemarker.template.TemplateException;
public class 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 0; 5; i++) {
   Person 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;
 }
}

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>

8.启动服务器,访问地址:http://localhost:8080/MyFreemark/list

效果图如下:

9.以上例子下载地址如下:

http://download.csdn.net/source/3490291


转:http://blog.csdn.net/sz_bdqn/article/details/6655564

0 0