default values for property tags (struts2)

Go To StackoverFlow.com

1

I am trying to have a default value come from a bean in a JSP page, but I am unable to get the expresion to extract the value. An example JSP that I am working with would be

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
    <head>
        <title> Testing OGNL conversions </title>
    </head>
  <body>
       The data stored is
       <s:property value="dataField" /><br/>
       The property of thisdata is 
       <s:property value="thisdata" default="%{dataField}" /><br/>
       <s:property value="dataField" />
  </body>
</html>

I get the following output

The data stored is YES 
The property of thisdata is %{dataField} 
YES 

Assuming the struts.xml and the class are correct (as since the YES is being printed, as expected, they should be) How can I get the default value to pull from the dataField (yes I know stupid name, but for test code it works enouph for me)

dataField exists in the bean, but thisdata does not exist (this is done so that I can get the default value to printed)

2012-04-05 19:20
by mpop


4

The default parameter for the property tag is not evaluated for OGNL.

So you can use struts if/elseif/else tags (or the JSTL equivalent):

    <s:if test="somevar1 == null">
        <s:property value="someVar2"/>
    </s:if>
    <s:else>
        <s:property value="someVar1"/>
    </s:else>

OGNL is evaluated in the value attribute and so a ternary saves a lot of space:

<s:property value="somevar1 == null?someVar2:someVar1"/>

Of further note... the value attribute of the property tag is set to the action class initially (in an iterator it would be what you expect) so it is generally required for the value attribute to be set for the default to work as expected, if it is not supplied the tag output is the same as <s:property/>

2012-04-05 21:16
by Quaternion


0

I did Struts2 a while back so not entirely sure but try this.

The documentation of the s:property tag mentions that default is used when value is not present. So try removing the value property and just have default="%{datafield}"

2012-04-05 19:33
by Omnipresent
Well 2 things First It came back with the wrong value in the place of %{dataField} above it came back with NO and it was my understanding from what I read in the book Struts2 In Action is that if the return value of the property is NULL then struts would use the default value (as it was above just not evaluating it) now it looks like it is being evaluated incorrectly (the only 2 possable values are YES and NO, and the getDataFeild function only returns the value does no processing of it) So now the questions is can you have it with a value, and why did it have the wrong value this tim - mpop 2012-04-05 19:48
return value of value was not NULL since you were putting random text for the value field. As for the wrong value coming, you are better of making sure everything in your getter/setter is correct - Omnipresent 2012-04-05 19:52
Another question, could the fact that the data type is using a custom conversion be any way affecting how the default is working? (Hence the 2 values are YES and NO, but really in the object it just boolean) - mpop 2012-04-05 20:08
Ads