[英]JSF 2 : Why my first bean is called during an ajax action to a second bean?
我有兩個backbean,一個用於在ui:repeat中檢索數據,另一個用於執行操作。
呈現頁面時,如果我對第二個backbean執行一個動作,即使我對Richfaces 4使用ajax動作,第一個也將被調用(初始化)。如果不使用重復組件,情況就不是這樣。 令人討厭的是,第一個bean是用repeat元素調用的。
這是我的代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:head>
</h:head>
<h:body>
<h:form>
<ui:repeat var="currentValue" value="#{test_form_backBean_1.testSimpleModels}">
#{currentValue.name}
<br />
</ui:repeat>
<a4j:commandButton value="Tester" actionListener="#{test_form_backBean_2.test}" execute="@this" render="@this">
</a4j:commandButton>
</h:form>
</h:body>
</html>
我的第一個豆:
@Named("test_form_backBean_1")
@RequestScoped
public class Test_form_backBean_1 {
private static final Logger logger = Logger.getLogger(Test_form_backBean_1.class);
private List<Test_Simple_Model> testSimpleModels;
@PostConstruct
public void init() {
if (logger.isTraceEnabled())
logger.trace("Initialisation de Test_form_backBean_1.");
testSimpleModels = new ArrayList<Test_Simple_Model>();
testSimpleModels.add(new Test_Simple_Model(1L, "name_1"));
testSimpleModels.add(new Test_Simple_Model(2L, "name_2"));
testSimpleModels.add(new Test_Simple_Model(3L, "name_3"));
testSimpleModels.add(new Test_Simple_Model(4L, "name_4"));
}
public List<Test_Simple_Model> getTestSimpleModels() {
logger.trace("getTestSimpleModels() : appel.");
return testSimpleModels;
}
public void setTestSimpleModels(List<Test_Simple_Model> testSimpleModels) {
logger.trace("setTestSimpleModels() : appel.");
this.testSimpleModels = testSimpleModels;
}
}
第二 :
@Named("test_form_backBean_2")
@RequestScoped
public class Test_form_backBean_2 {
private static final Logger logger = Logger.getLogger(Test_form_backBean_2.class);
@PostConstruct
public void init() {
logger.trace("Initialisation de Test_form_backBean_2.");
}
public void test() {
logger.trace("test() : appel de la fonction de test.");
}
}
在此先感謝您的幫助。
問題是您的bean是請求范圍的,這意味着它們是在請求響應生命周期完成后就超出范圍的,這是非面孔請求(整頁)或AJAX回發請求的結果。
您需要改用視圖作用域的bean。 由於您正在使用CDI,因此您可以停止使用該CDI並切換到使用具有@ViewScoped
批注的JSF 2托管bean,或者可以繼續將CDI與Seam Faces 3一起使用,該產品顯然也支持@ViewScoped
。 (我不知道它的作用是什么,但顯然它能起作用。)
也請嘗試使用Java的通用命名約定,例如TestFormBackBean1
而不是Test_form_backBean_1
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.