如何填充组合框,从枚举中获取值

发布于 2024-12-04 15:18:24 字数 839 浏览 4 评论 0原文

我有一个带有一个组合框的 jsp 页面,并且我有一个枚举类。

我想在 jsp 中填充组合框,填充后我想将这些值保存在 D/B 中,如何在 Struts 2 中做到这一点

public enum Roles {
  ONE   ("One"),
  TWO   ("Two"),
  THREE ("Three"),
  FOUR  ("Four"),
  FIVE  ("Five"),
  ALL   ("All");

  private final String displayValue;

  private Roles(String displayString){
      this.displayValue = displayString;
  }
  public String getDisplayString() {
      return displayValue;
  } 

  public static Roles getRoleOf(String displayValue){         
      if(displayValue.equals("One"))
          return ONE;
      if(displayValue.equals("Two"))
          return TWO;
      if(displayValue.equals("Three"))
          return THREE;
      if(displayValue.equals("Four"))
          return FOUR;
        if(displayValue.equals("All"))
          return ALL;
      else return ALL;
  }
}

I have a jsp page with one combo box and I have a class which is an enum.

I want to populate my combo box in jsp and when it's populated I want to save these value in D/B how can I do it in Struts 2

public enum Roles {
  ONE   ("One"),
  TWO   ("Two"),
  THREE ("Three"),
  FOUR  ("Four"),
  FIVE  ("Five"),
  ALL   ("All");

  private final String displayValue;

  private Roles(String displayString){
      this.displayValue = displayString;
  }
  public String getDisplayString() {
      return displayValue;
  } 

  public static Roles getRoleOf(String displayValue){         
      if(displayValue.equals("One"))
          return ONE;
      if(displayValue.equals("Two"))
          return TWO;
      if(displayValue.equals("Three"))
          return THREE;
      if(displayValue.equals("Four"))
          return FOUR;
        if(displayValue.equals("All"))
          return ALL;
      else return ALL;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2024-12-11 15:18:24

您可以使用 Roles.values() 获取所有枚举值并从那里开始。

you can use Roles.values() to get all the enum values and go from there.

愛放△進行李 2024-12-11 15:18:24
Roles[] allRoles = Roles.values();
for(Roles role : allRoles) {
    // add role to combo box using role.getDisplayString
}
Roles[] allRoles = Roles.values();
for(Roles role : allRoles) {
    // add role to combo box using role.getDisplayString
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文