JSF 添加操作侦听器
我正在动态创建一个 MenuItem,并且想在单击 MenuItem 时添加自定义侦听器。
我尝试添加 addActionListener 和 setActionListener 但当单击链接时这些都不会被调用。
似乎有一个名为“侦听器”的列表附加到 MenuItem(我可以在使用侦听器静态调试 MenuItem 设置时看到这一点)。知道如何正确添加监听器吗?
I'm creating a MenuItem dynamically and I want to add a custom listener when the MenuItem is clicked.
I've tried adding addActionListener and setActionListener but neither of these get called when the link is clicked.
It appears that there is a List called "listeners" attached to MenuItem (I can see this when debugging a MenuItem setup with the listener statically). Any idea how to add the listener correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
需要按如下方式创建和添加它们(复制自 我之前的答案之一):
...其中
#{bean.actionListener}
实际存在,并且在与托管 bean 名称bean 关联的支持 bean 类中声明如下
:更重要的是,您还需要为任何动态创建的
UICommand
(和UIInput
)组件提供一个固定的ID,否则它将获得一个自动生成的 ID,这可能导致 JSF 在应用请求值阶段无法定位/关联它。因此,也这样做:
They needs to be created and added as follows (copied from one of my previous answers):
...where
#{bean.actionListener}
actually exists and is declared like follows in the backing bean class associated with the managed bean namebean
:More importantingly, you need to give any dynamically created
UICommand
(andUIInput
) component in question a fixed ID as well, else it will get an autogenerated ID which may cause that JSF cannot locate/correlate it during the apply request values phase.Thus, do so as well:
BalusC指出的主要问题是你需要设置ID。
然后您可以添加事件监听器,如下所示:
私有MenuItem createItem(字符串名称){
菜单项项=新菜单项();
item.addActionListener(new ActionListener() {
The main issue as pointed out by BalusC is that you need to set the ID.
Then you can add event listeners as follows:
private MenuItem createItem(String name){
MenuItem item=new MenuItem();
item.addActionListener(new ActionListener() {