org.apache.struts2.components
public class: Text [javadoc |
source]
java.lang.Object
org.apache.struts2.components.Component
org.apache.struts2.components.ContextBean
org.apache.struts2.components.Text
All Implemented Interfaces:
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 in a property file, then the body of the
tag will be used as default message. If no body is used, then the stack will
be searched, and if a value is returned, it will written to the output.
If no value is found on the stack, the key of the message will be written out.
- 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>
| Field Summary |
|---|
| protected List | values | |
| protected String | actualName | |
| protected String | name | |
| protected String | searchStack | |
| Methods from org.apache.struts2.components.Component: |
|---|
|
addAllParameters, addParameter, altSyntax, altSyntax, completeExpressionIfAltSyntax, copyParams, determineActionURL, determineNamespace, end, end, fieldError, findAncestor, findString, findString, findStringIfAltSyntax, findValue, findValue, findValue, getComponentStack, getParameters, getStack, popComponentStack, setActionMapper, setThrowExceptionsOnELFailure, start, stripExpressionIfAltSyntax, stripExpressionIfAltSyntax, toString, usesBody |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from org.apache.struts2.components.Text Detail: |
public void addParameter(Object value) {
if (values.isEmpty()) {
values = new ArrayList(4);
}
values.add(value);
}
|
public void addParameter(String key,
Object value) {
addParameter(value);
}
|
public boolean end(Writer writer,
String body) {
actualName = findString(name, "name", "You must specify the i18n key. Example: welcome.header");
String defaultMessage;
if (StringUtils.isNotEmpty(body)) {
defaultMessage = body;
} else {
defaultMessage = actualName;
}
Boolean doSearchStack = searchStack != null ? (Boolean) findValue(searchStack, Boolean.class) : true;
String msg = TextProviderHelper.getText(actualName, defaultMessage, values, getStack(), doSearchStack == null || doSearchStack);
if (msg != null) {
try {
if (getVar() == null) {
writer.write(msg);
} else {
putInContext(msg);
}
} catch (IOException e) {
LOG.error("Could not write out Text tag", e);
}
}
return super.end(writer, "");
}
|
public void setName(String name) {
this.name = name;
}
|
public void setSearchValueStack(String searchStack) {
this.searchStack = searchStack;
}
|
public boolean usesBody() {
// 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;
}
|