我试图从selectCheckboxMenu中获取所选值,但我在控制台中获取的所有值都为null。它也不适用于selectOneMenu。这是我的jsf表格:
<h:form id="mmaster">
<p:dataTable
value="#{devicesBean.devices}"
var="dev"
widgetVar="dt"
border="1"
paginator="true"
paginatorPosition="top"
rows="10"
>
<f:facet name="header">Devices</f:facet>
<p:column headerText="UDN" sortBy="#{dev.deviceUDN}" filterBy="#{dev.deviceUDN}" filterMatchMode="contains" emptyMessage="No Devices Found">
<h:outputText value="#{dev.deviceUDN}" />
</p:column>
<p:column headerText="FriendlyName" sortBy="#{dev.deviceFriendlyName}" filterBy="#{dev.deviceFriendlyName}" filterMatchMode="contains">
<h:outputText value="#{dev.deviceFriendlyName}" />
</p:column>
<p:column headerText="Model" sortBy="#{dev.deviceModel}" filterBy="#{dev.deviceModel}" filterMatchMode="contains">
<h:outputText value="#{dev.deviceModel}" />
</p:column>
<p:column headerText="Manufacturer" sortBy="#{dev.deviceManufacturer}" filterBy="#{dev.deviceManufacturer}" filterMatchMode="contains">
<h:outputText value="#{dev.deviceManufacturer}" />
</p:column>
<p:column headerText="Type" sortBy="#{dev.deviceType}" filterBy="#{dev.deviceType}" filterMatchMode="contains">
<h:outputText value="#{dev.deviceType}" />
</p:column>
<p:column headerText="Actions">
<p:selectCheckboxMenu value="#{devicesBean.selectAnnotations}">
<f:selectItems value="#{devicesBean.annotations}" />
</p:selectCheckboxMenu>
</p:column>
<p:column>
<p:commandButton value="Annotate" action="#{devicesBean.doSave}" process="@this">
<f:setPropertyActionListener value="#{dev}" target="#{devicesBean.device}" />
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
我想知道bean的范围是否存在问题,这是我的托管bean:
@ManagedBean
public class DevicesBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<Device> devices;
private List<String> annotations;
private List<String> selectAnnotations = new ArrayList<String>();
private Device device;
@EJB
IOntoProcessor iop;
@EJB
IDevicesDao idd;
public DevicesBean() {
}
@PostConstruct
public void init() {
setDevices(idd.getAllDevices());
setAnnotations(iop.getAllAnnotations());
}
public List<Device> getDevices() {
return devices;
}
public void setDevices(List<Device> devices) {
this.devices = devices;
}
public List<String> getAnnotations() {
return annotations;
}
public void setAnnotations(List<String> annotations) {
this.annotations = annotations;
}
public Device getDevice() {
return device;
}
public void setDevice(Device device) {
this.device = device;
}
public List<String> getSelectAnnotations() {
return selectAnnotations;
}
public void setSelectAnnotations(List<String> selectAnnotations) {
this.selectAnnotations = selectAnnotations;
}
public void doSave() {
System.out.println(selectAnnotations);
System.out.println(device);
selectAnnotations = new ArrayList<String>();
}
}
答案 0 :(得分:1)
您正尝试通过带有值Annotate的Button提交表单,该指定已指定为仅处理自身:
这只会处理按钮及其关联的表单参数,而不会处理表单中的其他元素。
<p:commandButton value="Annotate" action="#{devicesBean.doSave}" process="@this">
<f:setPropertyActionListener value="#{dev}" target="#{devicesBean.device}" />
</p:commandButton>
删除process="@this"
,或将其替换为process="@form"
<p:commandButton value="Annotate" action="#{devicesBean.doSave}" process="@form">
<f:setPropertyActionListener value="#{dev}" target="#{devicesBean.device}" />
</p:commandButton>
两,声明您的托管bean范围:@RequestScope
或@SessionScoped
都可以正常工作。