0

I m new to JSF and i m facing since last two days to the following problem: I have two Input fields and one CommandButton, depending on which InputText filled, the appropriate event (on same button) will be triggered. I tried differnet approchs to resolve my problem however in vain, so now im looking for your help with all my thanks in advance

the first approch: i implemented a method actionButton that take ActionEvent parameter in order to sign it to commandButton-actionListener attribute.

second approch i developed a class which implemeted the ActionListener interface and ovverriding the processAction method

third approch i tried to use javaScript onclick method....

my application works fine as i ve one inputText field and one button,

here my files with some minimal changes: my Bean looks like as following:

    import java.sql.SQLException;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.event.ActionEvent;

import org.primefaces.context.RequestContext;
import ..basics.DataSetCollectionBeanBase;
import .webrfresnrsuche.services.WrrnsService;

@ManagedBean(name = "wrrnsBean")
@RequestScoped
public class WrrnsBean extends DataSetCollectionBeanBase {

    public String dbnumber;
    public String rfResNr;
    WrrnsService rrns;


    public WrrnsBean() {
    }

    public void startsearch(String dbnumber) throws SQLException {

        this.dbnumber = dbnumber;
        WrrnsService rrnss = new WrrnsService();
        this.rfResNr = rrnss.getRentfoxNummer(dbnumber);
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Rentfox-RES-Nummer:", this.rfResNr);
        RequestContext.getCurrentInstance().showMessageInDialog(message);
    }

    public void startsearchReservationNummer(String rentfoxresnr) throws SQLException {
        this.rfResNr = rentfoxresnr;
        rrns = new WrrnsService();

        try {

            this.dbnumber = rrns.getReservationNummer(rfResNr);
            String snunber = this.dbnumber;
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Original RES-Nummer", this.dbnumber);
            RequestContext.getCurrentInstance().showMessageInDialog(message);

        } catch (SQLException e) {
            System.out.println(e.getMessage() + "  " + e.getStackTrace());
        }
    }

    public String getdbnumber() {
        return dbnumber;
    }

    public void setdbnumber(String dbnumber) {
        this.dbnumber = dbnumber;
    }

    public String getRfResNr() {
        return rfResNr;
    }

    public void setRfResNr(String rfResNr) {
        this.rfResNr = rfResNr;
    }

    public WrrnsService getRrns() {
        return rrns;
    }

    public void setRrns(WrrnsService rrns) {
        this.rrns = rrns;
    }


    public void buttonAction(ActionEvent event) throws SQLException {



        List<UIComponent> components = event.getComponent().getParent().getChildren();

        for(UIComponent component:components){
            if(component.getId().equals("dbnumber"))
                startsearch(dbnumber);

            else if(component.getId().equals("reservationnummer"))
                startsearchReservationNummer(rfResNr);
        }

    }

}

my XHTML file looks like:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                template="/WEB-INF/templates/defaultTemplate.xhtml">

    <ui:define name="header">
        RES-Nr-Search
    </ui:define>

    <ui:define name="content">
        <h:form>
            <h:outputScript name="jsf.js" library="javax.faces" targer="head"></h:outputScript>
            <p:panel header="inpt Db number"
                     styleClass="ui-panel-titlebar ui-helper-clearfix ui-corner-all">
                <h:panelGrid id="searchPanel">
                    <f:facet name="header">
                        <p:row>
                            <p:column>
                                <p:outputLabel
                                    style="font-size:0.9em; float:left"
                                    for="dbnumber">start searching.
                                </p:outputLabel>
                            </p:column>
                        </p:row>
                        <h:outputText value="&lt;br/&gt;" escape="false" />
                        <p:row>
                            <p:column>
                                <p:outputLabel
                                    style="font-size:0.9em; float:left"
                                    for="dbnumber"> Pop-Up-window will be prompt.
                                </p:outputLabel>
                            </p:column>
                        </p:row>
                    </f:facet>
                    <h:outputText value="&lt;br/&gt;" escape="false" />
                    <p:row>
                        <p:column>
                            <h:outputLabel value="Nummer Eingeben" for="dbnumber" style="float:left"/>
                            <p:inputText id="dbnumber"
                                         value="#{wrrnsBean.dbnumber}" style="float:left" >

                             </p:inputText>
                             <h:message for="dbnumber" style="coloe:red"/>
                        </p:column>
                    </p:row>
                    <h:outputText value="&lt;br/&gt;" escape="false" />
                    <p:row>
                        <p:column> 
                            <h:outputLabel value="Rentfox-Res-mmer" for="reservationnummer" style="float:left"/>
                            <p:inputText id="reservationnummer" 
                                         value="#{wrrnsBean.rfResNr}" style="float:left" >                                          

                             </p:inputText> 

                         </p:column>
                   </p:row> 
                    <h:outputText value="&lt;br/&gt;" escape="false" /> 
                    <p:row>


                        <p:column>
                       <p:commandButton id="searchstart" value=" start search..."  onclick="jsf.ajax.request(this, event, {exceute: 'form:searchstart}); return false;"
                                        actionListener="#{wrrnsBean.buttonAction}">

                             </p:commandButton>
                        </p:column>
                    </p:row>
                </h:panelGrid>
            </p:panel>
        </h:form>
    </ui:define>
</ui:composition>
Derdy
  • 1
  • 2
  • What is the real usecase? I get the impression you ran into a http://www.xyproblem.info – Kukeltje Sep 20 '16 at 08:18
  • I could not bind properly the commandButton to the action or actionListener, in my case to buttonAction method. inorder to display result should i use a set or setValue method in the buttonAction. – Derdy Sep 20 '16 at 08:26
  • That is your technical issue (which I still fail to understand). Describe the functional usecase. And why do you manually include ``? – Kukeltje Sep 20 '16 at 10:06
  • during my searsch im some waht in confusion. when it s appropriate only to use only actionListner as attribute and when its convienient to use both attributes action and actionListener in a commandbutton Tag? – Derdy Sep 20 '16 at 11:18
  • http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener – Kukeltje Sep 20 '16 at 15:58

0 Answers0