ELResolver

あっているかどうか分からないので、晒してみようと思います。

  • WebworkELResolver.java


package webwork.resolver;

import java.util.Iterator;

import javax.el.ELContext;
import javax.el.ELException;
import javax.el.ELResolver;
import javax.el.PropertyNotFoundException;
import javax.el.PropertyNotWritableException;

import org.seasar.maya.engine.Engine;
import org.seasar.maya.engine.Page;
import org.seasar.maya.engine.Template;
import org.seasar.maya.engine.specification.PageSpecification;

import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.util.OgnlValueStack;

/**
* @author duran
*
*/
public class WebworkELResolver extends ELResolver {

public Object getValue(ELContext context, Object base, Object property)
throws NullPointerException, PropertyNotFoundException, ELException {
if (context == null) {
throw new NullPointerException();
}

if (base == null && property != null) {
PageSpecification specification = getSpecification(context);
if (specification == null) {
return null;
}

String name = property.toString();

if (name.equals("action")) {
OgnlValueStack stack = ActionContext.getContext()
.getValueStack();
ActionInvocation invocation = (ActionInvocation) stack
.getContext().get(ActionContext.ACTION_INVOCATION);

if (invocation != null) {
context.setPropertyResolved(true);
return invocation.getAction();
}
}
}
return null;
}

public Class getType(ELContext context, Object base, Object property)
throws NullPointerException, PropertyNotFoundException, ELException {
if (context == null) {
throw new NullPointerException();
}
if (base == null && property != null) {
PageSpecification specification = getSpecification(context);
if (specification == null) {
return null;
}

String name = property.toString();
if (name.equals("action")) {
OgnlValueStack stack = ActionContext.getContext()
.getValueStack();
ActionInvocation invocation = (ActionInvocation) stack
.getContext().get(ActionContext.ACTION_INVOCATION);

if (invocation != null) {
context.setPropertyResolved(true);
return invocation.getAction().getClass();
}
}

}
return null;
}

protected PageSpecification getSpecification(ELContext context) {
PageSpecification specification = null;
Template template = (Template) context.getContext(Template.class);
if (template != null) {
specification = template.getLocalSpecification();
} else {
Page page = (Page) context.getContext(Page.class);
if (page != null) {
specification = page.getPageSpecification();
} else {
Engine engine = (Engine) context.getContext(Engine.class);
if (engine != null) {
specification = engine.getDefaultSpecification();
}
}
}
return specification;
}

public void setValue(ELContext context, Object base, Object property,
Object value) throws NullPointerException,
PropertyNotFoundException, PropertyNotWritableException,
ELException {
}

public boolean isReadOnly(ELContext context, Object base, Object property)
throws NullPointerException, PropertyNotFoundException, ELException {
return true;
}

public Iterator getFeatureDescriptors(ELContext context, Object base) {
return null;
}

public Class getCommonPropertyType(ELContext context, Object base) {
return Object.class;
}

}

EL式の中に「action」という文字があった場合にActionを返すようにしています。getValueメソッド以外は動作しているかどうか分かりません・・・


追記
getSpecificationは必要ないので削除しました。
context.setPropertyResolved(true);の位置を変更しました。


このELResolverを登録するためのサーブレット

  • WebworkMayaServlet.java


package webwork.servlet;

import javax.servlet.ServletException;
import javax.servlet.jsp.JspApplicationContext;

import org.seasar.maya.standard.MayaServlet;
import org.seasar.maya.util.EngineUtil;

import webwork.resolver.WebworkELResolver;

/**
* @author duran
*
*/
public class WebworkMayaServlet extends MayaServlet {
public void init() throws ServletException {
super.init();

JspApplicationContext jspApplication = EngineUtil
.getJspApplicationContext(getServletContext());

jspApplication.addELResolver(new WebworkELResolver());

}
}

このサーブレットをMayaSevletの代わりにweb.xmlに登録します。


とりあえずこの実装で動作を確認することはできました。