Package com.cawring.simple.result.impl

Source Code of com.cawring.simple.result.impl.HtmlResult

package com.cawring.simple.result.impl;

import java.io.IOException;
import java.io.Writer;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cawring.simple.result.Model;
import com.cawring.simple.result.Result;
import com.cawring.simple.result.ViewResult;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
*
* <pre>
* 클래스명 : HTML 렌터링
* 패키지명 : com.cawring.simple.result.impl
* 파일명   : HtmlResult.java
* 설명     :
* freemarker로 화면을 제공한다.
* </pre>
* @Author    : 백승건
* @Date      : 2014. 3. 13.
* @Version   : v.1.0.0 - 2014. 3. 13. 최초작성
*/
public class HtmlResult implements ViewResult
{
  /* logger */
  private static final Logger log = Logger.getLogger(HtmlResult.class.getName());
 
  /* VIEW 파일 확장자 */
  private static final String URL_EXT = ".ftl.html";
 
  /* VIEW 파일경로  */
  private String path;
 
  /* Model */
  private Model model;
 
  /* request */
  private HttpServletRequest request;
 
  /* response */
  private HttpServletResponse response;
 
  /**
   * <pre>
   * 메소드명     : 생성자 메소드
   * 메소드작성일 : 2014. 3. 13.
   * 메소드설명   :
   * 생성시, 인스턴스 변수를 초기화 한다.
   * </pre>
   */
  public HtmlResult(String path, Model model, HttpServletRequest req, HttpServletResponse res)
  {
    this.path     = path;
    this.model    = model;
    this.request  = req;
    this.response = res;
  }
 
  /**
   * <pre>
   * 메소드명     : html Render
   * 메소드작성일 : 2014. 3. 13.
   * 메소드설명   :
   * freemarker로 결과값을 브라우저에 보여준다.
   * </pre>
   */
  public Result render()
  {
    /* request 정보 호출 */
    log.info(request.toString());
   
    /* freemarker configuration */
    Configuration cfg = new Configuration();
   
    /* freemarker path */
    String   ftlPath = null;
   
    try
    {
      /* template call */
      ftlPath  = appendExt(path);
      Template template = cfg.getTemplate(ftlPath);
     
      /* write */
      Writer out = response.getWriter();
     
      /* freemaker call */
      template.process(model.getAll(), out);
    }
    catch (IOException e)
    {
      log.severe(ftlPath+" 해당 파일을 찾을 수 없습니다.");
    }
    catch (TemplateException e)
    {
      log.severe(ftlPath+" 해당 파일에 문제가 발생했습니다.");
    }
   
    return new Result();
  }
 
  /**
   *
   * <pre>
   * 메소드명     : VIEW 파일 확장자 붙이기
   * 메소드작성일 : 2014. 3. 13.
   * 메소드설명   : 메스드 설명을 작성한다.
   * </pre>
   * @param path : 파일경로및파일명 - base/index
   * @return 확장자 붙임 - base/index.ftl.html
   */
  private String appendExt(String path)
  {
    return path+URL_EXT;
  }
}
TOP

Related Classes of com.cawring.simple.result.impl.HtmlResult

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.