Search code examples
javaxpagesactionlistener

ActionListener - how to call them with sending values to them


I have a button for which I have defined an actionlistener:

<xp:button value="#{matter.attachment_upload}"
        id="btnSaveFile" styleClass="btnSaveFile" disabled="true"
        title="#{matter.gen_Attachment_Help}">
        <xp:this.attrs>
            <xp:attr name="data-placement"
                value="bottom">
            </xp:attr>
            <xp:attr name="data-toggle"
                value="tooltip">
            </xp:attr>
        </xp:this.attrs>
        <i class="fa fa-upload" aria-hidden="true" />
        &#160;
        <xp:eventHandler event="onclick"
            submit="true" refreshMode="complete" disableValidators="true">
            <xp:this.action><![CDATA[#{javascript:attachmentBean.save(compositeData.ref,compositeData.key)}]]></xp:this.action>
            <xp:this.onComplete><![CDATA[//pnlFiles
XSP.partialRefreshGet('#{id:pnlFiles}');]]></xp:this.onComplete>
            <xp:this.actionListeners>
    <xp:actionListener
        type="se.acme.projectx.app.HelloWorld">
    </xp:actionListener>
</xp:this.actionListeners></xp:eventHandler>
</xp:button>

which is connected to this java class:

package se.acme.projectx.app;

import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

public class HelloWorld implements ActionListener {

    private String SomeVariable;
    private String AnotherVariable;

    // Class Constructor...
    public HelloWorld() {
        System.out.println("HelloWOrld");
    }

    // Property getters/setters
    public String getSomeVariable() {
        return SomeVariable;
    }

    public String getAnotherVariable() {
        return AnotherVariable;
    }

    public void setSomeVariable(String someVariable) {
        SomeVariable = someVariable;
    }

    public void setAnotherVariable(String anotherVariable) {
        AnotherVariable = anotherVariable;
    }

    @Override
    public void processAction(ActionEvent arg0) throws AbortProcessingException {
        // TODO Auto-generated method stub

    }
}

What I would like is to send a parameter (UNID of a document) to the class, locate a document, build a java object with the data and present the result on the screen e.g. in a dialog box.

The question is: how do I do this?

When defining the actionlistener I can only define the type. not parameters...


Solution

  • The ActionEvent transfers the data to your ActionListener. You have to get the event handler like this:

    package ch.hasselba.xpages;
    
    import java.util.List;
    import javax.faces.event.AbortProcessingException;
    import javax.faces.event.ActionEvent;
    import com.ibm.xsp.complex.Parameter;
    import com.ibm.xsp.component.xp.XspEventHandler;
    
    public class MyActionListener implements javax.faces.event.ActionListener {
    
        public void processAction(ActionEvent event)
                throws AbortProcessingException {
            XspEventHandler eventHandler = (XspEventHandler) event.getSource();
            List<Parameter> params = eventHandler.getParameters();
            for (Parameter p : params) {
                System.out.println(p.getName() + " -> " + p.getValue());
            }
        }
    }
    

    To set the parameter, add a parameter to your event handler:

    <?xml version="1.0" encoding="UTF-8"?>
    <xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    
        <xp:inputText
            id="inputTextMyData"
            value="#{requestScope.myData}">
        </xp:inputText>
    
        <xp:button
            value="do It!"
                id="buttonAction">
            <xp:eventHandler
                event="onclick"
                submit="true"
                refreshMode="norefresh">
    
                <xp:this.parameters>
                    <xp:parameter name="param">
                        <xp:this.value><![CDATA[#{javascript:requestScope.get('myData')}]]></xp:this.value>
                    </xp:parameter>
                </xp:this.parameters>
    
                <xp:this.actionListeners>
                        <xp:actionListener type="ch.hasselba.xpages.MyActionListener" />
                    </xp:this.actionListeners>
                </xp:eventHandler>
    
        </xp:button>