onLoad JSF 填充列表框
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只是 Netbeans 警告,而不是 Java 编译错误。您的代码应该可以正常编译和运行。 Netbeans 只是想表现得更聪明,并提示您潜在的设计问题。您的构造函数正在调用同一类的抽象方法。这是否不好实际上取决于我们一无所知的设计残余,但您应该意识到它可能会导致代码中的错误和错误。对我来说,为什么要调用抽象方法来预填充下拉列表至少没有任何意义,所以可能这根本不相关。
忽略并运行它。或者重新考虑你的设计方法。
另请参阅:
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:
现在应该加载项目。
编辑:关于填充值...
通常您有数据库服务并将其实例化(或注入)到 bean 并填充项目,例如
items = service.loadItems()
。也许您可能想要迭代它们,因为您需要创建 SelectItem 带有 String 构造函数的值:)
And items should now be loaded now.
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 :)