FileDocCategorySizeDatePackage
Text.javaAPI DocExample5725Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

Text

public class Text extends Component implements Param.UnnamedParametric
Render a I18n text message.

The message must be in a resource bundle with the same name as the action that it is associated with. In practice this means that you should create a properties file in the same package as your Java class with the same name as your class, but with .properties extension.

If the named message is not found, then the body of the tag will be used as default message. If no body is used, then the name of the message will be used.

  • name* (String) - the i18n message key

Example:



Accessing messages from a given bundle (the i18n Shop example bundle in the first example) and using bundle defined through the framework in the second example.



<!-- First Example -->
<s:i18n name="struts.action.test.i18n.Shop">
<s:text name="main.title"/>
</s:i18n>

<!-- Second Example -->
<s:text name="main.title" />

<!-- Third Examlpe -->
<s:text name="i18n.label.greetings">
<s:param >Mr Smith</s:param>
</s:text>




<-- Fourth Example -->
<s:text name="some.key" />

<-- Fifth Example -->
<s:text name="some.invalid.key" >
The Default Message That Will Be Displayed
</s:text>


see
Param

Fields Summary
private static final Log
LOG
protected List
values
protected String
actualName
protected String
name
Constructors Summary
public Text(com.opensymphony.xwork2.util.ValueStack stack)


       
        super(stack);
    
Methods Summary
public voidaddParameter(java.lang.String key, java.lang.Object value)

        addParameter(value);
    
public voidaddParameter(java.lang.Object value)

        if (values.isEmpty()) {
            values = new ArrayList(4);
        }

        values.add(value);
    
public booleanend(java.io.Writer writer, java.lang.String body)

        actualName = findString(name, "name", "You must specify the i18n key. Example: welcome.header");
        String defaultMessage;
        if (TextUtils.stringSet(body)) {
            defaultMessage = body;
        } else {
            defaultMessage = actualName;
        }
        String msg = null;
        ValueStack stack = getStack();

        for (Iterator iterator = getStack().getRoot().iterator();
             iterator.hasNext();) {
            Object o = iterator.next();

            if (o instanceof TextProvider) {
                TextProvider tp = (TextProvider) o;
                msg = tp.getText(actualName, defaultMessage, values, stack);

                break;
            }
        }

        if (msg != null) {
            try {
                if (getId() == null) {
                    writer.write(msg);
                } else {
                    stack.getContext().put(getId(), msg);
                }
            } catch (IOException e) {
                LOG.error("Could not write out Text tag", e);
            }
        }

        return super.end(writer, "");
    
public voidsetName(java.lang.String name)

        this.name = name;
    
public booleanusesBody()

        // overriding this to true such that EVAL_BODY_BUFFERED is return and
        // bodyContent will be valid hence, text between start & end tag will
        // be honoured as default message (WW-1268)
        return true;