0

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

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

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

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

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

4

2 回答 2

1

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:

于 2011-07-31T11:31:32.393 回答
0
  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().

也许您可能想遍历它们,因为您需要使用 String 构造函数创建SelectItem值:)

于 2011-07-31T11:15:10.800 回答