开发者

Combine two lists into set/hash/map in Java, then display in Struts 1.x

开发者 https://www.devze.com 2023-04-02 08:32 出处:网络
First off the two lists hold different sets of data, however through some combining of variables in the get method eventually hold the same type of data.The end goal is to populate a drop down with bo

First off the two lists hold different sets of data, however through some combining of variables in the get method eventually hold the same type of data. The end goal is to populate a drop down with both lists sorted together based on their names (description).

The basic is that there are TWO tables holding the type of data, but holding it so differently that it's virtually impossible to write a good SQL statement to get it out. The end result is a "name" or "nameIndex" object.

Both have an indexcode, but that is not related to the opposing table, it IS related to what is stored in a third table that needs to be updated with this code. The codes will never match between tables (one is two characters long, the other 3 or more).

How do I combine these two lists into a dropdown for the user so that the value of the dropdown is the indexcode and the description displayed as the label?

Example:

<html:select property="name">
   <html:options开发者_C百科Collection name="nameList" label="nameDescription" value="nameCode" />
</html:select>

<html:select property="nameIndex">
   <html:optionsCollection name="nameIndexList" label="nameIndexDescription" value="nameIndexId.nameCode" />
</html:select>

(note the "nameIndexId.nameCode" value) into this:

<html:select property="allNames">
   <html:optionsCollection name="allNames" label="nameDescription" value="nameCode" />
</html:select>


While virtually impossible, it was not completely impossible. I created a view in SQL to pull both lists at once with the data I needed, here is my answer:

CREATE OR REPLACE VIEW FULL_NAME_LIST_VIEW AS SELECT 
    N.NAME_ID AS ID, 
    N.DISPLAY DISPLAY,
    'BOSS' TYPE
FROM NAME N
UNION
SELECT 
    NI.NAME_INDEX_ID AS ID,
    (FIRST.DISPLAY || ' - ' || LAST.DISPLAY) AS DISPLAY,
    NI.TYPE_ID TYPE
FROM NAME_INDEX NI,
     NAMEHOLDER FIRST,
     NAMEHOLDER LAST
WHERE NI.FIRST_ID = FIRST.NAME_ID
AND NI.LAST_ID = LAST.LAST_ID;

This will create a view which you cal pull like any normal table for values of ID, DISPLAY and TYPE. It's probably not an often run into problem, but this is a good solution for anyone who runs into junk like this in the future when you can't change the data structure.


One simple thing you can do in your DAO logic, is; create objects of LabelValueBean by populating both the lists (of course, using 2 loops). From first list you have to create objects with label as "nameDescription" and value as "nameCode" and from second list it will be of either of data. You have to add these objects of LabelValueBean to a single list and set it as an attribute to either request or session. In your JSP now, you can refer this new list in html:optionsCollection tag.

0

精彩评论

暂无评论...
验证码 换一张
取 消