在Java中将两个列表组合成set/hash/map,然后在Struts 1.x中显示
首先,这两个列表保存不同的数据集,但是通过 get 方法中的一些变量组合最终保存相同类型的数据。最终目标是使用根据名称(描述)排序在一起的两个列表来填充下拉列表。
基本原理是,有两个表保存数据类型,但保存方式如此不同,以至于几乎不可能编写良好的 SQL 语句来取出数据。最终结果是“name”或“nameIndex”对象。
两者都有一个索引代码,但这与相对的表无关,它与需要使用此代码更新的第三个表中存储的内容相关。表之间的代码永远不会匹配(一个是两个字符长,另一个是 3 个或更多)。
如何将这两个列表组合成用户的下拉列表,以便下拉列表的值是索引代码和显示为标签的描述?
示例:(
<html:select property="name">
<html:optionsCollection name="nameList" label="nameDescription" value="nameCode" />
</html:select>
<html:select property="nameIndex">
<html:optionsCollection name="nameIndexList" label="nameIndexDescription" value="nameIndexId.nameCode" />
</html:select>
注意“nameIndexId.nameCode”值):
<html:select property="allNames">
<html:optionsCollection name="allNames" label="nameDescription" value="nameCode" />
</html:select>
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:optionsCollection 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然几乎不可能,但也并非完全不可能。我在 SQL 中创建了一个视图,以使用我需要的数据同时提取两个列表,这是我的答案:
这将创建一个视图,您可以像任何普通表一样提取 ID、DISPLAY 和 TYPE 值。这可能不是经常遇到的问题,但对于将来当您无法更改数据结构时遇到此类垃圾的任何人来说,这都是一个很好的解决方案。
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:
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.
您可以在 DAO 逻辑中做的一件简单的事情是:通过填充两个列表(当然,使用 2 个循环)来创建 LabelValueBean 的对象。从第一个列表中,您必须创建标签为“nameDescription”、值为“nameCode”的对象,而从第二个列表中,它将是任一数据。您必须将 LabelValueBean 的这些对象添加到单个列表中,并将其设置为请求或会话的属性。现在在您的 JSP 中,您可以在 html:optionsCollection 标记中引用这个新列表。
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.