Primefaces中使用的p:commandButton无法调用action函数

17

我在使用Primefaces 3.2和JSF 2.1时遇到了一些麻烦。

我的代码如下:

<p:toolbar id="jeditortoolbar" styleClass="jeditortoolbar">
      <p:toolbarGroup align="left" height="25" style="height:25px">
        <p:commandButton type="button" title="#{msg.beenden}"/>
        <p:commandButton type="button" title="#{msg.neu}"/>
      </p:toolbarGroup>
</p:toolbar>

我查看 Primefaces Showcase 后,发现我的 p:commandButton 需要

actionListener="#{myBean.myActionMethod}"

我的 Bean 需要一个像这样的方法

public void myActionMethod(){}

我在p:toolbar标签周围使用了一个h:form

我的Bean是ViewScoped。

我的解决方案是,在*.xhtml文件中进行操作。

<p:commandButton type="button" title="#{msg.neu}" onclick="addNewEmptyFile()"/>
<p:remoteCommand name="addNewEmptyFile" update=":codeTabForm">
   <f:setPropertyActionListener value="#{true}" target="#{myBean.myEvent}"/>
</p:remoteCommand>

在MyBean.java中

private String myEvent;

public void setMyEvent(String value){ myActionMethod();}

这对我有效,但我认为这是非常糟糕的代码。

大家能帮帮我吗?


问题出在哪里?如果您像展示中那样尝试它会发生什么? - Matt Handy
当我添加ActionListener方法时,事件没有被调用。 - user1740789
4
您是否尝试从命令按钮中删除 type="button" - Matt Handy
2
谢谢!那个愚蠢的“type =”按钮“”删除使我很开心。非常感谢。 - user1740789
不,"每个人"都不能帮助你。但有些人可以。 :) - Andrew
1个回答

14

试试这个。

Bean.java

@ManagedBean
@ViewScoped
public class Bean {

    public String testButtonAction() {
        System.out.println("testButtonAction invoked");
        return "anotherPage.xhtml";
    }

    public void testButtonActionListener(ActionEvent event) {
        System.out.println("testButtonActionListener invoked");
    }

}

页面.xhtml

<p:toolbar>
  <p:toolbarGroup>
    <p:commandButton action="#{bean.testButtonAction}"/>
    <p:commandButton actionListener="#{bean.testButtonActionListener}"/>
  </p:toolbarGroup>
</p:toolbar>

6
希望IDE能够在方法签名不匹配时提醒开发者,而不是简单地忽略它。;-) - Leo
1
供日后参考:在我的情况下,我从某个示例中复制了属性type=button,并想知道为什么这段代码可以工作而我的按钮却不能。 - Thomas
4
删除 type="button" 解决了我的问题。 - Fabii
移除 type="submit" 对我来说解决了这个问题。 - Ghos3t

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接