2013-02-22 48 views
3

當我從selectOneMenu中選擇另一個Skin時,我想更改inputTexts的值。 一切都很好,我的轉換器從菜單中返回正確的對象,但inputTexts沒有更新。selectOneMenu選擇後的JSF更新inputText

<h:form> 
    <h:selectOneMenu id="dropdownSkin" 
     value="#{helloBean.currentSkin}" defaultLabel="Select a skin.." 
     valueChangeListener="#{helloBean.skinValueChanged}" immediate="true" 
     onchange="this.form.submit()" converter="SkinConverter" > 
     <f:selectItems value="#{helloBean.mySkinsSI}" var="c" 
      itemValue="#{c.value}" /> 
    </h:selectOneMenu> 


    <br /> 
    <h:inputText id="name" value="#{helloBean.currentSkin.title}"></h:inputText> 
    <br /> 
    <h:inputText id="tcolor" value="#{helloBean.currentSkin.titleBar.textColor}"></h:inputText> 
    <br /> 
    <h:inputText id="bcolor" value="#{helloBean.currentSkin.titleBar.backgroundColorStart}"></h:inputText> 
</h:form> 

這是我的Bean的樣子。我調試了它並且Object currentSkin設置正確。現在我需要知道如何更新文本框內容。

@ManagedBean 
@SessionScoped 
public class HelloBean implements Serializable { 

private static final long serialVersionUID = 1L; 

private List<ExtendedSkin> mySkins; 
private List<SelectItem> mySkinsSI; 
private ExtendedSkin currentSkin; 

public void skinValueChanged(ValueChangeEvent e) { 
    currentSkin = (ExtendedSkin) e.getNewValue(); 
    FacesContext.getCurrentInstance().renderResponse(); 
} 

public List<ExtendedSkin> getMySkins() { 
    mySkins = XMLParser.readExtendedSkins(); 
    return mySkins; 
} 

public List<SelectItem> getMySkinsSI() { 
    mySkinsSI = new LinkedList<SelectItem>(); 
    for (ExtendedSkin s : getMySkins()) { 
     mySkinsSI.add(new SelectItem(s, s.getTitle())); 
    } 
    return mySkinsSI; 
} 

public void setMySkinsSI(List<SelectItem> myItems) { 
    this.mySkinsSI = myItems; 
} 

public ExtendedSkin getCurrentSkin() { 
    if (currentSkin == null) { 
     currentSkin = getMySkins().get(0); 
    } 
    return currentSkin; 
} 

public void setCurrentSkin(ExtendedSkin currentSkin) { 
    this.currentSkin = currentSkin; 
} 
} 
+0

嘗試將「」移出窗體外(作爲第一次嘗試)。這應該工作 – 2013-02-22 16:12:30

+0

Omg你做了thx :)發佈它的答案,所以我可以接受它:) – 4ndro1d 2013-02-22 16:32:01

+0

好吧,我會盡力解釋問題和可能的解決方案。我在評論中提供的只是一個。 – 2013-02-22 16:36:15

回答

7

這裏的問題是,該轉換器做它的工作填補了helloBean.currentSkin對象,但是是有界的這一helloBean.currentSkin<h:inputText>值:titletextColorbackgroundColorStart將被髮送到服務器並更換實際值由converter加載。換句話說:

  • 轉換器被執行並基於所選值建立helloBean.currentSkin
  • <h:inputText id="name">空值被髮送到服務器,並將被注入helloBean.currentSkin.title。其他2 <h:inputText> s的行爲相同。
  • 該視圖將使用所選的helloBean.currentSkin進行加載,並且它將使用空值加載helloBean.currentSkin.title。其他2 <h:inputText> s的行爲相同。

對於這個問題,有兩種可能的解決方案:

  1. 移動<h:inputText> S中的形式之外,因此空值將不會被髮送到服務器。加載視圖時,它將保持加載在轉換器中的值。

    <h:form> 
        <h:selectOneMenu id="dropdownSkin" 
         value="#{helloBean.currentSkin}" defaultLabel="Select a skin.." 
         valueChangeListener="#{helloBean.skinValueChanged}" immediate="true" 
         onchange="this.form.submit()" converter="SkinConverter" > 
         <f:selectItems value="#{helloBean.mySkinsSI}" var="c" 
          itemValue="#{c.value}" /> 
        </h:selectOneMenu> 
    </h:form> 
    <br /> 
    <h:inputText id="name" value="#{helloBean.currentSkin.title}"></h:inputText> 
    <!-- rest of Facelets code... --> 
    
  2. 既然你加載helloBean.currentSkin同時改變你的下拉列表選擇的值,你可以在一個更清潔的方式添加使用<h:selectOneMenu><f:ajax>標籤組成AJAX行爲和更新的領域。我會選擇這個解決方案。

    <h:form> 
        <!-- Note that there's no need of the onchange JavaScript function --> 
        <h:selectOneMenu id="dropdownSkin" 
         value="#{helloBean.currentSkin}" defaultLabel="Select a skin.." 
         valueChangeListener="#{helloBean.skinValueChanged}" immediate="true" 
         converter="SkinConverter" > 
         <f:selectItems value="#{helloBean.mySkinsSI}" var="c" 
          itemValue="#{c.value}" /> 
         <f:ajax process="@this" render="name tcolor bcolor" /> 
        </h:selectOneMenu> 
        <br /> 
        <h:inputText id="name" value="#{helloBean.currentSkin.title}" /> 
        <h:inputText id="tcolor" value="#{helloBean.currentSkin.titleBar.textColor}" /> 
        <br /> 
        <h:inputText id="bcolor" 
         value="#{helloBean.currentSkin.titleBar.backgroundColorStart}" /> 
    </h:form> 
    

您可以瞭解更多關於<f:ajax>在線教程像this one

由於您要在頁面中使用ajax調用,因此應該將託管bean範圍從@SessionScoped更改爲@ViewScoped。有關此處的更多信息,請執行以下操作:Communication in JSF 2

+0

這個答案幫助我解決了我的問題:http:// stackoverflow。COM /問題/ 40182423 /改變輸入型-ON-組合價值/ – user1156544 2016-10-22 15:43:18

相關問題