一键,多个动作

时间:2014-12-19 12:36:49

标签: jsf jsf-2 primefaces

我有两个要调用的bean,我想用一个按钮来调用它们。怎么做?

这是我的尝试:

<h:form enctype="multipart/form-data">
    <p:inputTextarea rows="6" cols="33" autoResize="false"
                     value="#{uploadText.text}" maxlength="174" />
</h:form>

<h:form enctype="multipart/form-data">
    <p:messages showDetail="true" />
    <p:panelGrid columns="2" style=" width:30px;">
        <h:outputLabel id="image" value="Select Image: *" />
        <p:fileUpload value="#{uploadImage.file}" mode="simple"
                      allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
    </p:panelGrid>

    <h:commandButton action="#{uploadImage.upload}" value="Submit">
        <f:actionListener binding="#{uploadText.upload}" />
    </h:commandButton>
</h:form>

但它引发了这个错误:

/calendar.xhtml @109,55 binding="#{uploadText.upload}": The class 'textView.UploadText' does not have the property 'upload'.

只是因为我在这里使用绑定:

<h:commandButton action="#{uploadImage.upload}" value="Submit">
    <f:actionListener binding="#{uploadText.upload}" />
</h:commandButton>

它表示课程textView.UploadText没有属性&#39;上传&#39;这不是真的!我也试过了actionListner,但它没有用。

这是uploadtext类:

public class UploadText implements Serializable {

    private static final long serialVersionUID = 1L;
    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void upload() {

        if (text != null) {

            try {
                String f_username = JloginDAO.user;

                Connection con = DBconnection.getConnection();
                PreparedStatement pre = con.prepareStatement("insert into upload_text (text,user_idt) values(?, (SELECT id from users WHERE username = ?))");
                pre.setString(1, text);
                pre.setString(2, f_username);

                pre.executeUpdate();
                System.out.println("Inserting Successfully!");
                pre.close();
                FacesMessage msg = new FacesMessage("Succesful", text + " is uploaded.");
                FacesContext.getCurrentInstance().addMessage(null, msg);

            } catch (Exception e) {
                System.out.println("Exception-File Upload." + e.getMessage());
            }
        } else {
            FacesMessage msg = new FacesMessage("Please select image!!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

如果查看documentatnon for f:actionListener,您会发现binding属性必须评估为javax.faces.event.ActionListener。这意味着UploadText类需要实现javax.faces.event.ActionListener接口(您在this blog上有一个示例)。

最简单的选择(取自here)是向你的bean添加这样的方法:

public ActionListener createActionListener() {
    return new ActionListener() {
        @Override
        public void processAction(ActionEvent event) throws AbortProcessingException {
            System.out.println("here I have both the event object, and access to the enclosing bean");
        }
    };
}

并从f:actionListener

定位此方法
<f:actionListener binding="#{uploadText.createActionListener()}"/>

使用此技术,您可以使用单个按钮调用多个方法。