使用<;f:视图>;在JSF中的模板页面上
我有一个数据表(PrimeFaces3.5,JSF2.0),它是从数据库填充的。在该表的第一列中,将显示复选框(多行选择) 选择行后,当按下按钮(使用<;f:视图>;在JSF中的模板页面上,jsf,jsf-2,primefaces,Jsf,Jsf 2,Primefaces,我有一个数据表(PrimeFaces3.5,JSF2.0),它是从数据库填充的。在该表的第一列中,将显示复选框(多行选择) 选择行后,当按下按钮()时,所选行将从数据库中删除 删除行之前,中会显示一条关于删除所选行的确认消息,其中有两个按钮“是”和“否”,如下所示 Test.xhtml: <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona
)时,所选行将从数据库中删除
删除行之前,
中会显示一条关于删除所选行的确认消息,其中有两个按钮“是”和“否”,如下所示
Test.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition template="template/Template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:define name="title">Test</ui:define>
<ui:define name="content">
<h:form id="form">
<p:dataTable id="dataTable" var="row" value="#{testManagedBean.list}"
selection="#{testManagedBean.selectedValues}"
rowKey="#{row.id}"
rowIndexVar="rowIndex">
<p:column selectionMode="multiple" style="width:5%; text-align: center;">
<f:facet name="footer">
----------------> <p:commandButton actionListener="#{testManagedBean.deleteMultipleActionListener}" oncomplete="confirmDeleteMultiple.show()" update=":form:confirmDialogDeleteMultiple" process=":form:dataTable" icon="ui-icon ui-icon-close"/>
</f:facet>
</p:column>
<p:column headerText="Index">
<h:outputText value="#{rowIndex+1}"/>
</p:column>
<p:column id="id" headerText="Id">
<h:outputText value="#{row.id}"/>
</p:column>
<p:column id="countryName" headerText="Description">
<h:outputText value="#{row.description}"/>
</p:column>
</p:dataTable>
---------------><p:confirmDialog id="confirmDialogDeleteMultiple" widgetVar="confirmDeleteMultiple" appendToBody="true" message="Delete row(s)?" showEffect="true" hideEffect="true" header="Deletion of row." severity="alert" closeOnEscape="true" closable="true">
<p:commandButton id="confirmDeleteMultiple" value="Yes" oncomplete="confirmDeleteMultiple.hide()" actionListener="#{testManagedBean.deleteMultiple}" process="@this dataTable" update="dataTable"/>
<p:commandButton id="declineDeleteMultiple" value="No" onclick="confirmDeleteMultiple.hide()" type="button" />
</p:confirmDialog>
</h:form>
</ui:define>
</ui:composition>
当我从上面的Test.xhtml
页面中删除此模板并在Test.xhtml
页面本身上使用此
标记时,一切正常,并且deleteMultiple()
方法中的选定行列表正确获得
那么,出了什么问题?如果模板页面由
括起,为什么按下“是”按钮时列表变为空?错了吗?如何做到这一点?(
用于应用程序的本地化,这是可以想象的)。当您用过程=“@this”
替换中的过程=“@this datatable”
时,您的问题就解决了,这使得按钮组件只能被处理。事实上,在发送删除确认时,您不需要Ajax来处理数据表,因为从上一步开始,删除的值已经存储在bean中(这是视图范围的主要优点)
在Ajax请求中处理数据表将再次调用setSelectedValues
setter,因此原始值将被新的(空)选择覆盖。这是正确的。非常感谢你。虽然我可以知道为什么只有当涉及到
时才会发生这种情况吗?数据表
就找不到了,完全被忽略了。只需将
保留在主模板中。顺便说一句,模板客户机中的
放错了位置,EJB上的瞬态
发出警报。把两者都去掉。我不懂EJB。我还忘了提到,正如@BalusC所说,您只需要位于模板中的h:head
。出于测试目的,我对模板客户端的一个进行了注释。你的意思是@BalusC,当f:view
放在模板客户机datatable
中时,无法找到它,因此它可以工作吗?
@ManagedBean
@ViewScoped
public final class TestManagedBean implements Serializable
{
@EJB(mappedName="ejb/JNDI")
private TestService testService;
private List<Test> list;
private List<Test>selectedValues;
public TestManagedBean(){}
@PostConstruct
public void init()
{
list=testService.getList();
}
public List<Test> getList() {
return list;
}
public List<Test> getSelectedValues() {
return selectedValues;
}
public void setSelectedValues(List<Test> selectedValues) {
this.selectedValues = selectedValues;
}
public void deleteMultipleActionListener(ActionEvent actionEvent)
{
//Just show a warning message, when the delete button is pressed.
for(Test test:selectedValues)
{
System.out.println(test.getId()+" : "+test.getDescription());
}//Displays the list.
}
public void deleteMultiple(ActionEvent actionEvent)
{
System.out.println("multiple");
for(Test test:selectedValues)
{
System.out.println(test.getId()+" : "+test.getDescription());
}//The list is not null and empty.
}
}
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="#{localeBean.language}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
--------->
<f:view locale="#{localeBean.locale}" encoding="UTF-8" contentType="text/html">
<f:loadBundle basename="messages.ResourceBundle" var="messages"/>
<h:head><title><ui:insert name="title">Default Title</ui:insert></title></h:head>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit position="north" size="135" collapsed="false" resizable="false" closable="false" collapsible="false" gutter="6">
<h:form>
<h:selectOneMenu id="languages" value="#{localeBean.language}" onchange="submit();" style="position: absolute; right: 0; top: 50px;">
<f:selectItem itemValue="en" itemLabel="English" />
<f:selectItem itemValue="hi" itemLabel="Hindi" />
</h:selectOneMenu>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="west" id="leftPanel" size="225" header="Menu Item" resizable="false" closable="false" collapsible="true" gutter="6">
</p:layoutUnit>
<p:layoutUnit position="center" size="2500" maxSize="2500">
<ui:insert name="content">Put default content here, if any.</ui:insert>
</p:layoutUnit>
</p:layout>
</h:body>
</f:view>
</html>