日系框架之Mayaa:HTML模板引擎

官方主页:http://mayaa.seasar.org/

如下图所示建立工程:

所需lib包一览:

commons-beanutils-core-1.7.0.jar
commons-collections-3.1.jar
commons-logging-1.0.4.jar
jaxen-1.1.1.jar
mayaa-1.1.20.jar
nekohtml-0.9.5.jar
rhino-1.6r5.jar
xercesImpl-2.7.1.jar
xml-apis-1.3.03.jar
jstl.jar
standard.jar

 

代码依次如下:

MyBean.java

  1. package example;
  2. import java.math.BigDecimal;
  3. import java.util.Date;
  4. public class MyBean {
  5.     private int _id = 1000;
  6.     private String _name = "MyBean name";
  7.     private BigDecimal _decimal = new BigDecimal("12.345");
  8.     private Date _timestamp = new Date();
  9.     
  10.     public int get_id() {
  11.         return _id;
  12.     }
  13.     public void set_id(int _id) {
  14.         this._id = _id;
  15.     }
  16.     public String get_name() {
  17.         return _name;
  18.     }
  19.     public void set_name(String _name) {
  20.         this._name = _name;
  21.     }
  22.     public BigDecimal get_decimal() {
  23.         return _decimal;
  24.     }
  25.     public void set_decimal(BigDecimal _decimal) {
  26.         this._decimal = _decimal;
  27.     }
  28.     public Date get_timestamp() {
  29.         return _timestamp;
  30.     }
  31.     public void set_timestamp(Date _timestamp) {
  32.         this._timestamp = _timestamp;
  33.     }
  34. }

base.css

  1. * {
  2.     margin0;
  3.     padding0;
  4.     text-indent0;
  5. }
  6. body {
  7.     text-aligncenter;
  8.     font-familysans-serif;
  9.     color#483d8b;
  10.     backgroundwhite;
  11. }
  12. body div {
  13.     text-alignleft;
  14.     margin0 auto;
  15. }
  16. .header {
  17.     colorwhite;
  18.     border-bottom6px double white;
  19.     background#1c4baf;
  20. }
  21. .header h1 {
  22.     padding8px;
  23.     border-bottom4px double white;
  24. }
  25. .container {
  26. }
  27. .main {
  28.     font-size100%;
  29.     padding8px;
  30.     margin-left216px;
  31. }
  32. .sidebar {
  33.     floatleft;
  34.     top: 4em;
  35.     left: 10px;
  36.     width200px;
  37.     padding8px;
  38. }
  39. .sidebar h1,h2,h3,h4,h5 {
  40.     font-size100%;
  41.     margin-top1em;
  42.     margin-bottom0.2em;
  43. }
  44. .sidebar h1 {
  45.     border-left4px solid #1c4baf;
  46.     padding-left2px;
  47. }
  48. .sidebar ul {
  49.     font-size100%;
  50.     margin-top0.2em;
  51.     margin-left1.5em;
  52. }
  53. .footer {
  54.     text-aligncenter;
  55.     colorwhite;
  56.     border-top6px double white;
  57.     background#1c4baf;
  58.     clearboth;
  59. }
  60. div.box {
  61.     text-aligncenter;
  62.     borderdotted #aaf 2px;
  63.     background-color#ffe;
  64.     padding10px;
  65.     margin2px;
  66. }

dump.js

  1. function printApplication() {
  2.     var names = application.iterateAttributeNames();
  3.     while (names.hasNext()) {
  4.         var name = names.next();
  5.         print("app: " + name + " = " + application[name]);
  6.     }
  7. }
  8. function printSession() {
  9.     var names = session.iterateAttributeNames();
  10.     while (names.hasNext()) {
  11.         var name = names.next();
  12.         print("ses: " + name + " = " + session[name]);
  13.     }
  14. }
  15. function printAttribute() {
  16.     var names = request.iterateAttributeNames();
  17.     while (names.hasNext()) {
  18.         var name = names.next();
  19.         print("att: " + name + " = " + request[name]);
  20.     }
  21. }
  22. function printParameter() {
  23.     var names = param.iterateAttributeNames();
  24.     while (names.hasNext()) {
  25.         var name = names.next();
  26.         print("prm: " + name + " = " + param[name]);
  27.     }
  28. }
  29. function printHeader() {
  30.     var names = header.iterateAttributeNames();
  31.     while (names.hasNext()) {
  32.         var name = names.next();
  33.         print("hed: " + name + " = " + header[name]);
  34.     }
  35. }
  36. function printBinding() {
  37.     var names = binding.iterateAttributeNames();
  38.     while (names.hasNext()) {
  39.         var name = names.next();
  40.         print("bid: " + name + " = " + binding[name]);
  41.     }
  42. }
  43. function print(value) {
  44.     java.lang.System.out.println("" + value);
  45. }
  46. print("------ req-path: " + request.getRequestedPath());
  47. printSession();
  48. printAttribute();
  49. printParameter();
  50. printHeader();
  51. printBinding();

script.js

  1. var obj = {
  2.      run: function() {
  3.          return 'こんにちは';
  4.      }
  5. };

exec.html

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
  2. <html xmlns:c="http://java.sun.com/jstl/core_rt">
  3. <head>
  4.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5.     <meta http-equiv="Cache-Control" content="no-cache">
  6.     <title>Mayaa samples</title>
  7.     <link rel="stylesheet" href="../css/base.css" type="text/css">
  8. </head>
  9. <body id="exec">
  10.     <div class="header">
  11.         <h1>Mayaa Samples</h1>
  12.     </div>
  13.     <div class="container">
  14.         <div class="sidebar"><span>
  15.             <h2>メニュー</h2>
  16.             <ul>
  17.                 <li><a href="../index.html">戻る</a></li>
  18.             </ul>
  19.         </span></div>
  20.         <div class="main">
  21.             <h2>mayaa exec processor</h2>
  22.             <div id="write" class="box">
  23.                     テンプレート上のダミー文字列
  24.             </div>
  25.         </div>
  26.         <div class="footer">
  27.             ©The Seasar Foundation.
  28.         </div>
  29.     </div>
  30. </body>
  31. </html>

exec.mayaa

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <m:mayaa xmlns:m="http://mayaa.seasar.org">
  3.     <m:exec id="exec" replace="false" 
  4.         src="../js/script.js" encoding="UTF-8"/>
  5.     <m:write id="write" replace="false" value="${ obj.run(); }"/>
  6. </m:mayaa>

load.html

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
  2. <html xmlns:c="http://java.sun.com/jstl/core_rt">
  3. <head>
  4.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5.     <meta http-equiv="Cache-Control" content="no-cache">
  6.     <title>Mayaa samples</title>
  7.     <link rel="stylesheet" href="../css/base.css" type="text/css">
  8. </head>
  9. <body>
  10.     <div class="header">
  11.         <h1>Mayaa Samples</h1>
  12.     </div>
  13.     <div class="container">
  14.         <div class="sidebar"><span>
  15.             <h2>メニュー</h2>
  16.             <ul>
  17.                 <li><a href="../index.html">戻る</a></li>
  18.             </ul>
  19.         </span></div>
  20.         <div class="main">
  21.             <h2>Script load Sample</h2>
  22.             <div id="write" class="box">
  23.                     テンプレート上のダミー文字列
  24.             </div>
  25.         </div>
  26.         <div class="footer">
  27.             ©The Seasar Foundation.
  28.         </div>
  29.     </div>
  30. </body>
  31. </html>

load.mayaa

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <m:mayaa xmlns:m="http://mayaa.seasar.org"
  3.         m:noCache="true">
  4.     <m:write id="write" replace="false"
  5.         value="${ load('../js/script.js', 'UTF-8'); obj.run(); }"/>
  6. </m:mayaa>

usebean.html

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
  2. <html xmlns:c="http://java.sun.com/jstl/core_rt">
  3. <head>
  4.     <title>use bean</title>
  5. </head>
  6. <body>
  7.     <h1>Use bean</h1>
  8.     <table border="1">
  9.     <tr>
  10.         <th>property</th>
  11.         <th>value</th>
  12.     </tr>
  13.     <tr>
  14.         <td>id</td>
  15.         <td><span id="id">10 dummy</span>;</td>
  16.     </tr>
  17.     <tr>
  18.         <td>name</td>
  19.         <td><span id="name">dummy name</span></td>
  20.     </tr>
  21.     <tr>
  22.         <td>decimal</td>
  23.         <td><span id="decimal">1.2 dummy</span></td>
  24.     </tr>
  25.     <tr>
  26.         <td>timestamp</td>
  27.         <td><span id="timestamp">1970/1/1 dummy</span></td>
  28.     </tr>
  29.     </table>
  30. </body>
  31. </html>

usebean.mayaa

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <m:mayaa xmlns:m="http://mayaa.seasar.org"
  3.         xmlns:fmt="http://java.sun.com/jsp/jstl/fmt">
  4.     <m:beforeRender>
  5.         request.setAttribute('bean', new Packages.example.MyBean());
  6.     </m:beforeRender>
  7.     <m:write m:id="id" value="${ bean._id }" />
  8.     <m:write m:id="name" value="${ bean._name }" />
  9.     <fmt:formatNumber m:id="decimal" value="${ bean._decimal }" />
  10.     <fmt:formatDate m:id="timestamp" value="${ bean._timestamp }" />
  11. </m:mayaa>

web.xml

  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <!DOCTYPE web-app
  3.     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  4.     "http://java.sun.com/dtd/web-app_2_3.dtd">
  5. <web-app>
  6.     <servlet>
  7.         <servlet-name>MayaaServlet</servlet-name>
  8.         <servlet-class>org.seasar.mayaa.impl.MayaaServlet</servlet-class>
  9.         <load-on-startup>1</load-on-startup>
  10.     </servlet>
  11.     <servlet-mapping>
  12.         <servlet-name>MayaaServlet</servlet-name>
  13.         <url-pattern>*.html</url-pattern>
  14.     </servlet-mapping>
  15. </web-app>

debug.js //设置控制台debug是否启用

  1. /* set true/false */
  2. var debug = true;
  3. if (debug) {
  4.     load("js/dump.js");
  5. }

default.mayaa

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <m:mayaa xmlns:m="http://mayaa.seasar.org">
  3.     <m:beforeRender><![CDATA[
  4.         var contextPath = request.getContextPath();
  5.         var println = function(value) {
  6.             if (value != null) {
  7.                 java.lang.System.out.println(value);
  8.             } else {
  9.                 java.lang.System.out.println("" + value);
  10.             }
  11.         };
  12.         load("debug.js");
  13.     ]]></m:beforeRender>
  14. </m:mayaa>

index.html

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3.     <head>
  4.         <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5.         <meta http-equiv="Cache-Control" content="no-cache">
  6.         <title>Mayaa samples</title>
  7.         <link rel="stylesheet" href="css/base.css" type="text/css">
  8.     </head>
  9.     <body>
  10.         <div class="header">
  11.             <h1><span id="message">Mayaa Samples</span></h1>
  12.         </div>
  13.         <div class="main">
  14.             <h2>Samples</h2>
  15.             <ul>
  16.                 <li><a href="view/load.html">Script load sample</a></li>
  17.                 <li><a href="view/exec.html">Exec sample</a></li>
  18.                 <li><a href="view/usebean.html">Show MyBean</a></li>
  19.             </ul>
  20.         </div>
  21.         <div class="footer">
  22.             ©2004-2005, The Seasar Foundation and the others, all rights reserved.
  23.         </div>
  24.     </body>
  25. </html>

index.mayaa

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <m:mayaa xmlns:m="http://mayaa.seasar.org">
  3.     <m:write id="message" value="Mayaa Samples"/>
  4. </m:mayaa>

官方的参考资料:http://mayaa.seasar.org/documentation/about.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值