Versions Compared

Key

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

In the Validating Input lesson, we used the validation framework to verify data submitted from a form. In the Localizing Output lesson, we use move the validation messages to a message resource bundle.

...

The framework associates message resources to classes. To add a message resource for the Login Logon action, we could just name the resource LoginLogon.properties and set it on the classpath next to the Login Logon Action.

But, most people find it counter-productive to use separate message resource bundles for each class. Instead, many people prefer to add a bundle for an entire package of classes. To do this, simply add a package.properties file to the package. In our case, it would be the tutorial package.

Code Block
titletutorial/package.properties
borderStylesolid
requiredstring = $\{getText(fieldName)} is required.
password = Password
username = User Name

We also need to make changes to the validator and Login page.

...

Logon page. As you see a value in resource bundle can also be specified as an expression.

Logon-validation.xml

(minus) <message>Username is required</message>
(plus) <message key="requiredstring"/>

(minus) <message>Password is required</message>
(plus) <message key="requiredstring"/>

...

Logon.jsp

(minus) <s:textfield label="User Name" name="username"/>
(plus) <s:textfield label="%{getText('username')}" name="username"/>

...

  • The "key" attribute tells the validator to check for a message resource bundle.
  • In the resource bundle, the expression
    No Format
    ${getText(fieldName)}
    tells the framework to lookup the field name in the bundle too. This way we can use the same default message for all the requiredstring validators.
  • Likewise, in the text filed, the expression
    No Format
    %{getText('password')}
    tells the framework to lookup "password" in the message resources.

...

Code Block
titletutorial/package.properties
borderStylesolid
# ... 
HelloWorld.message = Struts is up and running ...
Missing.message = This feature is under construction. Please try again in the next interationiteration.

This will work for HelloWorld since it is already in the tutorial package. But it won't work for the default Missing action, unless we add our own base class for the tutorial package.

...

(minus) This feature is under construction. Please try again in the next interationiteration.
(plus) <s:text name="Missing.message"/>

...

Code Block
formatJava
titleHelloWorld.java
borderStylesolid
package tutorial;

public class HelloWorld extends ExampleSupportTutorialSupport {

    public static final String MESSAGE = "HelloWorld.message";

    public String execute() throws Exception {
        setMessage(getText(MESSAGE));
        return SUCCESS;
    }

  // ... 
}

...