azaveri7
azaveri7

Reputation: 899

How to show 2 merged Arraylists merged using s:merge in same s:select in Struts2

I have two arraylists on my jsp, one arraylist is stored in servletcontext and the other in request in Struts2.

Now I am merging them using s:merge as follows:

<s:merge id="thirdList">
   <s:param value="#application[@list1]"/>
   <s:param value="#request.list2"/>
</s:merge>

I checked whether they are merged or not using following piece of code.

<s:iterator value="thirdList">
   <s:property/>
</s:iterator>

Both the arraylists are of POJOs, so it displays reference to these POJOs on jsp page.

Now I want to display the entire list into a drop-down list, so I am using for that.

<s:select ...... list="thirdList">
</s:select>

So on jsp page the dropdownlist shows the POJOs in the dropdown. Now I want to display some string values in drop down instead of POJO references.

The problem is : list1 is an arraylist of class(POJO) of object1 and list2 is an arraylist of class(POJO) of object2, and want their different properties to be shown in dropdown.

For e.g. there are 5 POJOs of type object1 in list1 and 10 POJOs of type object2 in list2, and my dropdown should contain total 15 entries.

To display these properties on jsp page, I used this code

<s:iterator value="thirdList">
        <s:property value="id_list1"/>
        <s:property value="description_list1"/>
        <s:property value="id_list2"/>
        <s:property value="description_list2"/>
</s:iterator>

The list is properly printed. Now in s:select tag, the attributes used are listKey and listValue.

Since I have to use a merged list for display in <s:select> tag, what should be my value for id_list1, id_list2, description_list1, description_list2 which I have to use in listKey and listValue.

Upvotes: 0

Views: 519

Answers (2)

azaveri7
azaveri7

Reputation: 899

You are right Aleksandr. I should not do this in view layer. But then I wonder, what is the use of s:merge tag.

To solve this, what I did was, is as below:

In the action class, I created a new arraylist . I created a new POJO lets say POJO t with two member variables int 'id' and string 'value'.

For both the arraylists, I iterated over them, copied their id and description which I need to show on dropdown list on jsp page into new POJO t and inserted this 't' instance into newly created arraylist. Then I stored this arraylist into request object.

Then in jsp page, in s:select tag, I iterated over this new list and for listKey and listValue, I used

listKey="id" and listValue="value"

And this worked.

A simple solution to a difficult problem.

Upvotes: 0

Aleksandr M
Aleksandr M

Reputation: 24396

First of all: Don't do this kind of work in a view layer. You should write an object that will hold values that you need to display in JSP, iterate both of your lists creating new instance of this view object with needed values and putting it to some list. Then in JSP you can iterate that list of the view objects.

That being said... moving forward to the hackish and not a best practice solution. I will demonstrate it on list which consist of String-s and Integer-s, if it is Integer than we want to call intValue() method, in case of String toUpperCase() method will be called. Because, by default, calling not existing property doesn't produce value that we can compare, we will abuse #attr to evaluate calls to the instance properties.

<s:select list="{'teststring', 2, 'somestring', 3}"
   listValue="#attr['top.intValue()'] == null ? top.toUpperCase() : top.intValue()" />

Note this won't work if you have null values in your properties.

And again don't do it in the view.

Upvotes: 0

Related Questions