我正在接近PrimeFaces中的JSF绑定参数。
这是我的表格:
<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:p="http://primefaces.org/ui">
<h:head>
<h1 class="title ui-widget-header ui-corner-all">
<p:spacer width="100" height="10" />
PrimeFaces Test Binding
</h1>
<title><ui:insert name="title">PrimeFaces Test</ui:insert></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>
<h:body>
<h:form id="form1">
<h:panelGroup binding="#{bindingTestClass.panelGroup}" />
</h:form>
</h:body>
</html>
这就是身体:
@ManagedBean(name="bindingTestClass")
@ViewScoped
public class BindingTestClass implements Serializable{
transient HtmlOutputLabel testValue = null;
transient HtmlSelectOneMenu menu = null;
transient HtmlPanelGroup panelGroup = null;
@PostConstruct
private void makeUp(){
menu = new HtmlSelectOneMenu();
panelGroup = new HtmlPanelGroup();
testValue = new HtmlOutputLabel();
panelGroup.setId("B");
panelGroup.setLayout("block");
panelGroup.setStyleClass("grid-form");
//LINE 0
testValue.setId("A");
testValue.setValue("BasicLabel");
panelGroup.getChildren().add(testValue);
HtmlOutputText linebreak = new HtmlOutputText();
linebreak.setValue("<br/>");
linebreak.setEscape(false);
panelGroup.getChildren().add(linebreak);
menu.setId("F");
// populate the drop down list
UISelectItems items = new UISelectItems();
List comboList = new ArrayList();
comboList.add(new SelectItem("---"));
for(int a = 0; a <10; a++){
comboList.add(new SelectItem( a+1 + " test"));
}
items.setId("ss");
items.setValue(comboList);
menu.getChildren().add(items);
//Add list to combobox
/*this first attempt doesn't work either, only change backend values
menu.addValueChangeListener(new ValueListenerTest());
menu.setOnchange("submit()");
*/
AjaxBehavior ajaxBehavior = (AjaxBehavior) FacesContext.getCurrentInstance().getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
ajaxBehavior.addAjaxBehaviorListener(new CustomAjaxListener());
//ajaxBehavior.setTransient(true);
ajaxBehavior.setUpdate("form1");
menu.addClientBehavior("change",ajaxBehavior);
panelGroup.getChildren().add(menu);
}
//Getters,Setters
public HtmlPanelGroup getPanelGroup() {
return panelGroup;
}
public HtmlOutputLabel getTestValue() {
return testValue;
}
public void setTestValue(HtmlOutputLabel testValue) {
this.testValue = testValue;
}
public void setPanelGroup(HtmlPanelGroup panelGroup) {
this.panelGroup = panelGroup;
}
public HtmlSelectOneMenu getMenu() {
return menu;
}
public void setMenu(HtmlSelectOneMenu menu) {
this.menu = menu;
}
}
事实是,当且仅当我以这种方式将单独的绑定代码放在我的视图中时:
<h:outputLabel binding="#{bindingTestClass.testValue}" />
显然从panelgroup中取出相应的子节点,当我更改selectOneMenu值时,侦听器更改了后备bean中的值,然后更新了确实更新“testValue”标签值的表单。
我尝试了不同的尝试,但仍然没有理解为什么它不能以这种方式工作,将所有内容放在一个小组中。
有人能指出我正确的方向吗?
许多人提前感谢!
编辑:上面的代码适用于@RequestScoped范围,显然将所有属性变为“私有”修饰符模式。但是@ViewScoped没有机会了吗?