疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

struts2中使用freemarker 生成静态页面


 

         按一下步骤走:

  1.创建项目

  2.导入struts2的相关jar文件

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

  [html] view plaincopyprint?

  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] view plaincopyprint?

  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

  "http://struts.apache.org/dtds/struts-2.0.dtd">

  ${url}

  5,创建javaBean User.java

  [java] view plaincopyprint?

  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] view plaincopyprint?

  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();

  writer.close();

  }

  }

  7.创建Action文件Struts2.java 具体内容如下:

  [java] view plaincopyprint?

  package org;

  import java.util.ArrayList;

  import java.util.HashMap;

  import java.util.List;

  import java.util.Map;

  import util.CreatHtml;

  import com.opensymphony.xwork2.ActionSupport;

  public class Struts2 extends ActionSupport {

  private String url;

  private List list;

  public String execute() throws Exception {

  CreatHtml creatHtml = new CreatHtml();

  list = new ArrayList();

  User user = null;

  for (int i = 0; i < 10; i++) {

  user = new User();

  user.setId(i);

  user.setName("name" + i);

  user.setAge(i + i);

  list.add(user);

  }

  Map> map = new HashMap>();

  map.put("userList", list);

  String fileName = "user.html";

  String ftl = "temalate.ftl";

  creatHtml.init(ftl, fileName, map);

  // url作为struts.xml中的result的视图返回页面

  this.url = fileName;

  return SUCCESS;

  }

  public List getList() {

  return list;

  }

  public void setList(List list) {

  this.list = list;

  }

  public String getUrl() {

  return url;

  }

  public void setUrl(String url) {

  this.url = url;

  }

  private static final long serialVersionUID = 1L;

  }

  8.创建模板文件在根目录templates文件夹下template.ftl

  [java] view plaincopyprint?

  编号  用户名  年龄

  <#list userList as user>

  ${user.id}  ${user.name}  ${user.age}

  9.浏览器输入http://localhost/Struts2/user.html,访问action