开发者

OGNL Addition / Type Coercion

%{control.current + #displayRows}

is ultimately the statement I need executed. I have it in an s:if tag and I use test to see if this value lies within a certain range.

Ultimately, I get string concatenation and not addition because both side of the addition are not regarded as numeric types by OGNL. Doin开发者_开发知识库g a little tinkering, I see that

%{control.current + control.current}

does result in numerical addition, so indeed the displayRows value which was set in an s:set tag earlier is regarded an the non-numeric value. Here is my s:set tag:

<s:set name="displayRows" value="%{#application['app_settings'].settings['MAX ACCESS FIELD TITLES ROWS']}" />

The settings represents a Map in Java. Whereas the key is always a String ... well ... the value is not always an Integer because assorted application settings are being stored. So the best we can do for value type is Object. And I believe this is the problem. OGNL does not regard this as something which can be converted automatically to a numeric type.

I have gone through the langauge guide at http://incubator.apache.org/ognl/language-guide.html which explains some of these concepts, but I do not see a way to tell OGNL "Yes this displayRows which contains the value of 15 REALLY is an integer". Is there a way to make this happen. I need to be able to do addition on the fly so I cannot create additional attributes in Javaland to assist me. I have looked at the OGNL, the s:set tag and the Java level and I do not see a proper place where I can make this happen.


Struts thinks #displayRows is a String when we need it as an Integer (I'll assume integer you'll be able to apply the following to any built in type).

First turn on static method access in struts.xml.

For reference here is my struts.xml, the last constant tag is what you'll need to add to yours:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />
    <constant name="struts.date.format" value="0,date,dd.MM.yyyy"/>
    <constant name="format.date" value="{0,date,dd.MM.yyyy}"/>
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> 
</struts>

Then in your jsp you'll do something like:

<s:property value='@java.lang.Integer@valueOf("123") + @java.lang.Integer@valueOf("123")' />

Which displays: 246

It would probably be better to do the conversion in the set tag:

<s:set name="displayRows" value="@java.lang.Integer@valueOf(#application['app_settings'].settings['MAX ACCESS FIELD TITLES ROWS'])" />

then,

<s:property value="control.current + #displayRows"/>

will behave as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜