聶永的博客

          記錄工作/學(xué)習(xí)的點(diǎn)點(diǎn)滴滴。

          很輕的,Servlet + Freemarker 組合體,沒有那么硬~

          老調(diào)重彈。對SSH經(jīng)典組合有些膩,不再那么輕,重返到若干年前的原始。

          Servlet的輕巧高效,F(xiàn)reemarker的強(qiáng)大簡便,兩者結(jié)合將是超輕的組合,即可避免丑陋的Java代碼和HTML代碼雜揉,又可高效基于模板的站點(diǎn)開發(fā)。

          閑話少說,項(xiàng)目需要:

          freemarker-2.3.13.jar

          servlet.jar

          定義兩個(gè)Servlet:

          HelloAction.java 對應(yīng) /hello,借助Freemarker硬編碼輸出

          public class HelloAction extends HttpServlet {
              private static final long serialVersionUID = -6082007726831320176L;

              private Configuration configuration;
              public void init() throws ServletException {
                  configuration = new Configuration();
                  configuration.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/pages");
                  configuration.setEncoding(Locale.CHINA, "UTF-8");
              }

              @SuppressWarnings("unchecked")
              public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                  // 填充數(shù)據(jù)類型
                  Map map = new HashMap();
                  map.put("userName", "小敏");   
                  Template template = configuration.getTemplate("hello.html");
                  response.setContentType("text/html; charset=" + template.getEncoding());
                  Writer out = response.getWriter();
                  try{
                      template.process(map, out);
                  }catch (TemplateException e) {
                      e.printStackTrace();
                  }
              }

              public void destroy() {
                  super.destroy();
                  if(configuration != null){
                      configuration = null;
                  }
              }
          }

          對應(yīng)模板:

          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <title>使用Freemarker渲染2</title>
          </head>
          <body>
          你好, ${userName!} !
          </body>
          </html>

           

          HiAction.java 對應(yīng) /hi ,借助Freemrker Servlet的攔截功能,如以往寫代碼方式,感覺不到Freemarker的存在。

          public class HiAction extends HttpServlet {
              private static final long serialVersionUID = 518767483952153077L;

              public void doGet(HttpServletRequest request, HttpServletResponse response)
                      throws ServletException, IOException {

                  request.setAttribute("thename", "小敏");
                  request.getRequestDispatcher("/WEB-INF/pages/hi.html").forward(request, response);
              }
          }

          對應(yīng)的模板:

          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <title>使用Freemarker渲染</title>
          </head>
          <body>
          hi ${thename!}~<br />
          </body>
          </html>

          但需要在web.xml 配置文件中定義如下:

          <servlet>
              <servlet-name>freemarker</servlet-name>
              <servlet-class>
                  freemarker.ext.servlet.FreemarkerServlet
              </servlet-class>

              <!-- FreemarkerServlet settings: -->
              <init-param>
                  <param-name>TemplatePath</param-name>
                  <param-value>/</param-value>
              </init-param>
              <init-param>
                  <param-name>NoCache</param-name>
                  <param-value>true</param-value>
              </init-param>
              <init-param>
                  <param-name>ContentType</param-name>
                  <param-value>text/html; charset=UTF-8</param-value>
                  <!-- Forces UTF-8 output encoding! -->
              </init-param>

              <!-- FreeMarker settings: -->
              <init-param>
                  <param-name>template_update_delay</param-name>
                  <param-value>0</param-value><!-- 0 is for development only! Use higher value otherwise. -->
              </init-param>
              <init-param>
                  <param-name>default_encoding</param-name>
                  <param-value>UTF-8</param-value><!-- The encoding of the template files. -->
              </init-param>
              <init-param>
                  <param-name>number_format</param-name>
                  <param-value>0.##########</param-value>
              </init-param>

              <load-on-startup>1</load-on-startup>
          </servlet>

          <servlet-mapping>
              <servlet-name>freemarker</servlet-name>
              <url-pattern>*.html</url-pattern>
          </servlet-mapping>

          使用哪一種組合方式,看您喜好了。

          借助于Freemarker自身的Servlet工具,只是用于攔截Servlet中forward轉(zhuǎn)向使用到的HTML資源文件。

          很簡陋,但湊合著能看。

          項(xiàng)目源代碼已經(jīng)打包如下:

          下載源文件

          posted on 2009-11-10 16:52 nieyong 閱讀(1353) 評論(1)  編輯  收藏 所屬分類: Servlet3

          評論

          # re: 很輕的,Servlet + Freemarker 組合體,沒有那么硬~[未登錄] 2009-11-11 15:58 咖啡妝

          效率是提高了 但是沒有jsp那樣好的設(shè)計(jì)頁面 除非提取出業(yè)務(wù)部分  回復(fù)  更多評論   

          公告

          所有文章皆為原創(chuàng),若轉(zhuǎn)載請標(biāo)明出處,謝謝~

          新浪微博,歡迎關(guān)注:

          導(dǎo)航

          <2009年11月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統(tǒng)計(jì)

          常用鏈接

          留言簿(58)

          隨筆分類(130)

          隨筆檔案(151)

          個(gè)人收藏

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 刚察县| 东乌珠穆沁旗| 磐安县| 香港| 邯郸市| 淮北市| 永寿县| 彝良县| 砀山县| 辰溪县| 安泽县| 桂林市| 彩票| 阿拉善左旗| 台东县| 安吉县| 颍上县| 新干县| 昌邑市| 江源县| 肇源县| 萝北县| 五原县| 涿鹿县| 化隆| 灵台县| 电白县| 襄城县| 长兴县| 安岳县| 通化市| 巴青县| 邳州市| 麻江县| 万载县| 米易县| 顺平县| 台前县| 宣汉县| 高台县| 客服|