4.2. 集成技术开发指南

Beetl默认提供了WebRender用于帮助web集成开发,所有内置的集成均基于此方法。如果你认为Beetl内置的各个web框架集成功能不够,你可以继承此类,或者参考此类源码重新写,其代码如下

  1. package org.beetl.ext.web;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.io.Writer;
  5. import java.util.Enumeration;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.beetl.core.GroupTemplate;
  9. import org.beetl.core.Template;
  10. import org.beetl.core.exception.BeetlException;
  11. /**
  12. * 通常web渲染的类,将request变量赋值给模板,同时赋值的还有session,request,ctxPath
  13. * 其他框架可以继承此类做更多的定制
  14. * @author joelli
  15. *
  16. */
  17. public class WebRender{
  18. GroupTemplate gt = null;
  19. public WebRender(GroupTemplate gt){
  20. this.gt = gt;
  21. }
  22. /**
  23. * @param key 模板资源id
  24. * @param request
  25. * @param response
  26. * @param args 其他参数,将会传给modifyTemplate方法
  27. */
  28. public void render(String key, HttpServletRequest request, HttpServletResponse response, Object... args){
  29. Writer writer = null;
  30. OutputStream os = null;
  31. try{
  32. //response.setContentType(contentType);
  33. Template template = gt.getTemplate(key);
  34. Enumeration<String> attrs = request.getAttributeNames();
  35. while (attrs.hasMoreElements()){
  36. String attrName = attrs.nextElement();
  37. template.binding(attrName, request.getAttribute(attrName));
  38. }
  39. WebVariable webVariable = new WebVariable();
  40. webVariable.setRequest(request);
  41. webVariable.setResponse(response);
  42. webVariable.setSession(request.getSession());
  43. template.binding("session", new SessionWrapper(webVariable.getSession()));
  44. template.binding("servlet", webVariable);
  45. template.binding("request", request);
  46. template.binding("ctxPath", request.getContextPath());
  47. modifyTemplate(template, key, request, response, args);
  48. String strWebAppExt = gt.getConf().getWebAppExt();
  49. if(strWebAppExt!=null){
  50. WebRenderExt renderExt = this.getWebRenderExt(strWebAppExt);
  51. renderExt.modify(template, gt, request, response);
  52. }
  53. if (gt.getConf().isDirectByteOutput()){
  54. os = response.getOutputStream();
  55. template.renderTo(os);
  56. }else{
  57. writer = response.getWriter();
  58. template.renderTo(writer);
  59. }
  60. } catch (IOException e){
  61. handleClientError(e);
  62. } catch (BeetlException e){
  63. handleBeetlException(e);
  64. } finally{
  65. try{
  66. if (writer != null)
  67. writer.flush();
  68. if (os != null)
  69. os.flush();
  70. } catch (IOException e){
  71. handleClientError(e);
  72. }
  73. }
  74. }
  75. /**
  76. * 可以添加更多的绑定
  77. * @param template 模板
  78. * @param key 模板的资源id
  79. * @param request
  80. * @param response
  81. * @param args 调用render的时候传的参数
  82. */
  83. protected void modifyTemplate(Template template, String key, HttpServletRequest request,
  84. HttpServletResponse response, Object... args){
  85. }
  86. /**处理客户端抛出的IO异常
  87. * @param ex
  88. */
  89. protected void handleClientError(IOException ex){
  90. //do nothing
  91. }
  92. /**处理客户端抛出的IO异常
  93. * @param ex
  94. */
  95. protected void handleBeetlException(BeetlException ex){
  96. throw ex;
  97. }
  98. }