IVR Avenger
IVR Avenger

Reputation: 15494

struts2 s:property tag: Better way to replace value?

I have a POJO whose properties I'm displaying on a JSP using Struts2 property tags:

public Class message
{
    string messageText;
    string messageType;

    //getters and setters, here
}

Text: <s:property value="messageBean.messageText"/>
Type: <s:property value="messageBean.messageType"/>

The object is created either by user input, or reading from a database. The "messageType" attribute only ever has integer values in it, which are used as keys for a lookup table within the database.

When selecting the "type" values, users are given a radio selection on a previous page, and each option corresponds to an integer. When I'm displaying the values, I'm currently showing just the integer. I'd like to show the text that corresponds to the integer, and I would accomplish this by having a key/value lookup method in the action class, and change the code to appear as follows:

//in Action class
public String getTranslatedType()
{
    if (messageBean.getMessageType().equals("12"))
        return "Message for frequent callers.";

    if (messageBean.getMessageType().equals("17"))
        return "Message for first time callers.";
    //etc
}

Text: <s:property value="messageBean.messageText"/>
Type: <s:property value="translatedType"/>

Is there a better way to do this?

This does not seem to work:

Type: <s:property value="Utility.getTranslatedText(messageBean.messageType)"/>

Is there a syntax for calling a static utility method from within the property tag?

Upvotes: 0

Views: 3439

Answers (1)

rees
rees

Reputation: 1576

I'm not sure exactly what you are trying to achieve. Why would you prefer to use a static call instead of accessing data from the ValueStack (which is where the action properties are accessed from)? It really is best to avoid static calls if possible and stick to the intended design of the framework and access data from the ValueStack/action.

If you just wish to separate the logic from your action, you could change your method to:

public String getTranslatedType() {
   return Utility.getTranslatedText(getMessageBean().getMessageType());
}

Or are you wishing to have the translated text appear on the page dynamically? If so, perhaps you could use Javascript and JSON to achieve your goal with something along the lines of the following steps:

  1. Place your messageType integers (as the keys) and your translatedText Strings (as the values) into a map along the lines of messageTypeText.put(12, "Message for frequent callers.");

  2. Convert this to JSON using something like (new JSONObject(messageTypeText)).toString();

  3. Then expose this to the client by any of a number of ways such as var messageTypeText = <s:property value="messageTypeText"/>;

  4. Bind a javascript method to the messageType radio button that then displays the translated text by accessing it from the javascript map/array using the value of the currently selected radio/messageType. I could give more detail on this approach, but I don't want to waste my time if its not what you're looking for! :)

Upvotes: 1

Related Questions