Search code examples
htmlstruts2labelognlstruts-tags

Use global-messages.properties with Struts2 label tag


Normally I use an HTML label to label my fields as follows

<div class="col-sm-2 col-xs-12">
    <label class="pull-right">
        <s:text name="fax"></s:text>:
    </label>
</div>
<div class="col-sm-3 col-xs-12 text-left">
    <s:property value="organizationInfo.fax"/>
</div>  

where fax is defined in the global-messages.properties file. But I need to use the Struts2 <s:label> inside of an <s:iterator> so each label will have an unique id to connect to each field. I tried several ways including the following

<s:iterator value="chosenShipperViewList" status="status">
    <div class="col-sm-2 col-xs-12 ">
        <s:label for="%{'deleteShipper'+#status.index}" class="pull-right">
            <s:text name="deleteShipperInfo"></s:text>:
        </s:label>
    </div>
    <div class="col-sm-3 col-xs-12 text-left">
        <s:url var="deleteLink" action="shipment_deleteShipperFromChosenShipperViewList">
        <s:param name="shipperName" value="%{organizationInfo.orgName}"></s:param>
            <s:param name="typeOfShipmentId" value="typeOfShipmentId"></s:param>
        </s:url>
        <s:a id="%{'deleteShipper'+#status.index}" href="%{deleteLink}"> 
            <img src="/llr/theme/delete.gif" width="16" height="16" alt="Map Red X" >
        </s:a>
    </div>
</s:iterator>

I could never get Struts to generate the HTML label using the label value from the global-messages.properties file. Any help would be appreciated!


Solution

  • Use a standard HTML tag:

    <label for='<s:property value="%{'deleteShipper'+#status.index}" />' class="pull-right">
        <s:text name="deleteShipperInfo"></s:text>:
    </label>
    

    Or drop <s:text> and use key:

    <s:label key="deleteShipperInfo" for="%{'deleteShipper'+#status.index}" class="pull-right" />