ponder275
ponder275

Reputation: 935

Need to access HashMap in my action class from the inside of a Struts2 iterator

I am writing a Struts2 application working with a database which has a column which consists of either Y or N. I need to present the columns as either Yes or No. So, I have seqIdMap in the action class:

private HashMap<String, String> seqIdMap; 
public ReviewQuestionAction(){
    seqIdMap = new HashMap<String, String>();
    seqIdMap.put("Y", " Yes ");
    seqIdMap.put("N", " No ");
}

I use the following Struts2 code to generate the radio labels and it works fine:

<div class="row">
    <label for="yesOrNo" class="pull-right">
        <s:text name="yesOrNo"></s:text>
    </label>
</div>
<div>
   <s:radio name="indicatiorFlag" id="yesOrNo" list="seqIdMap"  />
</div>

My issue is that, I need to display the indicatiorFlag as Yes or No instead of Y or N, what I get from database. I tried the following as advised on few sites, but it doesn't work:

<s:iterator value="questionInfoList">
<s:url var="link" action="reviewQuestion" method="getQuestionInfo">
    <s:param name="seqId">
        <s:property value="seqId"/>
    </s:param>
</s:url>
<s:a href="%{link}">
    <s:property value = "seqId"/></s:a>
<s:property value = "%{seqIdMap.indicatiorFlag}"/>

I can display a Y or N if I replace %{seqIdMap.indicatiorFlag} with indicatiorFlag. But I haven't figured out how to use the indicatiorFlag to a key in the seqIdMap. I have tried several different ways including %{seqIdMap['indicatiorFlag']} and

<s:set name="newMap" var = "seqIdMap">

outside the iterator and then %{#newMap[indicatiorFlag]} inside of it. Any help would be appreciated.

P.S. I realize indicatiorFlag should be indicatorFlag but I inherited the name.

Upvotes: 0

Views: 314

Answers (2)

ponder275
ponder275

Reputation: 935

I thought I had tried the following code and it didn't work but after looking at James's solution I tried it and it also works.

<s:property value = "%{seqIdMap[indicatiorFlag]}"/>

Upvotes: 0

James Jithin
James Jithin

Reputation: 10555

I got converted Y and N to Yes and No by the following code:

<s:iterator value="myTestList" var="mytestBean">
    <s:property value="%{seqIdMap[#mytestBean.name]}" />
</s:iterator>

Here, mytestBean.name held the values of Y and N.

Upvotes: 1

Related Questions