我有一个带有h:selectManyList
和Primefaces commandButton
的JSF页面。我想在单击按钮时将新元素添加到列表中。调用按钮的操作方法,但元素不会显示在列表中。我可能只是看不到树林。
页:
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head/>
<h:body>
<h:form id="form">
<h:messages id="errors"/>
<h:selectManyListbox id="listBox" value="#{testBean.availableThings}" style="width:100%">
<f:selectItems value="#{testBean.selectedThings}"/>
</h:selectManyListbox>
<br/>
<p:commandButton id="adder" value="Add" action="#{testBean.addThing}"
ajax="true" update="listBox" process="@this listBox"/>
</h:form>
</h:body>
</html>
支持bean:
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
@javax.faces.bean.ManagedBean
@javax.faces.bean.ViewScoped
@com.ocpsoft.pretty.faces.annotation.URLMapping(
id = "testbean",
pattern = "/testbean/",
viewId = "/pages/general/testbean.xhtml")
public class TestBean {
private List<SelectItem> availableThings;
private List<String> selectedThings;
public TestBean() {
availableThings = new ArrayList<>();
selectedThings = new ArrayList<>();
}
public List<SelectItem> getAvailableThings() {
return availableThings;
}
public void setAvailableThings(List<SelectItem> list) {
this.availableThings = list;
}
public List<String> getSelectedThings() {
return selectedThings;
}
public void setSelectedThings(List<String> list) {
this.selectedThings = list;
}
public void addThing() {
availableThings.add(new SelectItem("item", "item")); // I get this message
System.err.println("Added item");
}
}
为什么添加的项目不会出现在列表中,我需要做些什么才能使其显示?
答案 0 :(得分:2)
你颠倒了xhtml中的字段。
REST_FRAMEWORK = {
# Add custom renderer.
'DEFAULT_RENDERER_CLASSES'=(
'path.to.CustomJSONRenderer',
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer'),
# Add cutom exception handler
'EXCEPTION_HANDLER': 'path.to.custom_exception_handler'
}
应该是
<h:selectManyListbox id="listBox" value="#{testBean.availableThings}" style="width:100%">
<f:selectItems value="#{testBean.selectedThings}"/>
</h:selectManyListbox>