Question

My application uses Struts messages resources to show message on JSP file.

<message-resources parameter="ApplicationResources" />

This is what I have specified in my Struts XML file.

In my web application I want to give user the freedom to change the label name to some other name. How can I change the label name in Struts at run time, so that updated label is displayed on the screen.

I have tried updating the label using following code in Action class, but it updates the alert message shown on the screen and not the label.

ActionMessages messages = new ActionMessages();
messages.add("App.Screen.ScreenHeading", new ActionMessage("App.Screen.ScreenHeading", "My Heading"));
saveMessages(request, messages); 
Was it helpful?

Solution

Actions messages are used with the validation. Better you don't try to set request attributes using this approach because action message are incompatible to the newer versions of Struts framework and you might have problems upgrading those messages. Another approach is to use a form bean or request scope variable to provide the text used to substitute for the message displayed by bean:message tag.

request.setAttribute("App_Screen_ScreenHeading", "My Heading");

the JSP

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<td width="120" class="labelTextSelect"><span class="mandatory">*</span>
<c:if test="${empty App_Screen_ScreenHeading}">
  <bean:message key="App.Screen.ScreenHeading" />
</c:if>
<c:if test="${not empty App_Screen_ScreenHeading}">
  <c:out value=${App_Screen_ScreenHeading}"/>
</c:if>
&nbsp;&nbsp; </td>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top