如下图所示建立工程:
所需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
- package example;
- import java.math.BigDecimal;
- import java.util.Date;
- public class MyBean {
- private int _id = 1000;
- private String _name = "MyBean name";
- private BigDecimal _decimal = new BigDecimal("12.345");
- private Date _timestamp = new Date();
- public int get_id() {
- return _id;
- }
- public void set_id(int _id) {
- this._id = _id;
- }
- public String get_name() {
- return _name;
- }
- public void set_name(String _name) {
- this._name = _name;
- }
- public BigDecimal get_decimal() {
- return _decimal;
- }
- public void set_decimal(BigDecimal _decimal) {
- this._decimal = _decimal;
- }
- public Date get_timestamp() {
- return _timestamp;
- }
- public void set_timestamp(Date _timestamp) {
- this._timestamp = _timestamp;
- }
- }
base.css
- * {
- margin: 0;
- padding: 0;
- text-indent: 0;
- }
- body {
- text-align: center;
- font-family: sans-serif;
- color: #483d8b;
- background: white;
- }
- body div {
- text-align: left;
- margin: 0 auto;
- }
- .header {
- color: white;
- border-bottom: 6px double white;
- background: #1c4baf;
- }
- .header h1 {
- padding: 8px;
- border-bottom: 4px double white;
- }
- .container {
- }
- .main {
- font-size: 100%;
- padding: 8px;
- margin-left: 216px;
- }
- .sidebar {
- float: left;
- top: 4em;
- left: 10px;
- width: 200px;
- padding: 8px;
- }
- .sidebar h1,h2,h3,h4,h5 {
- font-size: 100%;
- margin-top: 1em;
- margin-bottom: 0.2em;
- }
- .sidebar h1 {
- border-left: 4px solid #1c4baf;
- padding-left: 2px;
- }
- .sidebar ul {
- font-size: 100%;
- margin-top: 0.2em;
- margin-left: 1.5em;
- }
- .footer {
- text-align: center;
- color: white;
- border-top: 6px double white;
- background: #1c4baf;
- clear: both;
- }
- div.box {
- text-align: center;
- border: dotted #aaf 2px;
- background-color: #ffe;
- padding: 10px;
- margin: 2px;
- }
dump.js
- function printApplication() {
- var names = application.iterateAttributeNames();
- while (names.hasNext()) {
- var name = names.next();
- print("app: " + name + " = " + application[name]);
- }
- }
- function printSession() {
- var names = session.iterateAttributeNames();
- while (names.hasNext()) {
- var name = names.next();
- print("ses: " + name + " = " + session[name]);
- }
- }
- function printAttribute() {
- var names = request.iterateAttributeNames();
- while (names.hasNext()) {
- var name = names.next();
- print("att: " + name + " = " + request[name]);
- }
- }
- function printParameter() {
- var names = param.iterateAttributeNames();
- while (names.hasNext()) {
- var name = names.next();
- print("prm: " + name + " = " + param[name]);
- }
- }
- function printHeader() {
- var names = header.iterateAttributeNames();
- while (names.hasNext()) {
- var name = names.next();
- print("hed: " + name + " = " + header[name]);
- }
- }
- function printBinding() {
- var names = binding.iterateAttributeNames();
- while (names.hasNext()) {
- var name = names.next();
- print("bid: " + name + " = " + binding[name]);
- }
- }
- function print(value) {
- java.lang.System.out.println("" + value);
- }
- print("------ req-path: " + request.getRequestedPath());
- printSession();
- printAttribute();
- printParameter();
- printHeader();
- printBinding();
script.js
- var obj = {
- run: function() {
- return 'こんにちは';
- }
- };
exec.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
- <html xmlns:c="http://java.sun.com/jstl/core_rt">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <meta http-equiv="Cache-Control" content="no-cache">
- <title>Mayaa samples</title>
- <link rel="stylesheet" href="../css/base.css" type="text/css">
- </head>
- <body id="exec">
- <div class="header">
- <h1>Mayaa Samples</h1>
- </div>
- <div class="container">
- <div class="sidebar"><span>
- <h2>メニュー</h2>
- <ul>
- <li><a href="../index.html">戻る</a></li>
- </ul>
- </span></div>
- <div class="main">
- <h2>mayaa exec processor</h2>
- <div id="write" class="box">
- テンプレート上のダミー文字列
- </div>
- </div>
- <div class="footer">
- ©The Seasar Foundation.
- </div>
- </div>
- </body>
- </html>
exec.mayaa
- <?xml version="1.0" encoding="UTF-8"?>
- <m:mayaa xmlns:m="http://mayaa.seasar.org">
- <m:exec id="exec" replace="false"
- src="../js/script.js" encoding="UTF-8"/>
- <m:write id="write" replace="false" value="${ obj.run(); }"/>
- </m:mayaa>
load.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
- <html xmlns:c="http://java.sun.com/jstl/core_rt">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <meta http-equiv="Cache-Control" content="no-cache">
- <title>Mayaa samples</title>
- <link rel="stylesheet" href="../css/base.css" type="text/css">
- </head>
- <body>
- <div class="header">
- <h1>Mayaa Samples</h1>
- </div>
- <div class="container">
- <div class="sidebar"><span>
- <h2>メニュー</h2>
- <ul>
- <li><a href="../index.html">戻る</a></li>
- </ul>
- </span></div>
- <div class="main">
- <h2>Script load Sample</h2>
- <div id="write" class="box">
- テンプレート上のダミー文字列
- </div>
- </div>
- <div class="footer">
- ©The Seasar Foundation.
- </div>
- </div>
- </body>
- </html>
load.mayaa
- <?xml version="1.0" encoding="UTF-8"?>
- <m:mayaa xmlns:m="http://mayaa.seasar.org"
- m:noCache="true">
- <m:write id="write" replace="false"
- value="${ load('../js/script.js', 'UTF-8'); obj.run(); }"/>
- </m:mayaa>
usebean.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
- <html xmlns:c="http://java.sun.com/jstl/core_rt">
- <head>
- <title>use bean</title>
- </head>
- <body>
- <h1>Use bean</h1>
- <table border="1">
- <tr>
- <th>property</th>
- <th>value</th>
- </tr>
- <tr>
- <td>id</td>
- <td><span id="id">10 dummy</span>;</td>
- </tr>
- <tr>
- <td>name</td>
- <td><span id="name">dummy name</span></td>
- </tr>
- <tr>
- <td>decimal</td>
- <td><span id="decimal">1.2 dummy</span></td>
- </tr>
- <tr>
- <td>timestamp</td>
- <td><span id="timestamp">1970/1/1 dummy</span></td>
- </tr>
- </table>
- </body>
- </html>
usebean.mayaa
- <?xml version="1.0" encoding="UTF-8"?>
- <m:mayaa xmlns:m="http://mayaa.seasar.org"
- xmlns:fmt="http://java.sun.com/jsp/jstl/fmt">
- <m:beforeRender>
- request.setAttribute('bean', new Packages.example.MyBean());
- </m:beforeRender>
- <m:write m:id="id" value="${ bean._id }" />
- <m:write m:id="name" value="${ bean._name }" />
- <fmt:formatNumber m:id="decimal" value="${ bean._decimal }" />
- <fmt:formatDate m:id="timestamp" value="${ bean._timestamp }" />
- </m:mayaa>
web.xml
- <?xml version="1.0" encoding="iso-8859-1"?>
- <!DOCTYPE web-app
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">
- <web-app>
- <servlet>
- <servlet-name>MayaaServlet</servlet-name>
- <servlet-class>org.seasar.mayaa.impl.MayaaServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>MayaaServlet</servlet-name>
- <url-pattern>*.html</url-pattern>
- </servlet-mapping>
- </web-app>
debug.js //设置控制台debug是否启用
- /* set true/false */
- var debug = true;
- if (debug) {
- load("js/dump.js");
- }
default.mayaa
- <?xml version="1.0" encoding="UTF-8"?>
- <m:mayaa xmlns:m="http://mayaa.seasar.org">
- <m:beforeRender><![CDATA[
- var contextPath = request.getContextPath();
- var println = function(value) {
- if (value != null) {
- java.lang.System.out.println(value);
- } else {
- java.lang.System.out.println("" + value);
- }
- };
- load("debug.js");
- ]]></m:beforeRender>
- </m:mayaa>
index.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <meta http-equiv="Cache-Control" content="no-cache">
- <title>Mayaa samples</title>
- <link rel="stylesheet" href="css/base.css" type="text/css">
- </head>
- <body>
- <div class="header">
- <h1><span id="message">Mayaa Samples</span></h1>
- </div>
- <div class="main">
- <h2>Samples</h2>
- <ul>
- <li><a href="view/load.html">Script load sample</a></li>
- <li><a href="view/exec.html">Exec sample</a></li>
- <li><a href="view/usebean.html">Show MyBean</a></li>
- </ul>
- </div>
- <div class="footer">
- ©2004-2005, The Seasar Foundation and the others, all rights reserved.
- </div>
- </body>
- </html>
index.mayaa
- <?xml version="1.0" encoding="UTF-8"?>
- <m:mayaa xmlns:m="http://mayaa.seasar.org">
- <m:write id="message" value="Mayaa Samples"/>
- </m:mayaa>