java - 正确使用 LabelValueBean

标签 java jsp jakarta-ee struts2 ognl

我有一个变量:

private ArrayList<LabelValueBean> circleNameIdList;

在我的 Action 类中,它的值被填充。

I want to display the label in my drop-down in JSP and when one label is selected, the corresponding value to that particular label in circleNameIdList to be passed to the server. 例如:如果标签:NewYork 被选中,那么它对应的 id = 5 被发送到服务器。

我怎样才能做到这一点?

到目前为止,我在 JSP 中是这样做的:

<s:select list="#session.circleNameIdList" label="Select Circle:" name="circleNameIdList" id="circleNameIdList"></s:select>

但是,它显示不正确。

最佳答案

我看到您正在使用 LableValueBean 来填充和显示下拉列表。它是一个以前的 bean,最后用于显示对象列表。在 Struts2 中,您不再需要这样的助手 bean。您可以通过指定一个键字段来显示对象列表,该键字段将包含所选选项的唯一值和要显示为选项文本的值。例如,如果您的对象

public class Circle {
   private Long id;
   //getter and setter here

  private String name;
  //getter and setter here
} 

你在 Action 类中有

private List<Circle> circleNameIdList;
//getter and setter here

/**
 * Hold the selected value
 */
private Long circleId;
//getter and setter here

然后

<s:select id="circleNameIdListID" label="Circle:" name="circleId" 
  list="circleNameIdList"   listKey="id" listValue="name" headerKey="-1" headerValue="Select Circle"/>

可用于显示下拉菜单。

关于java - 正确使用 LabelValueBean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20417711/

相关文章:

java - JSP 文件中的母版页

java - Eclipse Juno 中服务器启动失败

java - Android:渲染器的两种不同用途

java - java中通过点击鼠标将变量值传递给servlet

java - Servlet 给出 404 而不是加载适当的页面。控制台在启动 Tomcat 时显示严重错误

java - 表示层中的实体类?

java - Spring webflow 遇到问题

jakarta-ee - 集中 quarkus 的通用配置

java - 在Java中遍历双向链表?

java - 缺少/不可用依赖项错误的服务