【问题标题】:How to send a List from JSF to backing Bean如何将列表从 JSF 发送到支持 Bean
【发布时间】:2011-04-19 07:10:51
【问题描述】:

我正在使用 JSF1.2 中的数据表来填充从 Seam 组件/使用列表接收到的数据。当我使用列表时,正在获取数据。但是,当我想让 Datatable 可编辑时,以便可以将我在 JSF 页面上更改的值发送回 Seam 组件/Backing Bean,列表不会将值传递给 Seam 组件/Backing Bean。

我已经尝试过,但我无法再次将列表传递给 Seam 组件/Backing Bean。

下面是代码。

JSF 代码:

<h:dataTable value="#{mainbean.findCustomerlist}" var="findCustomerList">
   <h:column>
    <f:facet name="header">
     <h:outputText value="Sr No" />
    </f:facet>
    <h:outputText value="#{findCustomerList.id}" />
   </h:column>
   <h:column>
    <f:facet name="header">
     <h:outputText value="Company Name" />
    </f:facet>
    <h:inputText value="#{findCustomerList.companyName}" />
   </h:column>

   <h:column>
    <f:facet name="header">
     <h:outputText value="Account Number" />
    </f:facet>
    <h:inputText value="#{findCustomerList.accountNumber}" />
   </h:column>
   <h:column>
    <f:facet name="header">
     <h:outputText value="Contact Number" />
    </f:facet>
    <h:inputText value="#{findCustomerList.contactNumber}" />
   </h:column>
   <h:column>
    <f:facet name="header">
     <h:outputText value="Contact Name" />
    </f:facet>
    <h:inputText value="#{findCustomerList.contactName}" />
            </h:column>
      <br></br>

  </h:dataTable>

<h:commandButton value="Update" type="submit" action="#{mainbean.modifyCustomer(findCustomerlist)}" />

Seam 组件/Backing Bean 代码:

private List<Customer> findCustomerlist=null;


public List<Customer> getFindCustomerlist() {
  return findCustomerlist;
 }

 public void setFindCustomerlist(List<Customer> findCustomerlist) {
  this.findCustomerlist = findCustomerlist;
}


public void searchCustomer(ActionEvent actionEvent)
    {
     findCustomerlist=session.findCustomer(customerName);
    }

/* The searchCustomer function works fine and it returns the list to the JSF. But when I use the modifyCustomer function to retrieve the value from JSF then it is not working.*/  

    public void modifyCustomer(List<Customer> findCustomerlist)
    {
         session.updateCustomer(findCustomerlist);
         System.out.println("Inside modifyCustomer");
         System.out.println(findCustomerlist.get(0).getCompanyName());
    } 

【问题讨论】:

    标签: jsf


    【解决方案1】:

    我不确定 Seam 对此做了什么,但在正常的 JSF 中这是不必要的。通常,JSF 在更新模型值阶段已经更新了列表属性。您只需将其作为本地 bean 属性进行访问。即

    <h:commandButton value="Update" type="submit" action="#{mainbean.modifyCustomer}" />
    

    public void modifyCustomer()
    {
         session.updateCustomer(findCustomerlist);
         System.out.println("Inside modifyCustomer");
         System.out.println(findCustomerlist.get(0).getCompanyName());
    } 
    

    应该已经足够了。

    您只需要确保支持 bean 保留相同的列表。当 bean 是请求范围时,您所显示的代码将失败。如果您使用的是 JSF 2.0,您可以将 bean 放在 view 范围内。在 JSF 1.x 上,您需要将 bean 放在 session 范围内(不推荐)或在 bean 的构造函数或 @PostConstruct 中基于 customerName 预加载列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 2012-11-15
      相关资源
      最近更新 更多