生成HTML

别忘了模版 UploadFiles/2006-8/817485658.rar

CreateHtml.java

/*
 * CreateHtml.java
 *
 * Created on 2006年8月16日, 上午9:24
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.chen.Common;

import com.chen.Template.TemplateUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class CreateHtml {
   
    //模板路径
    private String tPath;
   
    //生成的html文档的路径
    private String hPath;
   
    //html文档的输出流
    //private OutputStream streamout;
   
    //写html文档的FileWriter
    private FileWriter fw;
   
    //html文档的名称
    private String hName;
   
    //html文档的内容
    private String hContent;
   
    //写html文档成功与否标识:
    public static final int WRITE_ERROR = 0;
   
    public static final int WRITE_OK = 1;
   
    /**
     * Creates a new instance of CreateHtml
     */
    public CreateHtml() {
    }
   
    //传入html的路径,名称,如:CreateHtml("C://file","file.html")
    public CreateHtml(String hPath,String hName) {
        //this.tPath = tPath;
        this.hPath = hPath;
        this.hName = hName;
    }
   
    public CreateHtml(String hPath,String hName,String hContent){
        this.hPath = hPath;
        this.hName = hName;
        this.hContent = hContent;
    }
   
    public void setHContent(String hContent){
        this.hContent = hContent;
    }
   
    public String getHContent(){
        return this.hContent;
    }
   
    public int createHtml(){
        try{
            File f = new File(this.hPath);
            f.mkdirs(); //创建文件路径,文件夹
            //写html文档
            this.fw = new FileWriter(hPath+"//"+this.hName);
            for(int i = 0;i<this.hContent.length();i++){
                fw.write(this.hContent.charAt(i));
            }
            return this.WRITE_OK;
        }catch(IOException e){
            System.out.println(e.getMessage());
            return this.WRITE_ERROR;
        }finally{
            try {
                fw.close();
            } catch (IOException ex) {
                ex.printStackTrace();
                return this.WRITE_ERROR;
            }
        }
    }
   
    //读取模板
    public void readTemplate(){
       
    }
   
    //替换操作
   
    public static void main(String[]  args){
       
        Utility ut = new Utility();
       
        String tPath = "C://testTemplate//build//web//test.html";
       
        TemplateUtils t = new TemplateUtils();
        t.splitTemplate(tPath);
        System.out.println("ok!");
       
        String  user = "as";
        ArrayList bloglist = new ArrayList();
        ArrayList list1 = new ArrayList();
        ArrayList list2 = new ArrayList();
       
        String a0 = "asdf";
        String a1 = "fddda";
        String a2 = "chengtuo";
        String a3 = "gsfdgfsd";
        String a4 = "asdfdafadsf";
       
        String b0 = "asdf";
        String b1 = "fddda";
        String b2 = "chengtuo";
        String b3 = "gsfdgfsd";
        String b4 = "asdfdafadsf";
       
        list1.add(a0);list1.add(a1);
        list1.add(a2);list1.add(a3);
        list1.add(a4);list2.add(b0);
        list2.add(b1);list2.add(b2);
        list2.add(b3);list2.add(b4);
        bloglist.add(list1);
        bloglist.add(list2);
        String rowsn = "";
       
        for (int i = 0; i < 2; i++) {
            String rows = t.getSection("TR_BLOG_LOGIN");
            ArrayList al = (ArrayList) bloglist.get(i);
//            System.out.println("al&&&&&&&&&&&" + al.size());
            String id = (String) al.get(0);
            String blogname = (String) al.get(1);
            //System.out.println("--blogname--$$$$博客名$$$$$$$-" + blogname);
            String chaptercount = (String) al.get(2);
            //System.out.println("--chaptercount---#####文章数#####" + chaptercount);
            String diarycount = (String) al.get(3);
            // System.out.println("--diarycount---####随笔数###" + diarycount);
            String replycount = (String) al.get(4);
            // System.out.println("--replycount---######回复数#####" + replycount);
            rows = rows.replaceAll("//{BLOG_NAME//}", id);
           
            rows = rows.replaceAll("//{LIST_BLOG_BLOGNAME//}", blogname);
            rows = rows.replaceAll("//{LIST_BLOG_WZS//}", chaptercount);
            rows = rows.replaceAll("//{LIST_BLOG_SBS//}", diarycount);
            rows = rows.replaceAll("//{LIST_BLOG_PLS//}", replycount);

            rowsn = rowsn + rows;
        }
        t.writeSection("TR_BLOG_LOGIN", rowsn);
        rowsn = t.merge();

        if (user == null) {
            rowsn = rowsn.replaceAll("//{LIST_BLOG_INDEX_LOGIN//}", "  <tr><td width='22%' height='23' valign='top'><div align='center'><table width='100%' border='0' cellpadding='0' cellspacing='0' bordercolor='#B5DEFA'><tr><td height='9'><form name='form1' method='post' action='blogLogin.do'> <div align='center'> <br> 用户名: <input name='username' type='text' size='8'><br> 密&nbsp;&nbsp;码:<input name='password' type='password' size='8'><br><br><input type='submit' name='Submit' value='登陆'> <input type='reset' name='Reset' value='取消'><a href="/blog/blogreg.html">注册</a> _fcksavedurl=""/blog/blogreg.html">注册</a>" <br> </div> </form> </td> </tr>");
        } else {
            rowsn = rowsn.replaceAll("//{LIST_BLOG_INDEX_LOGIN//}", "<a href="/blog/blogOut.do">注销</a>");
        }
         rowsn = rowsn.replaceAll("//{LIST_BLOG_IXDEX_WZ//}", "首页");
         rowsn = rowsn.replaceAll("//{MY_TEST//}","<p>i want to change!</p>");
        
         //生成html文档
         String hPath = "C://testTemplate//build//web//news//";
         hPath += ut.getPath();

         String hName = ut.getId();
         hName += ".html";
        
         CreateHtml tt = new CreateHtml(hPath,hName);
         tt.setHContent(rowsn);
         if(tt.createHtml()==tt.WRITE_OK){
             System.out.println("create ok!");
         }else{
             System.out.println("create error!");
         }
         System.out.println(rowsn);

    }

}

addArticlesAction.java

/*
 * addArticlesAction.java
 *
 * Created on 2006年8月17日, 上午9:25
 */

package com.chen.Template;

import com.chen.Common.Utility;
import com.chen.Common.CreateHtml;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
/**
 *
 * @author Administrator
 * @version
 */

public class addArticlesAction extends Action {
   
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
   
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String Path = request.getRealPath(request.getContextPath());
        Path = Path.substring(0,Path.lastIndexOf("//"));
        String tPath = Path + "//template//article1.html";
       
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        content = content.replaceAll("/r/n","<p>");
        TemplateUtils t = new TemplateUtils();
        t.splitTemplate(tPath);
        String html = "";
        html = t.merge();
        html = html.replaceAll("//{ARTICLE_TITLE//}",title);
        html = html.replaceAll("//{ARTICLE_CONTENT//}",content);
        System.out.println(html);

        Utility ut = new Utility();
        String hPath = Path + "//news" +ut.getPath();
        String hName = ut.getId()+".html";
       
        CreateHtml tt = new CreateHtml(hPath,hName);
         tt.setHContent(html);
         if(tt.createHtml()==tt.WRITE_OK){
             System.out.println("create ok!");
         }else{
             System.out.println("create error!");
         }
        
        String url = "/news"+ut.getUrl()+"/"+hName;
        request.setAttribute("url",url);
        return new ActionForward(url);
       
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值