如何访问bean中的表单对象

时间:2013-07-31 10:00:01

标签: jsf

Iam使用ajax调用对话框,我的问题是我已经在对话框中定义了一个表单并且在我单击保存时接受数据我希望我的Bean中的详细信息。

我的问题:1)当我点击保存按钮时,它没有点击Bean。 2)我无法在我的bean中访问此表单的对象。

提前致谢:)

我的JSF文件

<h:form id="outCompForm">
   <table border="0"  class="InnerBox"    width="60%">
              <tr><th  colspan="7" align="Center" class="clsTitle">OUTBOUND COMPLAINTS</th></tr>
              <tr>
                   <td>Consignment No </td>
                   <td>Consignor Branch</td>
                   <td>Consignor  </td>
                   <td>Contact No </td>
                    <td>Remark</td>
              </tr>
               <tr>
                   <td><h:inputText  id="acno" value="#{outBound.accNo}" onfocus="this.select()" styleClass="formText"  /></td>
                   <td>
                            <h:selectOneMenu value="#{outBound.cnor_Branch}" id="fcoBran" styleClass="formText" >
                                <f:selectItems value="#{outBound.cnor_BranchLi}" var="c" />
                            </h:selectOneMenu>
                        </td>
                        <td>
                            <p:selectOneMenu value="#{outBound.cnor_Name}"  id="fCnNam" editable="true">  
                                <f:selectItem itemLabel="Select One" itemValue="" />  
                                <f:selectItems value="#{outBound.cnor_NameLi}" />  
                            </p:selectOneMenu>  
                        </td>
                        <td><h:inputText  id="fcoCont" value="#{outBound.cnor_Contact}" onfocus="this.select()" styleClass="formText"  /></td>
                        <td><h:inputText  id="fremark" value="#{outBound.remark}" onfocus="this.select()" styleClass="formText"  /></td>
                    </tr>
                    <tr>
                        <td>Consignee Branch</td>
                        <td>Consignee Name </td>
                        <td>Address</td>
                        <td>Contact No</td>
                        <td >Booking Date </td>
                    </tr>
                    <tr>
                        <td>
                            <h:selectOneMenu value="#{outBound.cnee_Branch}" id="fceBran" styleClass="formText" >
                                <f:selectItems value="#{outBound.cnee_BranchLi}" var="c" />
                            </h:selectOneMenu>
                        </td>
                        <td><h:inputText  id="fceName" value="#{outBound.cnee_Name}" onfocus="this.select()" styleClass="formText"  /></td>
                        <td><h:inputText  id="fceAdr" value="#{outBound.cnee_Addr}" onfocus="this.select()" styleClass="formText"  /></td>
                        <td><h:inputText  id="fceCnt" value="#{outBound.cnee_Contact}" onfocus="this.select()" styleClass="formText"  /></td>
                        <td>  <p:calendar value="#{outBound.booking_Date}" id="fBDat"  >  </p:calendar>  </td> 
                        <td> <h:commandButton value="Save" action="#{outBound.saveAction}" onclick="return validate()" /> </td> 
                    </tr>
                    <tr>
                        <td colspan="7">

                            <h:message for="acno"/>
                        </td>
                    </tr>
                </table>
                <p:growl id="growl" showDetail="true"/>  
                <p:dataTable id="tbl" var="o" value="#{  outBound.tick_List}"  
                             paginator="true" rows="10" rowsPerPageTemplate="5,10,15" 
                             rowKey="#{o.accNo}" selection="#{outBound.outSelected}" selectionMode="single"  >  

                    <p:ajax event="rowSelect" listener="#{outBound.onRowSelect}"    
                            update=":outCompForm:display :outCompForm:growl" oncomplete="AccDialog.show()"   />  

                    <p:column headerText="Booking Date" sortBy="#{o.date_conv}">
                        <h:outputText value=" #{o.date_conv.substring(0, 10)}" /> 
                    </p:column>
                    <p:column headerText="Consignment No" sortBy="#{o.accNo}">
                        <h:outputText value=" #{o.accNo}" />  
                    </p:column>
                    <p:column headerText="Con Branch" sortBy="#{o.cnor_Branch}">
                        <h:outputText value=" #{o.cnor_Branch}" />  
                    </p:column >
                    <p:column headerText="Consignor" sortBy="#{o.cnor_Name}">
                        <h:outputText value=" #{o.cnor_Name}" /> 
                    </p:column>
                    <p:column headerText="Con Contact" sortBy="#{o.cnor_Contact}">
                        <h:outputText value=" #{o.cnor_Contact}" /> 
                    </p:column>
                    <p:column headerText="Remarks" sortBy="#{o.remark}">
                        <h:outputText value=" #{o.remark}" /> 
                    </p:column>
                    <p:column headerText="Cnee Branch" sortBy="#{o.cnee_Branch}">
                        <h:outputText value=" #{o.cnee_Branch}" /> 
                    </p:column>
                    <p:column headerText="Cnee Name" sortBy=" #{o.cnee_Name}">
                        <h:outputText value=" #{o.cnee_Name}" /> 
                    </p:column>
                    <p:column headerText="Cnee Addr" sortBy="#{o.cnee_Addr}">
                        <h:outputText value=" #{o.cnee_Addr}" /> 
                    </p:column>
                    <p:column headerText="Cnee Contact" sortBy="#{o.cnee_Contact}">
                        <h:outputText value=" #{o.cnee_Contact}" /> 
                    </p:column>


                </p:dataTable>



                <p:dialog id="dialog" header="Complaint Details" widgetVar="AccDialog" resizable="false" appendToBody="true"  
                          showEffect="fade" hideEffect="slide">
                    <h:panelGrid id="display" columns="2" cellpadding="4">  
                        <h:form id="formtrans">
                            <table border="0"  class="PopupBox"  >
                                <tr>
                                    <td>Consignment No.</td>
                                    <td colspan="3"> <h:outputText value="#{outBound.outSelected.accNo}" />  </td>
                                </tr>
                                <tr class="FormLabel" >
                                    <td>Detail</td>
                                    <td>Suggestion</td>
                                    <td>Privacy</td>
                                    <td>Status</td>
                                </tr>
                                <tr>
                                    <td><h:inputTextarea  id="fdetail" value="#{outBound_trans.details}" onfocus="this.select()" styleClass="formText"  /></td>
                                    <td><h:inputTextarea  id="fsuggest" value="#{outBound_trans.suggest}" onfocus="this.select()" styleClass="formText"  /></td>
                                    <td>
                                        <h:selectOneMenu value="#{outBound_trans.privacy}" id="fpriv" styleClass="formText" >
                                            <f:selectItems value="#{outBound_trans.privacyLi}" var="c" />
                                        </h:selectOneMenu>
                                    </td>
                                    <td>
                                        <h:selectOneMenu value="#{outBound_trans.status}" id="fstatus" styleClass="formText" >
                                            <f:selectItems value="#{outBound_trans.statusLi}" var="c" />
                                        </h:selectOneMenu>
                                    </td>


                                </tr>
                                <tr>
                                    **<td> <h:commandButton value="Save" action="#{outBound_trans.saveTrans}" onclick="return validate1()" /> </td> **
                          </tr>
               </table>

            </h:form>
      </h:panelGrid>  
     </p:dialog>
</h:form>

outBound bean

@ManagedBean(name = "outBound")
@SessionScoped
public class OutBound implements Serializable {

private String dataBase, finYear;
private int userCode, cnt = 0, ticketNo;
private String accNo, cnor_Branch, cnor_Name, cnor_Contact, cnee_Branch, cnee_Name, cnee_Addr, cnee_Contact, message = "Error";

private String cnor_Addr, cnor_Email, cnee_Email, remark;
private Date booking_Date;
private int cnee_branchCode, cnor_Code;
private ArrayList cnor_BranchLi = new ArrayList();
private ArrayList cnee_BranchLi = new ArrayList();
private ArrayList cnor_NameLi = new ArrayList();
DataFetch fetch;
DataConnect dataPush;
FacesMessage doneMessage = null;
ResultSet rs;
private ArrayList<OutBoundB> Tick_List;
Connection con;
Statement stat;
PreparedStatement pstat;
DBConnection dbconn = new DBConnection();
Utility dateFetch = new Utility();
private OutBoundB outSelected;


public OutBound() {
    FacesContext ctx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) ctx.getExternalContext().getSession(false);
    this.dataBase = (String) session.getAttribute("dataBase");
    this.finYear = (String) session.getAttribute("FinYear");
    this.userCode = (Integer) session.getAttribute("userCode");

    if (this.dataBase == null) {
        doneMessage = new FacesMessage("Session Expired");
    } else {
        fetch = new DataFetch(this.dataBase);
        cnor_BranchLi = fetch.getArrayString("select subbranchcode from subbranch  order by subbranchcode");
        cnor_NameLi = fetch.getArrayString("Select FirmName from accountscustomer where flag='Acti' order by FirmName");
        cnee_BranchLi = fetch.getArrayString("select distinct city from destinationmaster order by city");
        addList();
        fetch.closeAll();
    }


}

public String saveTicketOut() {
    // program to save to database
}

public String saveAction() {
    //validation before saving
    return null;
}



public void onRowSelect(SelectEvent event) {  
FacesMessage msg = new FacesMessage("Acc Selected", ((OutBoundB) event.getObject()).getAccNo());  
   FacesContext.getCurrentInstance().addMessage(null, msg);  
}  

public void onRowUnselect(UnselectEvent event) {  
    FacesMessage msg = new FacesMessage("Acc Unselected", ((OutBoundB) event.getObject()).getAccNo());  
    System.out.println("entered unselect");
    FacesContext.getCurrentInstance().addMessage(null, msg);  
}  


public static class OutBoundB {
    private int cnor_branchCode, cnee_branchCode, cnor_Code, userCode, ticketNo;
    private String accNo, cnor_Branch, cnor_Name, cnor_Contact, cnee_Branch, cnee_Name, cnee_Addr, cnee_Contact,date_conv;
    private String cnor_Addr, cnor_Email, cnee_Email, remark, type, escala;
    private Date booking_Date, ticket_Date;
    Utility dateFetch1 = new Utility();

    public OutBoundB() {
    }

    public OutBoundB(String conBran, String conNam, String cnoCnt, String acno, String cneeBran, String cneeNam, String cneAdd, String cneeCnt, Date boDate) {
        this.accNo = acno;
        this.cnor_Branch = conBran;
        this.cnor_Name = conNam;
        this.cnor_Contact = cnoCnt;
        this.cnee_Name = cneeNam;
        this.cnee_Branch = cneeBran;
        this.cnee_Addr = cneAdd;
        this.cnee_Contact = cneeCnt;
        this.date_conv = dateFetch1.setCorrectDate(boDate);
        this.booking_Date = boDate;
    }
 }
}

//我删除了所有的getter和setter outBound_trans bean

@ManagedBean(name = "outBound_trans")
@SessionScoped
public class OutBoundTrans {

private String dataBase, finYear, userName;
private int userCode;
private String  details, suggest,  privacy, status,message;
private ArrayList statusLi = new ArrayList();
private ArrayList privacyLi = new ArrayList();
//
DataFetch fetch;
 Connection con;
Statement stat;
PreparedStatement pstat;
DBConnection dbconn = new DBConnection();
Utility dateFetch = new Utility();
//

public void saveTrans( ) {

System.out.println(this.details);
if(this.details==null){
      System.out.println("save trans");
 }else{
      System.out.println(this.details);
 }
  // code to validate and save 
}

public class outboundTransB {

    private int ticketNo, responseCount;
    private Date resp_Date;
    private String oeBrach, deBranch, details, suggest, ticketType, flag, privacy;
  }
}

1 个答案:

答案 0 :(得分:1)

审核完代码后,我注意到您已嵌套表单!这极其糟糕,可能会造成不可预测的行为。如果问题仍然存在,请对其进行修复并再次测试。

其他信息

如果您的问题是能够检索某些表单值,我建议您查看:

String value = FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap().get("myInputField");

正在调用的方法内部。这可以让你从表单中的方法中获取任何值。