Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Code Block
xml
xml
titleUsing an expression to set the label
<s:textfield labelkey="%{getText("postalCode.label")}" name="postalCode"/>

The expression language (OGNL) lets us call methods and evaluate properties. The method getText is provided by ActionSupport, which is the base class for most Actions. Since the Action is on the stack, we can call any of its methods from an expression, including getText.

...

Code Block
xml
xml
titleEvaluating booleans
<s:select labelkey="%{getText("state.label")}" name="state" multiple="true"/>

...

Code Block
xml
xml
titleEvaluating booleans (verbose)
<s:select labelkey="%{getText("state.label")}" name="state" multiple="%{true}"/>
Code Block
xml
xml
titleEvaluating booleans (with property)
<s:select labelkey="%{getText("state.label")}" name="state" multiple="allowMultiple"/>
Code Block
xml
xml
titleEvaluating booleans (verbose with property)
<s:select labelkey="%{getText("state.label")}" name="state" multiple="%{allowMultiple}"/>

...

Most often, the value attribute is set automatically, since name attribute usually tells the framework which property to call to set the value. But, if there is a reason to set the value directory directly, be advised that value is an Object NOT a String.

...

Code Block
xml
xml
titleProbably wrong!
<s:textfield labelkey="%{getText("state.label")}" name="state" value="CAca"/>

If a textfield is passed the value attribute "CAca", the framework will look for a property named getCa. Generally, this is not what we mean. What we mean to do is pass a literal String. In the expression language, literals are placed within quotes

Code Block
xml
xml
titlePassing a literal value the right way
<s:textfield labelkey="%{getText("state.label")}" name="state" value="%{'CAca'}" />

Another approach would be to use the idiom value="'CAca'", but, in this case, using the expression notation is recommended.

...

  1. All String attribute types are parsed for the "%{ ... }" notation.
  2. All non-String attribute types are not parsed, but evaluated directly as an expression
  3. The exception to rule #2 is that if the non-String attribute uses the escape notion "{%{}", the notation is ignored as redundant, and the content evaluated.
Note

Please remember about altSyntax option that can change when value is evaluated as an expression - Alt Syntax

Expression Language Notations

Code Block
<p>Username: ${user.username}</p>

A JavaBean object in a standard context in Freemarker, Velocity, or JSTL EL (Not OGNL).

Code Block
<s:textfield name="username"/>

A username property on the Value Stack.

Code Block
<s:url id="es" action="Hello">
  <s:param name="request_locale">
    es
  </s:param>
</s:url>
<s:a href="%{es}">Espanol</s:a>

Another way to refer to a property placed on the Value Stack.

Code Block
<s:property
  value="#session.user.username" />

The username property of the User object in the Session context.

Code Block
<s:select
  label="FooBar" name="foo"
  list="#{'username':'trillian',
    'username':'zaphod'}" />

A static Map, as in put("username","trillian").

Disallowed property names

The following names of property are disallowed:

  • parameters
  • application
  • session
  • struts
  • request
  • servletRequest
  • servletResponse

The below code will not work:

Code Block
xml
xml

<s:iterator value="parameters"/>
Code Block
java
java

public class MyAction {

    private String[] parameters;

    public String[] getParameters() {
        return parameters;
    }

}

Next: JSP