FileDocCategorySizeDatePackage
Property.javaAPI DocExample5075Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

Property

public class Property extends Component
Used to get the property of a value, which will default to the top of the stack if none is specified.

  • default (String) - The default value to be used if value attribute is null
  • escape (Boolean) - Escape HTML. Default to true
  • value (Object) - value to be displayed






TextUtils






Example 1 prints the result of myBean's getMyBeanProperty() method.
Example 2 prints the result of myBean's getMyBeanProperty() method and if it is null, print 'a default value' instead.




<s:property value="getText('some.key')" />


Fields Summary
private static final Log
LOG
private String
defaultValue
private String
value
private boolean
escape
Constructors Summary
public Property(com.opensymphony.xwork2.util.ValueStack stack)


       
        super(stack);
    
Methods Summary
private java.lang.Stringprepare(java.lang.String value)

        if (escape) {
            return TextUtils.htmlEncode(value);
        } else {
            return value;
        }
    
public voidsetDefault(java.lang.String defaultValue)

        this.defaultValue = defaultValue;
    
public voidsetEscape(boolean escape)

        this.escape = escape;
    
public voidsetValue(java.lang.String value)

        this.value = value;
    
public booleanstart(java.io.Writer writer)

        boolean result = super.start(writer);

        String actualValue = null;

        if (value == null) {
            value = "top";
        }
        else if (altSyntax()) {
            // the same logic as with findValue(String)
            // if value start with %{ and end with }, just cut it off!
            if (value.startsWith("%{") && value.endsWith("}")) {
                value = value.substring(2, value.length() - 1);
            }
        }

        // exception: don't call findString(), since we don't want the
        //            expression parsed in this one case. it really
        //            doesn't make sense, in fact.
        actualValue = (String) getStack().findValue(value, String.class);

        try {
            if (actualValue != null) {
                writer.write(prepare(actualValue));
            } else if (defaultValue != null) {
                writer.write(prepare(defaultValue));
            }
        } catch (IOException e) {
            LOG.info("Could not print out value '" + value + "'", e);
        }

        return result;