0

I have two beans to be called and I want to use one button to call them. How to do this?

Here is my try:

<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>

But it throws this error:

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

Only because I'm using binding here :

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

It says The class textView.UploadText does not have the property 'upload' which is not true! I tried actionListner as well but it's not working.

Here is uploadtext class:

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);
        }
    }
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
sasuri
  • 972
  • 3
  • 11
  • 26
  • Could you show the class as well? – HoXa Dec 19 '14 at 12:41
  • Please see edited post above @HoXa – sasuri Dec 19 '14 at 12:58
  • Upload is method not a property, don't you have to add parenthesis? action="#{uploadImage.upload()}" – HoXa Dec 19 '14 at 13:03
  • You should use the `type` attribute of `` instead to refer to its implementation (a fully qualified class name) which `ActionListner` is implemented on or just the `actionListener` attribute of ``. – Tiny Dec 19 '14 at 14:28
  • Possible duplicate of [Call multiple bean method in primefaces simultaneously](https://stackoverflow.com/questions/18850322/call-multiple-bean-method-in-primefaces-simultaneously) – ziMtyth Sep 14 '17 at 08:55

1 Answers1

2

If you look at documentatnon for f:actionListener, you'll see that binding attribute must evaluate to javax.faces.event.ActionListener. This means that UploadText class needs to implement javax.faces.event.ActionListener interface (you have an example on this blog).

The easiest option (taken from here) is to add a method like this to your 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");
        }
    };
}

and target this method from f:actionListener

<f:actionListener binding="#{uploadText.createActionListener()}"/>

Using this technique, you can call multiple methods with single button.

Community
  • 1
  • 1
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68