我有下一种情况。我试图在干净的项目中从项目中重新创建错误。
我有一个带有2个链接的简单索引页面
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/templates/template.xhtml">
<ui:define name="content">
<h:form id="mainForm" enctype="multipart/form-data">
<p:messages id="messages" showDetail="true" closable="true" autoUpdate="true">
</p:messages>
<div class="ui-fluid" jsf:id="itemForm">
<p:commandLink actionListener="#{workRequestController.confirmWorkRequest}" update=":mainForm:itemForm">Send Parameter </p:commandLink>
<br/>
<br/>
<p:link href="main.xhtml">LIST</p:link>
</div>
</h:form>
</ui:define>
第一个链接通过id参数= 1重定向到main.xhtml,因此:
@Named(value = "workRequestController")
@ViewScoped
public class WorkRequestController implements Serializable {
public void confirmWorkRequest() throws IOException {
Integer id = 1;
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
context.redirect(context.getRequestContextPath() + "/main.xhtml?id=" + id);
}
}
第二个链接无需参数即可直接重定向到主页,该主页的定义如下:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/templates/template.xhtml">
<ui:define name="scripts">
<h:outputScript library="js" name="script.js"></h:outputScript>
</ui:define>
<ui:define name="metadata">
<f:metadata>
<f:viewParam name="id" value="#{beanController.workOrderId}"/>
<f:viewAction action="#{beanController.resolveRequestParams}" phase="UPDATE_MODEL_VALUES" />
</f:metadata>
</ui:define>
<ui:define name="content">
<h:form id="mainForm" enctype="multipart/form-data">
<p:messages id="messages" showDetail="true" closable="true" autoUpdate="true">
</p:messages>
<div class="ui-fluid" jsf:id="itemForm">
<ui:include src="#{beanController.currentForm}" />
</div>
</h:form>
</ui:define>
</ui:composition>
如果您看到为主页定义了元数据标签,以便在通过URL发送参数时捕获该参数。主页可以基于currentForm变量加载2个页面(List.xhtml或View.xhtml)。默认情况下为“ List.xhtml”,当从index.xhtml发送参数ID时,变量currentForm更改为View.xhtml。这样定义了bean控制器:
@Named(value = "beanController")
@ViewScoped
public class BeanController implements Serializable {
private String workOrderId;
protected String currentForm = "List.xhtml";
private static List<Model> models;
private Model model;
private List<Client> clients;
private Client client;
//Initialization model
static {
models = new ArrayList<Model>();
Model model1 = new Model(0, "Model 1", "Description 1");
Model model2 = new Model(1, "Model 2", "Description 2");
Model model3 = new Model(2, "Model 3", "Description 3");
Model model4 = new Model(3, "Model 4", "Description 4");
Model model5 = new Model(4, "Model 5", "Description 5");
Model model6 = new Model(5, "Model 6", "Description 6");
Model model7 = new Model(6, "Model 7", "Description 7");
Model model8 = new Model(7, "Model 8", "Description 8");
models.add(model1);
models.add(model2);
models.add(model3);
models.add(model4);
models.add(model5);
models.add(model6);
models.add(model7);
models.add(model8);
}
public void resolveRequestParams() {
if(workOrderId != null){
Integer workId = Integer.valueOf(workOrderId);
model = models.get(workId);
currentForm = "View.xhtml";
} else {
currentForm = "List.xhtml";
}
}
public String getWorkOrderId() {
return workOrderId;
}
public void setWorkOrderId(String workOrderId) {
this.workOrderId = workOrderId;
}
public String getCurrentForm() {
return currentForm;
}
public void setCurrentForm(String currentForm) {
this.currentForm = currentForm;
}
public List<Model> getModels() {
return models;
}
public void setModels(List<Model> models) {
this.models = models;
}
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
public List<Client> getClients() {
clients = new ArrayList<Client>();
Client client1 = new Client(1, "Charles");
Client client2 = new Client(2, "Michael");
Client client3 = new Client(3, "Kevin");
Client client4 = new Client(4, "Jason");
clients.add(client1);
clients.add(client2);
clients.add(client3);
clients.add(client4);
return clients;
}
public void setClients(List<Client> clients) {
this.clients = clients;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public void onSelectClient() {
model.setClient(client);
}
public void prepareView(){
if(model == null){
currentForm = "List.xhtml";
}
else {
currentForm = "View.xhtml";
}
}
public void close(){
model = null;
currentForm = "List.xhtml";
}
}
因此定义了List.xhtml:
<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns="http://www.w3.org/1999/xhtml">
<p:commandLink actionListener="#{beanController.prepareView}" update=":mainForm:itemForm">View
</p:commandLink>
<br/>
<br/>
<p:dataTable var="model" value="#{beanController.models}" selection="#{beanController.model}" rowKey="#{model.id}">
<p:column selectionMode="single" style="width: 42px"
styleClass="align-center"
priority="1"/>
<p:column headerText="Id">
<h:outputText value="#{model.id}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{model.name}" />
</p:column>
<p:column headerText="Description">
<h:outputText value="#{model.description}" />
</p:column>
</p:dataTable>
</ui:composition>
和View.xhtml这样:
<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<p:commandLink onclick="PF('ClientList').show();" value="Open Dialog"/>
<br/>
<br/>
<p:commandLink actionListener="#{beanController.close}" value="Close" update=":mainForm:itemForm"/>
<br/>
<br/>
<p:inputText id="id" value="#{beanController.model.id}" disabled="true"/>
<p:inputText id="name" value="#{beanController.model.name}" disabled="true"/>
<p:inputText id="description" value="#{beanController.model.description}" disabled="true"/>
<p:inputText id="client" value="#{beanController.model.client.name}" disabled="true"/>
<p:dialog id="ClientListDialog"
modal="true"
widgetVar="ClientList"
resizable="false"
closable="true"
appendTo="@(body)"
closeOnEscape="true">
<h:form>
<p:dataTable id="ClientTable"
class="fit-content-table"
binding="#{clients}"
value="#{beanController.clients}"
var="item"
rowKey="#{item.id}"
paginator="true"
paginatorAlwaysVisible="true"
paginatorPosition="top"
selection="#{beanController.client}"
selectionMode="single"
widgetVar="ClientTable">
<p:ajax event="rowSelect" listener="#{beanController.onSelectClient}"
onstart="PF('ClientList').hide();"
update=":mainForm:client"/>
<p:column>
<f:facet name="header">
<h:outputText value=""/>
</f:facet>
<h:outputText value=""/>
</p:column>
<p:column sortBy="#{item.name}" filterBy="#{item.id}">
<f:facet name="header">
<h:outputText value="Id"/>
</f:facet>
<h:outputText value="#{item.id}"/>
</p:column>
<p:column sortBy="#{item.name}" filterBy="#{item.name}">
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</p:column>
</p:dataTable>
</h:form>
</p:dialog>
</ui:composition>
问题发生在从索引页面发送参数的瞬间加载View页面时,在这种情况下,是在View页面中加载了模型,但是当我为对话框中显示的表选择行时,不会为event =“ rowSelect”执行侦听器beanController.onSelectClient,而如果从“列表”页面加载了“查看”页面却没有通过URL发送参数,则侦听器将起作用。