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)
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/>
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}"
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