MayaaとWebWork2

またMayaaResult作りました。

WebworkValueStackScope

ScriptResolverの役割がScopeに変わっているようなので作り直し。
iterateAttributeNamesはコストがかかるので実装しなかったけど何に使うんだろう…。

public class WebworkValueStackScope extends AbstractReadOnlyAttributeScope {
 public Object getAttribute(String name) {
  OgnlValueStack valueStack = ActionContext.getContext().getValueStack();

  if (name != null) {
   Object value = valueStack.findValue(name);
   if (value != null) {
    return value;
   }
  }
  return null;
 }

 public boolean hasAttribute(String name) {
  OgnlValueStack valueStack = ActionContext.getContext().getValueStack();

  if (name != null) {
   Object value = valueStack.findValue(name);
   if (value != null) {
    return true;
   }
  }
  return false;
 }

 public Iterator iterateAttributeNames() {
  return null;
 }

 public String getScopeName() {
  return "webwork";
 }
}

MayaaResult

基本的にはMayaaServletをまねるだけ。
Scopeは設定ファイルで追加する方法がわからないのでstaticイニシャライザで追加してます。
使い方は id:matobat:20050902:1125631017 と同じ。

public class MayaaResult extends WebWorkResultSupport {
 static {
  ProviderUtil.getScriptEnvironment().addAttributeScope(
    new WebworkValueStackScope());
 }

 protected void doExecute(String location, ActionInvocation actionInvocation)
   throws Exception {
  HttpServletRequest request = ServletActionContext.getRequest();
  if (location != null) {
   request = new MayaResultRequest(request, location);
  }
  CycleUtil.initialize(request, ServletActionContext.getResponse());
  Engine engine = ProviderUtil.getEngine();

  setupCharacterEncoding(ServletActionContext.getRequest(), engine
    .getParameter("requestCharacterEncoding"));

  engine.doService(true);
 }

 public static class MayaResultRequest extends HttpServletRequestWrapper {
  private String location;

  public MayaResultRequest(HttpServletRequest request, String location) {
   super(request);
   this.location = location;
  }

  public String getPathInfo() {
   return location;
  }

  public String getServletPath() {
   return "";
  }
 }

 protected void setupCharacterEncoding(HttpServletRequest request,
   String encoding) {
  if (request.getCharacterEncoding() == null) {
   try {
    request.setCharacterEncoding(encoding);
   } catch (UnsupportedEncodingException e) {
    String message = StringUtil.getMessage(MayaaServlet.class, 0,
      encoding);
    Log log = LogFactory.getLog(MayaaServlet.class);
    log.warn(message, e);
   }
  }
 }
}

index.html

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>タイトル</title>
</head>
<body>
<h1><span id="title">ダミータイトル</span></h1>
</body>
</html>

index.maya

こんな感じに書くとtitleというプロパティでValueStackに探しに行きます。

<?xml version="1.0" encoding="UTF-8"?>
<m:mayaa xmlns:m="http://mayaa.seasar.org">
 <m:write id="title" 
    value="${webwork.title}"/>
</m:mayaa>

今度の開発はアノテーションとMayaaResultで小設定WebWork2の予定。