2014-03-31 76 views
0

Sample.xhtml命令按鈕不調用功能不起作用更新屬性

<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:p="http://primefaces.org/ui"> 

<h:head> 
    <f:event listener="#{sample.dosamplelist}" type="preRenderView" /> 
</h:head> 

<h:body> 
<h:form> 

    <h:panelGrid id="samplesetting" columns="6" cellpadding="5"> 
     <f:facet name="header">Name Setting</f:facet> 
     <h:outputLabel for="samplename" value="Name:" /> 
     <p:inputText value="#{sample.name}" id="samplename" 
      required="true" label="samplename" /> 
    </h:panelGrid> 


    <p:panel id="sampleview" header="Sample List"> 
     <p:dataTable var="spl" value="#{sample.samplelist}" rowKey="#{spl.name}" 
     selection="#{sample.selectedname}" 
     selectionMode="single"> 
      <p:column headerText="Name"> 
       <h:outputText value="#{spl.name}" /> 
      </p:column> 
      <p:column> 
       <p:commandButton id="one" value="View Details" action="#{sample.setSelectedsample(spl)}" update="@form:samplesetting"> 
       </p:commandButton> 
      </p:column>     
     </p:dataTable> 
    </p:panel> 

</h:form> 

Managed Bean的

@SuppressWarnings("serial") 
@ManagedBean(name = "sample") 
@RequestScoped 
public class Sample implements Serializable 
{ 
    private String   name; 
    private List<Sample> samplelist; 
    private String   selectedname; 

    //getters and setters 

    public void dosamplelist(ComponentSystemEvent event) 
    { 
      List<Sample> samplelist = new ArrayList<Sample>(); 

      Sample configA = new Sample(); 
      configA.setName("John"); 
      samplelist.add(configA); 

      Sample configB = new Sample(); 
      configB.setName("David"); 
      samplelist.add(configB); 

      this.samplelist = samplelist; 
    } 

    public void setSelectedsample(Sample smpl) 
    { 
      this.name = smpl.name; 
    } 
} 

這是有點大的樣本形式,需要的是,當我們從底部選擇表格行時,它將顯示到頂部輸入框用於編輯目的。

但是當我按下命令按鈕它不起作用。爲什麼?請問是什麼原因?

回答

0

可能的問題

一個明顯的問題是,在類級別,您已經定義:

private List<Sample> samplelist; 

然後你繼續前進,隱藏doSampleList變量與

List<Sample> samplelist = new ArrayList<Sample>(); 

加上你有你的豆標記爲@RequestScoped這一事實,它將保證th在JSF請求處理期間,samplelist的內容將不一致。

要解決

標記您的bean作爲@ViewScoped,而不是和你認爲合適的解決變量隱藏問題。

進一步閱讀: