ELå¼(EL2.2)ãã¢ããªã±ã¼ã·ã§ã³ã¸çµã¿è¾¼ãæ¹æ³
æ¦è¦
Javaã®JSP/JSFã§ã¯ãELå¼ãã¨ããç°¡æãªå¼è¨èªãç¨ãã¦ãªãã¸ã§ã¯ãã®é£éãè©ä¾¡ãããã¨ãã§ããã
ãã¨ãã°ã"${foo[bar].baz}"ã¨ããELå¼ã§ã¯ãfooã¨ããé åã¾ãã¯ãªã¹ããããæ·»ãåã示ãbarã®ä½ç½®ã®ãããªãã¸ã§ã¯ãã®bazã¨ããããããã£ãè©ä¾¡ãããã¨ã表ãã
ããããä»çµã¿ã¯JSPã®ä¸ã«ã¨ã©ã¾ãããè¨å®ãã¡ã¤ã«ãªã©ã§ãå¤æ°å±éã®ããã«ä½¿ããã¨ä¾¿å©ã ã¨æãã¨ãããã³ãã³ããã
ã¾ããã¡ãã£ã¨ããæ°å¼ã"eval"ããããã¨ããå ´åãããã ããã
ããã§ãELå¼ãèªåã®ã¢ããªã±ã¼ã·ã§ã³ããå©ç¨ããæ¹æ³ã«ã¤ãã¦èª¿ã¹ã¦ã¿ãã
ELå¼ã®ã¢ããªã±ã¼ã·ã§ã³ã¸ã®çµã¿è¾¼ã¿
ELã®ãã¼ã¸ã§ã³
ELå¼ã«ã¯ããã¤ããã¼ã¸ã§ã³ãããã
- JSP2.0ã®ELå¼ (${}ã«ããå³æè©ä¾¡)
- JSF1.0ã®ELå¼ (#{}ã«ããé 延è©ä¾¡)
- JSP2.1ã¨JSF1.1ã®ãELå¼ãçµ±åãããUnified EL(EL2.1)ãJavaEE5以éãTomcat6ç
- JSP2.2ã¨ããã¼ã³ã®ã¡ã½ããå¼ã³åºããªã©ããµãã¼ãããããã«ãªã£ãEL2.2(âjsr245)ãJavaEE6以éãTomcat7ç â ç¾å¨ã®ã¡ã¤ã³ã¹ããªã¼ã ï¼
- ã©ã ããè¤æ°å¼ãä»£å ¥ããµãã¼ãããããã«å¤§å¹ ã«è¨èªæ¡å¼µãããEL3.0(âjsr341) (JavaEE7, 2013/6ãªãªã¼ã¹)
ææ°ã®EL3.0ã§ã¯ãJSP/JSFã ãã§ãªãã¹ã¿ã³ãã¢ãã³ã§ã®å©ç¨ãæ³å®ãã"ELProcessor(âJavadoc)"ã®ãããªã¯ã©ã¹ã追å ããã¦ãããèªã¢ããªã±ã¼ã·ã§ã³ã«çµã¿è¾¼ãã®ã§ããã°ãããªã容æããªã£ã¦ããã
åé¢ãã¦ã§ãã¢ããªå ã§ELå¼ã使ãå ´åã«ã¯ãç°å¢ãã¾ã JavaEE6ãTomcat7ã ã£ããããã¨ãæ°ããã©ã¤ãã©ãªãå©ç¨ããã®ã¯é£ãããããããªãã
ã¾ããEL3ã¯Java8ã®ç»å ´ãå¾ ããå è¡ãã¦ã©ã ããã³ã¬ã¯ã·ã§ã³ãã¹ããªã¼ã ã§æ±ãä»çµã¿ãåãå ¥ãã¦ãããªã©ã®å°ã£ãé¨åãç®ç«ã¡ãä»å¾ã®Java8ã¨ã®æ´åæ§ãæ°ã«ãªãã¨ããã§ããã
ã¨ãããããã¾ãã¯ç¾è¡ã§åºã使ããã¦ããEL2.2ãå©ç¨ã§ããããã«ãããã¨ãèãã¦ã¿ãã
ã©ã¤ãã©ãªã®æºå (EL2.2ã®ã¤ã³ã¿ãã§ã¼ã¹ããã³å®è£ ã¯ã©ã¹)
ELå¼ã¯JavaEE5以éã§javax.el.*ããã±ã¼ã¸(âJavadoc)ã¨ãã¦æ¨æºåããã¦ããã
EL2.2ã®ã¤ã³ã¿ãã§ã¼ã¹ã§ããã°ã"el-api-2.2.jar(âmvn)"ãªã©ãã¯ã©ã¹ãã¹ãéããã¨ã§ãEL2.2ãå©ç¨ããã½ã¼ã¹ã³ã¼ããã³ã³ãã¤ã«ã§ããããã«ãªãã
å®éã«åä½ãããã«ã¯ELã®å®è£ ã¯ã©ã¹ãå¿ è¦ã ããTomcat7ãGlassfish-v3(JavaEE6)ç³»ã§ã¯ããããã®EL2.2ã®å®è£ ã¯ã©ã¹ãçµã¿è¾¼ã¾ãã¦ããã
ã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ã§ã¯ãªããã¹ã¿ã³ãã¢ãã³ãªã¢ããªã±ã¼ã·ã§ã³ã§EL2.2ãå©ç¨ãããå ´åã¯ãGlassfishãæä¾ãã¦ãã"el-impl-2-2.jar(âmvn)"ã®ãããªå®è£ ã¯ã©ã¹ãåç §ã«å ããã
Mavenã®pomã§ç¤ºãã°ã以ä¸ã®ãããªæãã¨ãªãã
<dependency> <groupId>javax.el</groupId> <artifactId>el-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>el-impl</artifactId> <version>2.2</version> <scope>runtime</scope> </dependency>
ã©ã¤ã»ã³ã¹
ãªãã"el-impl-2-2.jar"ã¯ãGlassfishã®ææç©ãªã®ã§ã©ã¤ã»ã³ã¹ã¯CDDLã¾ãã¯ã¯ã©ã¹ãã¹ä¾å¤ã®GPLv2ã§ããã
ãã£ã¦èªã¢ããªããã©ã¤ãã©ãªã¨ãã¦åç
§ããã ããªãã°ãèªã¢ããªã®ã©ã¤ã»ã³ã¹ã«å½±é¿ãä¸ããªãã
æºåããã¯ã©ã¹
EL2.2ãå©ç¨ããã«ã¯ä»¥ä¸ã®ã¯ã©ã¹ã®æºåãå¿ è¦ã§ããã
- ELContext (å¿ é )
- FunctionMapper (â³å¿ é ãææãå¯è½)
- VariableMapper (â³å¿ é ãææãå¯è½)
- ELResolver (å¿ è¦ãªãã°)
ELContextã®å®è£
ELContextã¯æ½è±¡ã¯ã©ã¹ã§ããã
FunctionMapper, VariableMapper, ELResolverã®3ã¤ã®è¦ç´ ãè¿ãã¡ã½ãããå®ç¾©ããå¿
è¦ãããã
FunctionMapperã®å®è£
FunctionMapperã¯ãELå¼ã§å¼ã®å¼ã³åºãããã£ãå ´åã«ããããå®éã®ã¡ã½ããã¨çµã³ã¤ããããã®ãã®ã§ããã
ELå¼ã®ä¸ã§ãå¼ãã使ããªãã®ã§ããã°ãnullãè¿ãã ãã®ç°¡åãªå®è£
ã§ãè¯ãã
ãããã¯ãel-impl.jarã®ä¸ã«å«ã¾ãããFunctionMapperImplãã¨ããã¯ã©ã¹ãåç¨ãã¦ãè¯ãã
VariableMapperã®å®è£
VariableMapperã¯ãELå¼ä¸ã§åç
§ãããå¤æ°ãäºåã«å®ç¾©ãã¦ãããã®ã§ãå¤æ°åããå¤æ°ãçµã³ã¤ããããã®ãã®ã§ããã
ãããåç´ãªHashMapã«ã¤ãªããã ãã®å®è£ ã§åé¡ãªãã
ãããã¯ãããã«ææãããã¦el-impl.jarã®ä¸ã«å«ã¾ãããVariableMapperImplãã¨ããã¯ã©ã¹ãåç¨ãã¦ãè¯ãã
ELResolverã®å®è£
ELResolverã¯ãELå¼ã®"è"ã¨ãªãé¨åã§ããã
ELå¼ã¯ã"${foo[bar].baz}"ã®ææ³ã解éããfoo, bar, bazã®3è¦ç´ ã«åå²ããã
ãã®ãã¨ã
- ããfooããããã§ããã°barããã¼ã¨ãã¦ã
- ããfooããªã¹ãã§ããã°barãæ·»ãåã¨ãã¦ã
- ããfooããªã½ã¼ã¹ãã³ãã«ã§ããã°barããªã½ã¼ã¹ãã¼ã¨ãã¦ã
ã¨ããããã«ããªãã¸ã§ã¯ãã®ç¨®é¡ã«å¿ãã¦å¤ãåå¾ããæ¹æ³ãç°ãªãç¹ãå¸åããå¿ è¦ãããã
ãã®ãããããªããããªã¹ããªããããªã½ã¼ã¹ãã³ãã«ãªãããã¨ãã£ã解éã®éãããµãã¼ãããã®ãELResolverã§ããã
ãããã®ELResolverãã¾ã¨ããããã®CompositeELResolverã¨ããELResolverãããã
å¿ è¦æä½éã®ELResolverã¨ãã¦ã¯ã
CompositeELResolver resolver = new CompositeELResolver(); resolver.add(new ResourceBundleELResolver()); // ãªã½ã¼ã¹ãã³ãã«ã®è§£æ±ºç¨ resolver.add(new MapELResolver()); // Mapã®è§£æ±ºç¨ resolver.add(new ListELResolver()); // Listã®è§£æ±ºç¨ resolver.add(new ArrayELResolver()); // é åã®è§£æ±ºç¨ resolver.add(new BeanELResolver()); // Beanã®setter/getterã®è§£æ±ºç¨
ã¨ããã°ãä¸è¬çãªELå¼ã®è§£éã¯ã§ããããã«ãªãã
ä¸è¨ã®ELResolverã¯ãä¸ããé ã«è©ä¾¡ãã解決ããæç¹ã§çµäºããã®ã§ãaddããé åºã¯éè¦ã§ããã
ã¾ããJSPãJSFãªã©ã§ã¯ã"æé»ã®å¤æ°"ã®ãããªä»®æ³çãªå¤æ°ã®ãããªãã®ããµãã¼ãããããã«ããã«ããã¤ãã®ELResolverã追å ãã¦ããã
ãã¨ãã°JSPä¸ã®ELå¼ã§ä½¿ããrequestå¤æ°çã¯ãå®éã«ã¯VariableMapperã«æ ¼ç´ããã¦ããå¤æ°ã§ã¯ãªããELResolverã§ããªãã¸(ãããã¯å°å½±)ããã¦ãã"ä»®æ³ã®å¤æ°"ã¨ãããã
èªã¢ããªã±ã¼ã·ã§ã³ã§ãç¬èªã®æé»ã®å¤æ°ã®ãããªãã®ããµãã¼ãããã®ã§ããã°ELResolverã®æ¡å¼µãè¡ããã¨ãã§ããã
æä½éã®å©ç¨ä¾
æä½éãä¸è¨ã®æºåãå®è£ ãããã®ã以ä¸ã®ä¾ã¨ãªãã
/** * [ãã¹ã] æä½éã®ELå¼è©ä¾¡æ¹æ³ã®ãã¹ã */ public void testMinimum() { final CompositeELResolver resolver = new CompositeELResolver(); resolver.add(new ResourceBundleELResolver()); // ãªã½ã¼ã¹ãã³ãã«ã®è§£æ±ºç¨ resolver.add(new MapELResolver()); // Map, Propertiesã®è§£æ±ºç¨ resolver.add(new ListELResolver()); // Listã®è§£æ±ºç¨ resolver.add(new ArrayELResolver()); // é åã®è§£æ±ºç¨ resolver.add(new BeanELResolver()); // Beanã®setter/getterã®è§£æ±ºç¨ final VariableMapper varMapper = new VariableMapperImpl(); // åç¨ final FunctionMapper funcMapper = new FunctionMapperImpl(); // åç¨ ELContext elContext = new ELContext() { @Override public ELResolver getELResolver() { return resolver; } @Override public FunctionMapper getFunctionMapper() { return funcMapper; } @Override public VariableMapper getVariableMapper() { return varMapper; } }; // ELå¼ã®è©ä¾¡ãã¡ã¯ã㪠ExpressionFactory ef = ExpressionFactory.newInstance(); // ELå¼ã§è©ä¾¡ããããã®å¤æ°ãè¨å®ãã. varMapper.setVariable("foo", ef.createValueExpression("FOO", String.class)); MyBean myBean = new MyBean(); myBean.setX(1); myBean.setY(2); varMapper.setVariable("bar", ef.createValueExpression(myBean, myBean.getClass())); // ELå¼ãè©ä¾¡ãã. String expression = "hello, ${foo}! ${bar.x + 234}"; ValueExpression ve = ef.createValueExpression(elContext, expression, String.class); String ret = (String) ve.getValue(elContext); System.out.println("result=" + ret); // result=hello, FOO! 235 }
å¤æ°"foo"ã«ä½¿ã£ã¦ãããã¼ã³ã¯ä»¥ä¸ã®å®ç¾©ã¨ããã
public static final class MyBean { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public void mes(String mes) { System.out.println("mes=" + mes); } @Override public String toString() { return "(x: " + x + ", y:" + y + ")"; } }
ããã§ä¸å¿ã"hello, ${foo}! ${bar.x + 234}"ã¨ããELå¼ã解æãã
çµæã¨ãã¦ãhello, FOO! 235ãã¨ããå¤ãå¾ããã¨ãã§ããããã«ãªã£ã¦ããã
å¤æ°ã®æºåæ¹æ³
ELå¼ã®ä¸ã«ããå¤æ°ã¯ãVariableMapperããåå¾ãããã
(ãããªããã°ä»®æ³å¤æ°ã®ãããªãã®ã¨ãã¦ELResolverã®ãã§ã¼ã³å ãæ¤ç´¢ããã)
ELå¼ã¯createValueExpressionã¡ã½ããã§ãValueExpressionãä½æããæç¹ã®ELContextã®ä¸ã«ããå¤æ°ã®ç¶æ
ãè¨æ¶ãããããäºåã«å¤æ°ãè¨å®ãã¦ããå¿
è¦ãããã
(ã¤ã¾ããå¼ãä½æããæç¹ã§è¦ãã¦ããå¤æ°ã®ç¶æ
ãè¨æ¶ããã¦ããã)
å¤æ°ã"ValueExpression"ã¨ããå½¢å¼ã§ä½æããªããã°ãªããªããã
ããã¯ExpressionFactory#createValueExpression(Object, Class)ã¨ãããã«ãã¡ã½ããã«ãã£ã¦ã
ãªãã¸ã§ã¯ãããã®ã¾ã¾è¿ãã ãã®ã©ããã¼ã¨ãã¦å®¹æã«ä½æãããã¨ãã§ããã
ãããã£ã¦ã
varMapper.setVariable("foo", ef.createValueExpression("FOO", String.class));
ã®ããã«"foo"ã¨ããå¤æ°åã§ã"FOO"ã¨ããæååãæ ¼ç´ãããã¨ãã§ããã
ãªãã第äºå¼æ°ã¯ããã®ValueExpressionãè¿ããªãã¸ã§ã¯ãã®åã表ãã
ELå¼ã®ValueExpressionã®è©ä¾¡çµæã¯ãæ»ãå¤ã¨ãªãã¹ãåã«èªåçã«"åå¤æ"ãããã
ELå¼ã®è§£æ(èªã¿è¾¼ã¿)
ELå¼ã«ã¯ã${}ã«ããå³æè©ä¾¡ã¨ã#{}ã«ããé
延è©ä¾¡ã®äºç¨®é¡ããããã
EL2.2ã§ã¯APIçã«ã¯${}ã#{}ãåºå¥ãªããã©ã¡ããåãæ±ãã¨ãªã£ã¦ããã
(å¯ä¸ãåºå¥ãã¦ããã®ã¯ã1ã¤ã®ELå¼ã®ä¸ã§${}ã¨#{}ãæ··å¨ãããããªããã¨ãããã§ããã)
ãã¨ãã°ã"${bar.x}"ã¨ããELå¼ã解æããå ´åã
String expression = "${bar.x}"; ValueExpression ve = ef.createValueExpression(elContext, expression, Object.class);
ãã®ããã«ãValueExpressionãã¨ããå½¢ã§åå¾ãããã¨ã«ãªãã
ã¤ã¾ãã${}ã§ã#{}ã§ãAPIçã«ã¯ãé 延è©ä¾¡åã®æ±ãã«ãªã£ã¦ããããã§ããã
å®éã«å¤æ°ã®å¤ãåå¾ããã«ã¯ãValueExpression#getValue()ã¡ã½ãããå¼ã³åºãå¿
è¦ãããã
ELå¼ã®è§£æ(æ¸ãè¾¼ã¿)
é 延è©ä¾¡åãªã®ã§ãå¤æ°ãæ´æ°ãããå ´åãåãããã«ä½¿ããã¨ãã§ãããã¨ãã°ä»¥ä¸ã®ããã«è¨è¿°ãããã¨ãã§ããã
String expression2 = "${bar.x}"; ValueExpression ve2 = ef.createValueExpression(elContext, expression2, Object.class); ve2.setValue(elContext, 123); System.out.println("myBean.x=" + myBean.getX()); // myBean.x=123
å¤æ°ã¨ãã¦åç §ããã¦ããmyBeanã¤ã³ã¹ã¿ã³ã¹ã®å¤ãå¤æ´ããã¦ãããã¨ã確èªã§ããã
ãã¡ããæ´æ°ã§ããã®ã¯ELå¼ãæããã®ããªãã¸ã§ã¯ãã®å ´åã®ã¿ã§ããã
以ä¸ã®ããã«ãªãã©ã«ãæ··å¨ããELå¼ã¯èªã¿è¾¼ã¿å°ç¨ã§ããã
String expression = "Hello, ${foo}!"; ValueExpression ve = ef.createValueExpression(elContext, expression, Object.class); ve.setValue(elContext, 123); // ããã§ä¾å¤ãçºçãã
æ¸ãè¾¼ããã¨ããã¨ãçµæã¯"javax.el.PropertyNotWritableException: Illegal Syntax for Set Operation"ã¨ä¾å¤ãçºçããã
ELå¼ã®è§£æ(ã¢ã¯ã·ã§ã³ã®å¼ã³åºã)
EL2.2ã§ã¯ELå¼ä¸ã§ãªãã¸ã§ã¯ãã®ã¡ã½ãããå¼ã³åºããã
String expression = "${bar.mes('hello')}"; MethodExpression me = ef.createMethodExpression(elContext, expression, Object.class, new Class[0]); me.invoke(elContext, new Object[0]); // ã³ã³ã½ã¼ã«ã«ãmes=helloãã¨åºãã
ã¡ã½ããmesã®å¼æ°ãELå¼ã®ä¸ã§ä¸ãã¦ããã®ã§ãMethodExpressionã«ããã¨ãã
ValueExpression#getValue()ã§ãåãçµæã¨ãªãã
MethodExpressionã®ã¡ãªããã¯ãELå¼ã§ã¯ã¡ã½ããã¾ã§ãæå®ããå¼æ°ã¯ããã°ã©ã å´ããä¸ããããç¹ã§ãããã
String expression = "#{bar.mes}"; MethodExpression me = ef.createMethodExpression( elContext, expression, Object.class, new Class[] {String.class}); me.invoke(elContext, new Object[] {"ããã«ã¡ã¯ãä¸ç!!"}); // mes=ããã«ã¡ã¯ãä¸ç!!
ã¤ãã³ãã«å¯¾ããã#{ã¢ã¯ã·ã§ã³}ã¤ãã³ããã³ãã©ããªã©ãã³ã¼ã«ããã¯ãåããã¡ã½ãããELå¼ãã示ããããªç¨éãèããããã
æé»ã®å¤æ°ããµãã¼ãããç¬èªã®ELResolverã®ä½æ
ããã¾ã§ã®æ¹æ³ã§ãä»»æã®å¤æ°ãè¨å®ãã¦ELå¼ã§è©ä¾¡ãããã¨ã¯å¯è½ã¨ãªã£ã¦ããã
ããã«å ãã¦ãjspã®ELå¼ãªã©ã§ä½¿ããã¦ããããã«ãæé»ã®å¤æ°ãããµãã¼ãããå ´åã¯ELResolverãæ¡å¼µããå¿ è¦ãããã
ãæé»ã®å¤æ°ãã¯ãå¤æ°åããä½ããã®ãªãã¸ã§ã¯ãã«ããªãã¸ããããã®ã§ãããå¤æ°ã¨ã¯ç°ãªãValueExpressionã®ä¸ã«ã¯ç¶æ ãä¿æããã¦ããããå®éã®è©ä¾¡æã«ããã®æã®å¤ã使ç¨ããããã¨ããç¹å¾´ãããã
ELResolverã¯ãELå¼ã®ã${first.second}ãã®å¼ã解æããçµæã
- firstã«ç¸å½ãããã®ã解éãã
- firstã«å¯¾ãããªãã¸ã§ã¯ããããã£ã¦ããå ´åã«secondã«ç¸å½ãããã®ã解éãã
ã¨ãã2ã¤ã®åããè¡ãã
ELResolverã¯åãåã£ããã®ãèªåãèªèã§ããªãå ´åã¯ä½ãããã次ã®ELResolverã«æ¸¡ããã¨ããåãã«ãªã£ã¦ããã
ãæé»ã®å¤æ°ããå¦çããå ´åã¯ãfirstã®é¨åã«ãæé»ã®å¤æ°åããæ¥ãå ´åã«ä½ããã®ãªãã¸ã§ã¯ããè¿ãããã«ããã°ããã
ãã®å ´åãsecondã«ã¤ãã¦ã¯èæ ®ããªãã¦ããã
ãªããªãã°æé»ã®å¤æ°ãä½ããã®beanãMapãè¿ããã¨ããã¨ããã®ãªãã¸ã§ã¯ãã¯æ¢åã®MapELResolverãBeanELResolverã«ãã£ã¦è§£éãããã¨ãã§ããããã§ããã
ã¾ããæé»ã®å¤æ°ã¯ä¸è¬çã«ä»£å ¥ä¸å¯ãªã®ã§ãä»£å ¥å¦çã¯èæ ®ããªãã¦ããã
ããããã¨ã以ä¸ã®ãããªELResolverã®å®è£ ãè¡ãã°ããã
/** * æé»ã®"implicit"ã³ã³ããã¹ãã®å */ private static class ImplicitContext extends HashMap<String, Object> {} /** * æé»ã®å¤æ°"implicit"ãELContextããåå¾ããããã®ELResolverã®å®è£ ä¾. */ private static class MyImplicitELResolver extends ELResolver { @Override public Class<?> getCommonPropertyType(ELContext context, Object base) { if (base == null) { // baseãnullãã¤ã¾ã${first.xxxx}ã®firstã®å ´åã¯ã // æååã¨ãã¦å¤æ°åãåãåããã¨ã示ã. return String.class; } return null; } @Override public Iterator<FeatureDescriptor> getFeatureDescriptors( ELContext context, Object base) { if (base != null) { // ã¨ãããã空ãè¿ãã¦ãã.(ææãããªãã¦ãELå¼ã¯åã) return Collections.<FeatureDescriptor>emptyList().iterator(); } return null; } @Override public Class<?> getType(ELContext context, Object base, Object property) { if (base == null && property != null) { String name = property.toString(); if ("implicit".equals(name)) { // ${first.second}ã®ãfirstã®å ´åã // ããã"implicit"ã¨ããååã§ããã°ãImplicitContextã¯ã©ã¹ãè¿ã. context.setPropertyResolved(true); // 解éæ¸ã¿ã§ãããã¨ã示ã return ImplicitContext.class; } } return null; // ä¸æãªå ´åã¯æ¬¡ã®ELResolver㸠} @Override public Object getValue(ELContext context, Object base, Object property) { if (base == null && property != null) { String name = property.toString(); if ("implicit".equals(name)) { // ${first.second}ã®ãfirstã®å ´åã // ããã"implicit"ã¨ããååã§ããã°ãELContextã«è¨å®ããã¦ãã // ImplicitContextã³ã³ããã¹ããè¿ã. // â» ELContext#setContext()ã§äºåã«è¨å®ãã¦ãããã¨. context.setPropertyResolved(true); // 解éæ¸ã¿ã§ãããã¨ã示ã return context.getContext(ImplicitContext.class); } } return null; // ä¸æãªå ´åã¯æ¬¡ã®ELResolver㸠} @Override public boolean isReadOnly(ELContext context, Object base, Object property) { return true; } @Override public void setValue(ELContext context, Object base, Object property, Object value) { throw new PropertyNotWritableException("ä»£å ¥ã¯ãµãã¼ãããã¦ãã¾ãã/base=" + base + "/property=" + property); } }
ãã®MyImplicitELResolverã¯ã"implicit"ã¨ããä»®æ³ã®å¤æ°åã解éããELContextã«è¨å®ããã¦ãã
ImplicitContextã¯ã©ã¹ã®ã³ã³ããã¹ããè¿ãããã«ãã¦ããã
ImplicitContextã¯ãå®ä½ã¯ãã ã®ãããã ããELContextã®ã³ã³ããã¹ãã¨ãã¦ã¯ã©ã¹ããã¼ãã¦ã¢ã¯ã»ã¹ããããã
ImplicitContextã¨ããåã«ãã¦å©ç¨ããããããã«ãã¦ããã
ãããå©ç¨ããã³ã¼ãã¯ä»¥ä¸ã®ãããªä½¿ãæ¹ãã§ããã
// æååãªã¹ãã®å¤æ°ãä½æãã. List<String> lst = Arrays.asList("aaa", "bbb", "ccc", "ddd"); varMapper.setVariable("list", ef.createValueExpression(lst, List.class)); // æé»ã®å¤æ°ããã¤ELå¼ String expression = "${list[implicit.idx]}"; ValueExpression ve = ef.createValueExpression(elContext, expression, String.class); // "implicit"å¤æ°ã®å¤ã¨ãªããªãã¸ã§ã¯ã ImplicitContext implicitContext = new ImplicitContext(); elContext.putContext(ImplicitContext.class, implicitContext); for (int idx = 0; idx < lst.size(); idx++) { implicitContext.put("idx", Integer.valueOf(idx)); String ret = (String) ve.getValue(elContext); System.out.println("ret[" + idx + "]=" + ret); }
"${list[implicit.idx]}"ã¨ããELå¼ã解æãã¦ãä¸æ¦ãValueExpressionãåå¾ããã
ãã®æç¹ã§å¤æ°listã¯ValueExpressionã«æ ¼ç´ããã¦ããç¶æ ã¨ãªããã"implicit.idx"ã¯å®ä½ããªãã®ã§ãå¤æ°ã¨ãã¦ã¯æ ¼ç´ããã¦ããªãç¶æ ã§ããã
ãã®ãã¨ãforã«ã¼ãã§"implicit.idx"ã®å¤ãå¤ããªãããåãValueExpressionãç¹°ãè¿ãè©ä¾¡ããã
ããã¨ãçµæã¯ä»¥ä¸ã®ããã«ãªãã
ret[0]=aaa ret[1]=bbb ret[2]=ccc ret[3]=ddd
ãã®ããã«ãELResolverã«ããæé»ã®å¤æ°ã®æ¡å¼µãæ¯è¼çç°¡åã«ã§ãããã¨ããããã
ELå¼ã®é¢æ°ã®ãµãã¼ã
ELã§ã¯ç¬èªã®é¢æ°ãå®ç¾©ãããã¨ãã§ããã
EL2.2ããã¯ãªãã¸ã§ã¯ãã®ã¡ã½ãããå¼ã³åºããã¨ã容æã«ãªã£ã¦ããããåºçªãå°ãªããªã£ã¦ããã¨æããããã
ELæ¨æºã®æ¼ç®åã§ã¯è¶³ããªãæ±ç¨çãªæ¼ç®ã®ããã«ãã¢ããªã±ã¼ã·ã§ã³åºæã®é¢æ°ãç¨æãããã¨ãã§ããã
ãã¨ãã°ãjava.lang.Mathã¯ã©ã¹ã«ããã¦ã¼ããªãã£é¢æ°ã®ãã¡ã第ä¸å¼æ°ãdoubleã®ãã®ãå©ç¨å¯è½ã¨ããå ´åã
final FunctionMapper funcMapper = new FunctionMapper() { private HashMap<String, Method> methods = new HashMap<String, Method>(); { for (Method method : Math.class.getMethods()) { if (Modifier.isStatic(method.getModifiers())) { String name = method.getName(); Class<?>[] params = method.getParameterTypes(); boolean accept = true; if (params.length > 0) { if (!params[0].isAssignableFrom(double.class)) { // 第ä¸å¼æ°ãããå ´åãdoubleåã§ãªããã°ä¸å¯ã¨ãã. // â» ååã®åéãã®é¢æ°ãé¿ãããã accept = false; } } if (accept) { methods.put(name, method); } } } } @Override public Method resolveFunction(String prefix, String localName) { if ("math".equals(prefix)) { return methods.get(localName); } return null; // nullã®å ´åã¯é¢æ°ãæªç»é²ã§ãããã¨ã示ã } };
ãã®ããã«FunctionMapperã§é¢æ°åãä¸ãããããããã®é¢æ°ã®ã¡ã½ãããè¿ãã ãã§ããã
ãã¨ã¯ããã®ããã«å®è£ ããfuncMapperãELContextã§è¿ãããã«ããã°è¯ãã
ä¸è¨ã³ã¼ãã§ã¯ãããªãã£ãã¯ã¹ã¨ãã¦ãmathããç¨ãã¦ãããããELå¼ã¨ãã¦ã¯ã以ä¸ã®ããã«ä½¿ããã¨ã«ãªãã
varMapper.setVariable("a", ef.createValueExpression(10, Integer.class)); varMapper.setVariable("b", ef.createValueExpression(20, Integer.class)); String expression = "max=${math:max(a,b)}, min=${math:min(a,b)}"; ValueExpression ve = ef.createValueExpression(elContext, expression, String.class); String ret = (String) ve.getValue(elContext); System.out.println("ret=" + ret); // çµæã¯ã"ret=max=20.0, min=10.0"
å¤æ°a, bã¯Integeråã ããELå¼ã¯èªåçã«åå¤æããµãã¼ããã¦ãããããdoubleåã¨ãã¦è¨ç®ããã¦ããã
(ä»®ã«å¤æ°ãæåååã®"10", "20"ã§ãã£ã¦ããæ£ããdoubleåã«å¤æããã¦è¨ç®ãããã)
éå¤ãªã¦ã¼ããªãã£
ã·ã¹ãã ããããã£ã¸ã®ã¢ã¯ã»ã¹
ELå¼ã®ä¸ã§ã·ã¹ãã ããããã£ãåç §ã§ããããã«ããã«ã¯ã"System.getPriperties()"ã®çµæããããã«è©°ããªããã¦å¤æ°ã«è¨å®ããã
MapELResolverãè¨å®ããã¦ããã°ããã¨ã¯é常ã®Mapã¨åæ§ã«å¦çãããã
Properties sysProps = System.getProperties(); HashMap<String, Object> sysMap = new HashMap<String, Object>(); for (String name : sysProps.stringPropertyNames()) { sysMap.put(name, sysProps.getProperty(name)); } varMapper.setVariable("sys", ef.createValueExpression(sysMap, Map.class)); String expression = "${sys['java.version']}"; ValueExpression ve = ef.createValueExpression(elContext, expression, String.class); String ret = (String) ve.getValue(elContext);
â» Propertiesã¯ã©ã¹ã¯Mapã¤ã³ã¿ãã§ã¼ã¹ããã£ã¦ããã®ã§ä¸è¨ã®ããã«è©°ãæ¿ããªãã¦ããããã¨ãã¦ã¢ã¯ã»ã¹ã¯å¯è½ã§ããããPropertiesã«ã¯è¦ªã¨ãªãPropertiesãã«ã¹ã±ã¼ããã§ã¤ã³ã«ã§ããæ©è½ãããããã®å ´åãMap#get()ã¯ããã®ãã§ã¤ã³ãããã®ã¼ããã¨ãã§ããªãåé¡ããããããå³å¯ã«ããã°ãè©°ãæ¿ãå¦çãå¿
è¦ã¨ãªãã
ResourceBundleã¸ã®ã¢ã¯ã»ã¹
ResourceBundleELResolverãè¨å®ããã¦ããã°ãå¤æ°ã¨ãã¦ãªã½ã¼ã¹ãã³ãã«ãè¨å®ããã ãã§ããã¨ã¯ååã§ã¢ã¯ã»ã¹ã§ããã
// ãªã½ã¼ã¹ãã³ãã« ResourceBundle res = ResourceBundle.getBundle(getClass().getCanonicalName()); varMapper.setVariable("res", ef.createValueExpression(res, ResourceBundle.class)); String expression = "${res['foo.bar.baz']}"; ValueExpression ve = ef.createValueExpression(elContext, expression, String.class); String ret = (String) ve.getValue(elContext);
çµè«
ç¬èªã«ELå¼ãè©ä¾¡ãããã¨ã¯æã£ã以ä¸ã«ç°¡åã ã£ãã
ELå¼ã¯ãè¥å¹²ã®å®åçãªã³ã¼ããæ¸ãã ãã§ãå´ããã«ãã¦ãªãããªå¤æ°å±éã®ä»çµã¿ãå©ç¨ã§ããã大å¤æçãªãã®ã§ãã£ãã
ãã¾ã¾ã§ããã®ä»çµã¿ã調ã¹ã¦ããªãã£ããã¨ãå¾æããããªãã»ã©ã§ããã
EL3.0ããã¯ããã£ããã¹ã¿ã³ãã¢ãã³çã«ELå¼ã使ãããã®ä»çµã¿ãæ¡å
ããã¦ãããããªã®ã§ã
ä»å¾ã¯ãã£ã¨ç©æ¥µçã«ä½¿ã£ã¦ãã£ã¦ãè¯ãææ³ã ã¨æãããã
åèã«ããã¦ããã ããURL
- [Oracleå
¬å¼]
- ELResolverã®å®è£ æ¹æ³ã®èª¬æ http://otn.oracle.co.jp/technology/global/jp/sdn/java/j2ee/techtips/2007/private/ett0127.html
- [EL2.2ãã¹ã¿ã³ãã¢ãã³ã§ä½¿ããä»ã®ãµã³ãã«ä¾]
- ãã®ä»ãELResolverã®ã«ã¹ã¿ãã¤ãºè¨äºãªã©
- ç¬èªã®ELResolverã®å®è£ ä¾ http://d.hatena.ne.jp/shin/20090426/p1
- ELResolverã®è©ä¾¡é åº http://mk.hatenablog.com/entry/20041210/1132029220
- ELResolverã®è©ä¾¡é åº http://oss.infoscience.co.jp/myfaces/cwiki.apache.org/confluence/display/MYFACES/ELResolver+ordering.html
以ä¸ãã¡ã¢çµäºã