freemarkerservlet 重写

package com.sinovatech.cncsns.common;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sinovatech.cncsns.goods.model.facade.ProductFacade;
import com.sinovatech.cncsns.message.model.facade.BulletinFacade;
import com.sinovatech.cncsns.message.model.facade.LabelFacade;
import com.sinovatech.cncsns.search.model.facade.SearchFacade;
import com.sinovatech.cncsns.user.model.facade.CommunityFacade;
import com.sinovatech.cncsns.user.model.facade.DistrictFacade;
import com.sinovatech.common.exception.AppException;

import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.servlet.FreemarkerServlet;
import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateModel;

public class OutHtmlServlet extends FreemarkerServlet {
 private Configuration config;
 private ObjectWrapper wrapper = new BeansWrapper();
 HashMap hash = new HashMap();
 
 
 ApplicationContext app = new ClassPathXmlApplicationContext(
   "/spring-cfg/applicationContext*.xml");

 public OutHtmlServlet() {
  config = new Configuration();
  // - 模板跟新时间
  config.setTemplateUpdateDelay(0);
  // - error处理方式
  config
    .setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
  // - 包装器

  // - 文件读取编码
  config.setDefaultEncoding("GBK");

  // - 输出编码
  config.setOutputEncoding("GBK");
  config.setLocale(Locale.CHINA);

 
 }

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  config.setServletContextForTemplateLoading(this.getServletContext(),
    "/WEB-INF");
  try {
   process(request, response);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  config.setServletContextForTemplateLoading(this.getServletContext(),
    "/");
  try {
   process(request, response);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 private void process(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException,
   Exception {
  Template template;
  ServletContext servletContext;
  servletContext = getServletContext();
  if (preprocessRequest(request, response))
   return;
  String path = requestUrlToTemplatePath(request);
  if (debug)
   log("Requested template: " + path);
  template = null;
  try { // TemplateLoader tem= config.getTemplateLoader();
   // tem.findTemplateSource("include.ftl");
   template = config.getTemplate("include.ftl");
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  Object attrContentType = template.getCustomAttribute("content_type");
  if (attrContentType != null)
   response.setContentType(attrContentType.toString());
  response.setCharacterEncoding("GBK");

  BulletinFacade myBulletinFacade = (BulletinFacade) app
    .getBean("myBulletinFacade");
  DistrictFacade myDistrictFacade = (DistrictFacade) app
    .getBean("myDistrictFacade");
  CommunityFacade myCommunityFacade = (CommunityFacade) app
    .getBean("myCommunityFacade");
  LabelFacade myLabelFacade = (LabelFacade) app.getBean("myLabelFacade");
  SearchFacade mySearchFacade = (SearchFacade) app
    .getBean("mySearchFacade");
  ProductFacade myProductFacade = (ProductFacade) app
    .getBean("myProductFacade");
  List listProductType = new ArrayList();
  List products = new ArrayList();
  List newNeeds = new ArrayList();
  List hotworld = new ArrayList();
  List tips = new ArrayList();
  List newsproduct = new ArrayList();
  List top10 = new ArrayList();
  try {
   listProductType = mySearchFacade.listOrderByCatId();
   request.setAttribute("listProductType", listProductType);

   // 开始逛摊儿

   products = mySearchFacade.listProductByCategory(listProductType);

   // 开始最新需求
   request.setAttribute("products", products);
   newNeeds = mySearchFacade.listLatestByCategory(listProductType);
   request.setAttribute("newNeeds", newNeeds);
   // 取得所有热词
   hotworld = mySearchFacade.listHotWorld();
   request.setAttribute("hotworld", hotworld);
   // 秘笈在label表中换物分类
   String pids = "3";
   tips = myBulletinFacade.listByTLabelDTO(pids);
   request.setAttribute("tips", tips);
   // 最新闲置
   newsproduct = myProductFacade.ListNewestProduct(listProductType);

   // 新鲜top10
   request.setAttribute("newsproduct", newsproduct);
   top10 = mySearchFacade.ListTopNewestProduct(listProductType);

   request.setAttribute("top10", top10);
   // 人气top10
   List visits = mySearchFacade.ListVisits();
   request.setAttribute("visits", visits);
  } catch (AppException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  TemplateModel model;
  wrapper.wrap(hash);
  model = super.createModel(wrapper, servletContext, request, response);
  if (!super.preTemplateProcess(request, response, template, model))  ;
   final  ByteArrayOutputStream os = new ByteArrayOutputStream();   
   final   PrintWriter pw = new PrintWriter("indexother.html");   
   final ReServletOutputStream stream=new ReServletOutputStream();
     HttpServletResponse rep = new HttpServletResponseWrapper(response) {   
          public ServletOutputStream getOutputStream() {   
           return stream;   
          }   
          public PrintWriter getWriter() {   
           return pw;   
          }
   };
  template.process(model,rep.getWriter());
  super.postTemplateProcess(request, response, template, model);
  
  //FileOutputStream fie = new FileOutputStream("xxxb.html");
        
      pw.flush();
    //  stream.getByteout().writeTo(fie);
     
      os.close();
    //  fie.close();
     

}
 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值