struts2+freemarker 生成静态页面

 

struts2+freemarker 生成静态页面

1.创建项目

2.导入struts2的相关jar文件

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

< ?xml version= "1.0"  encoding= "UTF-8" ?>
< 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" >
 < display-name>MyFreemark< /display-name>
  < filter>
        < filter-name>struts< /filter-name>
        < filter-class>org.apache.struts2.dispatcher.FilterDispatcher< /filter-class>        
    < /filter>
    < filter-mapping>
        < filter-name>struts< /filter-name>
        < url-pattern>/*< /url-pattern>
    < /filter-mapping>
 < welcome-file-list>
  < welcome-file>index.jsp< /welcome-file>  
 < /welcome-file-list>
< /web-app>

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

< ?xml version= "1.0"  encoding= "GBK" ?>
< !DOCTYPE struts PUBLIC
 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts>
  <!--- Add packages here -->
 < constant name= "struts.devMode"  value= "true"  />
 < constant name= "struts.i18n.encoding"  value= "GBK"  />
 < package name= "default"  namespace= "/"  extends= "struts-default" >
  < action name= "list"  class= "com.zsw.action.ListAction" >
   < result type= "redirect" >/${msg}< /result>
  < /action>
 < /package>
< /struts>

5.创建CreateHtml.java用来生成静态页面

package com.zsw.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 CreateHtml {
  /**
  * 
  * @param ftl  模板文件
  * @param htmlName html文件名称
  * @param map    map保存数据
  * @param relaPath  //在这里没有用到
  * @throws IOException
  * @throws TemplateException
  */

  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<Person> persons =  new ArrayList<Person>();
   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< String, List<Person>> map =  new HashMap< String, List<Person>>();
  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

< 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>   
    < tr>   
        < td>< b>编号< /b>< /td>   
        < td>< b>用户名< /b>< /td>   
        < td>< b>密码< /b>< /td>   
        < td>< b>性别< /b>< /td>   
        < td>< b>年龄< /b>< /td>   
    < /tr>
    < #list personlist as person>   
    < tr>   
        < td>${person.id}< /td>   
        < td>${person.name}< /td>
        < td>${person.sex}< /td>   
        < td>${person.age}< /td>   
    < /tr>
 < /#list>
< /table>

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

效果图如下:

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

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

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值