Tuesday, January 20, 2009

Struts 2 JSP select tag woes

Today I have been sucking around with a Struts 2 tag that is really selfish. I need a multiple select that has preselected values.

Now since I don't want to spend much time for this implementation I am saving these values in a text field in database. And please don't tell me that there is another "correct" way of doing it (maybe saving the values as a realations in a table). Now the items in dropdown come from DB table with primary key ID-s as dropdown values.

And now we get to the fun part - Struts2 "s:select" tag is type safe! Since my list values come from DB primary keys they are integers. Since I want to store the selected values in a text field, they are strings. If I do string.split(), I still get strings. Struts 2 does'nt have a simple solution to this. I just have to save the same type of values in DB or convert the type of the values someplace. So there is no one-line approach to make preselected values in this setup.

---rant on---
Message to Struts/JSP developers. In the view level there has to be simple solutions to these kind of problems or Struts just will be tossed aside. So please, make a simple parameter to s:select that forces comparing strings whatever the source types are. Ups Struts is open source so I might just as well blame myself :p .
---rant off---

No comments:

Post a Comment