2010-03-23 49 views
1

我有這樣的形式:JSF2 - f:ajax元素的範圍是什麼?

<h:form> 
    <h:outputText value="Tag:" /> 
    <h:inputText value="#{entryRecorder.tag}"> 
     <f:ajax render="category" /> 
    </h:inputText> 
    <h:outputText value="Category:" /> 
    <h:inputText value="#{entryRecorder.category}" id="category" /> 
</h:form> 

我想要實現:當您在「標籤」字段中鍵入時,entryRecorder.tag場用什麼類型的更新。通過這個動作的一些邏輯,這個bean也更新它的category字段。這種變化應該體現在形式上。

問題:

  1. 我將用什麼餘地EntryRecorder?對於多個AJAX請求,請求可能不盡如人意,而會話將無法在每個會話中使用多個瀏覽器窗口。
  2. 如何在EntryRecorder中註冊我的updateCategory()動作,以便在更新bean時觸發它?
+0

我會使用'ViewScoped',但我非常希望看到有人對第一點有更多的瞭解。 – Romain 2010-03-23 19:09:38

回答

0

應答點2:

<h:inputText styleClass="id_tag" value="#{entryRecorder.tag}" 
    valueChangeListener="#{entryRecorder.tagUpdated}"> 
    <f:ajax render="category" event="blur" /> 
</h:inputText> 

豆:

@ManagedBean 
@ViewScoped 
public class EntryRecorder { 
    private String tag; 
    private String category; 
    @EJB 
    private ExpenseService expenseService; 

    public void tagUpdated(ValueChangeEvent e) { 
     String value = (String) e.getNewValue(); 
     setCategory(expenseService.getCategory(value)); 
    } 
} 

1號,任何人?

0

要點1,我會使用請求,因爲沒有必要使用視圖和會話,正如你所指出的,完全沒有必要。

對於第2點,由於您使用的是< f:ajax/>我建議充分利用它。這裏是我的建議:

XHTML:

<h:form> 
    <h:outputText value="Tag:" /> 
    <h:inputText value="#{entryRecorder.tag}"> 
     <f:ajax render="category" event="valueChange"/> 
    </h:inputText> 
    <h:outputText value="Category:" /> 
    <h:inputText value="#{entryRecorder.category}" id="category" /> 
</h:form> 

的使用注意事項valueChange事件,而不是模糊的(不是模糊不工作,但我找了價值架組件valueChange更多的「正確」)。

豆:

@ManagedBean 
@RequestScoped 
public class EntryRecorder { 
    private String tag; 
    private String category; 

    public String getCategory() { 
     return category; 
    } 

    public String getTag() { 
     return tag; 
    } 

    public void setCategory(String category) { 
     this.category = category; 
    } 

    public void setTag(String tag) { 
     this.tag = tag; 
     tagUpdated(); 
    } 

    private void tagUpdated() { 
     category = tag; 
    } 
} 

除非你真的想tagUpdated方法執行,只有當標籤通過視圖更新,我的建議看起來更清晰。您不必處理事件(也不是投射),並且tagUpdated方法可以隱藏它的功能,避免可能的誤用。

+0

如果這個bean出於某種原因很重,請求範圍方法似乎並不是最優的。 – 2010-04-08 18:48:53

+0

「如果這個bean出於某種原因很重,請求範圍方法似乎不是最佳的。」 - 當然。 如果你有一個非常沉重的bean,你應該考慮將它分裂,甚至把標籤類型的輸入封裝到一個自定義組件中,而不是僅僅因爲它使你的應用程序運行更平滑就更改bean的範圍(你是否運行過一些性能測試?)而不是因爲它是有道理的。 – 2010-04-12 05:18:51