struts2 optiontransferselect retrieve and display value from database

Solution 1:

I think there are two things you need to know in order to solve your issue:

  1. The lifetime of a Struts action.
  2. How the optiontransferselect actually works.

Lifetime of a Struts Action

I start with that, because that might already solve your issue.

👉 A new instance of a Struts action is created for each request.

That means for you when connect the attributes list (left side) and doubleList (right side) to fields in a class derived from ActionSupport - like in the example OptionTransferSelectAction above - you must ensure, that you initialize it with meaningful values, I mean values that reflect the current state you want to show to the user. In the worst case you have to fetch the values from the database with each request.

Alternatively you can inject a bean into the action with a dedicated lifetime (e.g. session scoped). I might line that out in a separate post.

(BTW I would be thankful if someone else could provide further information, if the lifetime of a Struts action can be changed directly).


How optiontransferselect works

This tag wraps up two select tags (or more specifically updownselect tags). I will line out the function of it first, because it makes us better understand optiontransferselect. (Sidenote: Contrary to JSF, Struts clearly distinguishes between attributes for get and set operations.)

To connect the select tag with the underlying data, these are the three relevant attributes:

  1. list - This is the list of all available values (=get).
  2. value - This is the list of values initially selected (=get).
  3. name - The name of the property (field) of the action where the selected values will be supplied on form submit (=set).

So the only property your action will recieve is the one connected with name. The select tag will NOT...

  • ...post back the complete list of values (the value of list) to the action.
  • ...change the values of list and value in the underlying Java class / action.

And the same is true for optiontransferselect. Which makes the semantics quite weird, because some changes (namely which values show up left and which show up right) will never be post back to the Struts action, you only can get hold of the selected values of both sides.

The major difference is that you have those three attributes now twice:

  1. list --> list for the left and doubleList for the right side.
  2. value --> value for the left and doubleValue for the right side.
  3. name --> name for the left and doubleName for the right side.

The question now is how to come by that you will only get the selected values? I would suggest to automatically select them right before submission:

<s:submit value="Save" onclick="selectAllOptions(document.getElementById('idLeft'));selectAllOptions(document.getElementById('idRight'));"/>

Where idLeft corresponds to the value of id in the optiontransferselect and idRight corresponds to the value of doubleId. With that change you would get the complete list of left and right values in name and doubleName respectively.

The code is exactly the same as for the button you would get with allowSelectAll="true" and the JavaScript method selectAllOptions is provided by Struts via the optiontransferselect.js, regardless of allowSelectAll was true or false.