我正在尝试为应用程序进行部分渲染导航。下面的代码是一个概念测试,除了x.xhtml文件上的commandbutton之外,它运行良好。它不会点击actionListener。这用于更改包含部分的URL。
的index.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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">
<f:view contentType="text/html">
<h:head>
<f:facet name="first">
<meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
<title>PrimeFaces</title>
</f:facet>
<style type="text/css">
.ui-layout-north {
z-index:20 !important;
overflow:visible !important;;
}
.ui-layout-north .ui-layout-unit-content {
overflow:visible !important;
}
</style>
</h:head>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit position="north" size="100" header="Top" resizable="true" closable="true" collapsible="true">
<h:form>
<p:menubar>
<p:submenu label="File" icon="ui-icon-document">
<p:menuitem value="XXX" update=":wrapper" actionListener="#{tbean.doNav}">
<f:attribute name="xxx_page" value="x.xhtml" />
</p:menuitem>
<p:menuitem value="YYY" update=":wrapper" actionListener="#{tbean.doNav}">
<f:attribute name="xxx_page" value="y.xhtml" />
</p:menuitem>
</p:submenu>
</p:menubar>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="center">
<p:outputPanel id="wrapper">
<ui:include src="#{tbean.url}"/>
</p:outputPanel>
</p:layoutUnit>
</p:layout>
</h:body>
</f:view>
</html>
x.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:component 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"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:form>
<p:commandButton value="zzz" update=":wrapper" actionListener="#{tbean.doNav}">
<f:attribute name="xxx_page" value="z.xhtml" />
</p:commandButton>
<p:dataTable var="car" value="#{tbean.cars}">
<p:column>
<f:facet name="header">
Name
</f:facet>
<h:outputText value="#{car.name}" />
</p:column>
</p:dataTable>
</h:form>
</ui:component>
y.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:component 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"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:outputText value="yyy"/>
</ui:component>
z.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:component 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"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:outputText value="zzz"/>
</ui:component>
tbean.java
package com.teste;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;
@ManagedBean
@RequestScoped
public class tbean {
private String url = "y.xhtml";
private List<Car> cars = new ArrayList<>();
public tbean() {
for (int i = 0; i < 10; i++) {
cars.add(new Car(i));
}
}
public void setUrl(String url) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "setUrl :{0}", this.url);
this.url = url;
}
public String getUrl() {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "getUrl :{0}", this.url);
return this.url;
}
public void doNav(ActionEvent event) {
this.url = (String) event.getComponent().getAttributes().get("xxx_page");
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "doNav :{0}", this.url);
}
public List<Car> getCars() {
return cars;
}
}
答案 0 :(得分:1)
您的支持bean是请求作用域。这意味着它是在每个HTTP请求上创建的。因此url
属性在每个请求中都默认为y.xhtml
。
通过命令按钮提交表单会创建一个新的HTTP请求。因此,它获取了请求范围bean的新实例,url
属性默认为y.xhtml
。当JSF需要处理表单提交时,它无法按下按钮,因为它在y.xhtml
中不存在。因此,JSF无法调用与按下的按钮相关联的操作。
将bean放在视图范围内可以解决您的问题。
@ManagedBean
@ViewScoped
public class tbean {
这将在同一视图上的HTTP请求中正确记住url
属性(通过在每个操作上返回null
或void
)。
对于整个设计,你需要确保所有的ajax动作都是由PrimeFaces组件调用的,而不是由标准的JSF <f:ajax>
组件调用,否则命令按钮仍然不会被调用JSF JS中的错误。