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="<br/>" 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="<br/>" 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="<br/>" 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="<br/>" 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>