onLoad JSF 填充列表框

发布于 2024-11-27 04:54:23 字数 330 浏览 1 评论 0原文

我正在使用 netbeans 6.9.1 Glassfish 3。我有一个 JSF 文件,上面有一个 selectOneMenu 列表框。该列表框将填充来自数据库的值。

现在,当我单击表单上的按钮时,值将添加到列表框中。我想要做的是,在页面自行加载时填充列表框。

知道我应该怎么做吗?我尝试向 Java 构造函数添加代码,以便它调用它,但它不起作用。 (我收到一条警告,要求将类设为最终类 - 如果我这样做,我将无法从 JSF 访问任何方法)

我收到的警告 - 构造函数中可重写的方法调用

代码太大,我无法将其放在这里,我需要知道一个解决方案来克服这个问题

I am using netbeans 6.9.1 Glassfish 3. I have a JSF file, and there is a selectOneMenu listbox on it. this list box will be populated with values from the DB.

For now, when i click on a button on the form, the values get added to the listbox. What i want to do is, to populate the list box when the page loads it self.

Any idea how i should do this. I tried adding code to the Java constructor so it would call it, but it didn't work. (I get a warning asking to Make the class final - if i do this, i can't access any methods from my JSF)

The warning i received - overridable method calls in constructors

The code is too big i can't put it here, i need to know a solution to overcome this

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

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

发布评论

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

评论(2

柠北森屋 2024-12-04 04:54:23

知道我应该怎么做吗?我尝试向 Java 构造函数添加代码,以便它调用它,但它不起作用。 (我收到一条警告,要求将类设为最终类 - 如果我这样做,我将无法从 JSF 访问任何方法)

我收到的警告 - 构造函数中可重写的方法调用

这只是 Netbeans 警告,而不是 Java 编译错误。您的代码应该可以正常编译和运行。 Netbeans 只是想表现得更聪明,并提示您潜在的设计问题。您的构造函数正在调用同一类的抽象方法。这是否不好实际上取决于我们一无所知的设计残余,但您应该意识到它可能会导致代码中的错误和错误。对我来说,为什么要调用抽象方法来预填充下拉列表至少没有任何意义,所以可能这根本不相关。

忽略并运行它。或者重新考虑你的设计方法。

另请参阅:

Any idea how i should do this. I tried adding code to the Java constructor so it would call it, but it didn't work. (I get a warning asking to Make the class final - if i do this, i can't access any methods from my JSF)

The warning i received - overridable method calls in constructors

That's just a Netbeans warning, not a Java compilation error. Your code should compile and run just fine. Netbeans is just trying to be smart and hint you about a potential design problem. Your constructor is calling an abstract method of the very same class. Whether that's bad depends actually on the remnant of the design which we know nothing about, but you should realize that it can potentially lead to bugs and errors in the code. To me, it makes at least no sense why you would call an abstract method to prepopulate a dropdown, so probably this is not related at all.

Ignore and run it. Or rethink your design approach.

See also:

白馒头 2024-12-04 04:54:23
  1. 创建一些 ManagedBean 类并为其提供 SelectItem 列表(使其成为属性)
  2. 从 jsf (xhtml) 页面引用此属性

现在应该加载项目。

@ManagedBean
public class MyBean {
  private List<SelectItem> items; // populate from DB
  public List<SelectItem> getItems() { return items; }
  // no need to setter
}
<h:selectOneMenu value="#{myBean.whatever}">
    <f:selectITems value="#{myBean.items}" />
</h:selectOneMenu>

编辑:关于填充值...

通常您有数据库服务并将其实例化(或注入)到 bean 并填充项目,例如 items = service.loadItems()

也许您可能想要迭代它们,因为您需要创建 SelectItem 带有 String 构造函数的值:)

  1. Create some ManagedBean class and give it List of SelectItem (make it property)
  2. Refer this property from your jsf (xhtml) page

And items should now be loaded now.

@ManagedBean
public class MyBean {
  private List<SelectItem> items; // populate from DB
  public List<SelectItem> getItems() { return items; }
  // no need to setter
}
<h:selectOneMenu value="#{myBean.whatever}">
    <f:selectITems value="#{myBean.items}" />
</h:selectOneMenu>

Edit: About the populating values ...

Usually you have database service and instantiate it (or inject it) to bean and fill items, something like items = service.loadItems().

Maybe you might want to iterate over them, because you need to create SelectItem value with String constructor :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文