JSF2.1 在带有 Facelets 或不带 Facelets 的页面中使用不同行为的 Ajax 侦听器
如果在 een .xhtml 中使用不带 Facelets 模板的支持 ajax 的 commandButton,则 ajax 侦听器必须是 commandButton 标记的属性。如果与模板结合使用,侦听器必须是 ajax 标记的属性。
我需要一些时间来解决这个问题,所以如果这是正确的并且是正常行为,请注意。
viewscoped bean 的
@ManagedBean
@ViewScoped
public class Test implements Serializable {
private static final long serialVersionUID = 123456L;
private static int i = 0;
private int counter;
private String test = "test ";
@PostConstruct
public void test() {
System.out.println(".......... PostConstruct");
i++;
}
public String getTest() {
return test + i;
}
public String action() {
counter++;
System.out.println(".......... action() " + counter);
return "";
}
public void listener() {
counter++;
System.out.println(".......... listener() " + counter);
}
public void ajaxListener(AjaxBehaviorEvent actionEvent) {
System.out.println(".......... ajaxListener() - " +
"AjaxBehaviorEvent: " + actionEvent);
}
public void ajax() {
System.out.println(".......... ajax() - not using Facelets " +
counter);
}
public void ajaxList2(ActionEvent actionEvent) {
System.out.println(".......... ajaxList2() - ActionEvent" +
actionEvent);
}
public int getCounter() {
return counter;
}
}
前 2 个按钮在两个页面中都有效(使用和不使用 Facelets)。其他按钮看不到按钮值属性。
使用facelets的.xhtml
<ui:composition 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.prime.com.tr/ui"
template="/WEB-INF/templates/test.xhtml">
<ui:define name="ui_content">
#{test.test}
<h:form id="frm">
<h:commandButton value="testAction" action="#{test.action}"/>
<h:commandButton value="testListener" actionListener="#{test.listener}"/>
<h:commandButton value="Ajax - works ONLY using Facelets" immediate="true" >
<f:ajax event="click" execute="@form" render=":grp2" listener="#{test.ajaxListener}" />
</h:commandButton>
<h:commandButton value="testAjax - works ONLY without using Facelets" immediate="true" action="#{test.ajax}" >
<f:ajax event="click" execute="@form" render=":grp2"/>
</h:commandButton>
<h:commandButton value="testAjax - actionListener works ONLY without using Facelets"
immediate="true" actionListener="#{test.ajaxList2}" >
<f:ajax event="click" execute="@form" render=":grp2"/>
</h:commandButton>
</h:form>
<h:panelGroup id="grp1" rendered="#{test.counter > 2}">
1st group: #{test.counter}
</h:panelGroup><br />
<h:panelGroup id="grp2" rendered="#{test.counter > 7}">
2de group: #{test.counter}
</h:panelGroup>
不带facelets的.xhtml
<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:p="http://primefaces.prime.com.tr/ui">
#{test.test}
<h:form id="frm">
<h:commandButton value="testAction" action="#{test.action}"/>
<h:commandButton value="testListener" actionListener="#{test.listener}"/>
<h:commandButton value="Ajax - works ONLY using Facelets" immediate="true" >
<f:ajax event="click" execute="@form" render=":grp2" listener="#{test.ajaxListener}" />
</h:commandButton>
<h:commandButton value="testAjax - action works ONLY without using Facelets" immediate="true" action="#{test.ajax}" >
<f:ajax event="click" execute="@form" render=":grp2"/>
</h:commandButton>
<h:commandButton value="testAjax - actionListener works ONLY without using Facelets" immediate="true" actionListener="#{test.ajaxList2}" >
<f:ajax event="click" execute="@form" render=":grp2"/>
</h:commandButton>
</h:form>
<h:panelGroup id="grp1" rendered="#{test.counter > 2}">
1st group: #{test.counter}
</h:panelGroup><br />
<h:panelGroup id="grp2" rendered="#{test.counter > 7}">
2nd group: #{test.counter}
</h:panelGroup>
</html>
模板文件
<?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 xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>test</title>
</h:head>
<h:body>
<f:view>
<div id="page">
<ui:insert name="ui_header">header</ui:insert>
<ui:insert name="ui_subnav"/>
<div id="content">
<ui:insert name="ui_content"/>
</div>
<ui:insert name="ui_footer" />
</div>
</f:view>
</h:body>
</html>
If a commandButton with ajax support used in een .xhtml without facelets templates, the ajax listener has to be an attribute of the commandButton-tag. If used in conjunction with a template, the listener has to be an attribute of the ajax-tag.
I needed some time to figure this out so if this is correct and a normal behavior, be warned.
The viewscoped bean
@ManagedBean
@ViewScoped
public class Test implements Serializable {
private static final long serialVersionUID = 123456L;
private static int i = 0;
private int counter;
private String test = "test ";
@PostConstruct
public void test() {
System.out.println(".......... PostConstruct");
i++;
}
public String getTest() {
return test + i;
}
public String action() {
counter++;
System.out.println(".......... action() " + counter);
return "";
}
public void listener() {
counter++;
System.out.println(".......... listener() " + counter);
}
public void ajaxListener(AjaxBehaviorEvent actionEvent) {
System.out.println(".......... ajaxListener() - " +
"AjaxBehaviorEvent: " + actionEvent);
}
public void ajax() {
System.out.println(".......... ajax() - not using Facelets " +
counter);
}
public void ajaxList2(ActionEvent actionEvent) {
System.out.println(".......... ajaxList2() - ActionEvent" +
actionEvent);
}
public int getCounter() {
return counter;
}
}
the first 2 buttons work in both pages (with and without using facelets). The other buttons don't see the buttons value attribute.
the .xhtml using facelets
<ui:composition 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.prime.com.tr/ui"
template="/WEB-INF/templates/test.xhtml">
<ui:define name="ui_content">
#{test.test}
<h:form id="frm">
<h:commandButton value="testAction" action="#{test.action}"/>
<h:commandButton value="testListener" actionListener="#{test.listener}"/>
<h:commandButton value="Ajax - works ONLY using Facelets" immediate="true" >
<f:ajax event="click" execute="@form" render=":grp2" listener="#{test.ajaxListener}" />
</h:commandButton>
<h:commandButton value="testAjax - works ONLY without using Facelets" immediate="true" action="#{test.ajax}" >
<f:ajax event="click" execute="@form" render=":grp2"/>
</h:commandButton>
<h:commandButton value="testAjax - actionListener works ONLY without using Facelets"
immediate="true" actionListener="#{test.ajaxList2}" >
<f:ajax event="click" execute="@form" render=":grp2"/>
</h:commandButton>
</h:form>
<h:panelGroup id="grp1" rendered="#{test.counter > 2}">
1st group: #{test.counter}
</h:panelGroup><br />
<h:panelGroup id="grp2" rendered="#{test.counter > 7}">
2de group: #{test.counter}
</h:panelGroup>
the .xhtml without facelets
<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:p="http://primefaces.prime.com.tr/ui">
#{test.test}
<h:form id="frm">
<h:commandButton value="testAction" action="#{test.action}"/>
<h:commandButton value="testListener" actionListener="#{test.listener}"/>
<h:commandButton value="Ajax - works ONLY using Facelets" immediate="true" >
<f:ajax event="click" execute="@form" render=":grp2" listener="#{test.ajaxListener}" />
</h:commandButton>
<h:commandButton value="testAjax - action works ONLY without using Facelets" immediate="true" action="#{test.ajax}" >
<f:ajax event="click" execute="@form" render=":grp2"/>
</h:commandButton>
<h:commandButton value="testAjax - actionListener works ONLY without using Facelets" immediate="true" actionListener="#{test.ajaxList2}" >
<f:ajax event="click" execute="@form" render=":grp2"/>
</h:commandButton>
</h:form>
<h:panelGroup id="grp1" rendered="#{test.counter > 2}">
1st group: #{test.counter}
</h:panelGroup><br />
<h:panelGroup id="grp2" rendered="#{test.counter > 7}">
2nd group: #{test.counter}
</h:panelGroup>
</html>
the template file
<?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 xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>test</title>
</h:head>
<h:body>
<f:view>
<div id="page">
<ui:insert name="ui_header">header</ui:insert>
<ui:insert name="ui_subnav"/>
<div id="content">
<ui:insert name="ui_content"/>
</div>
<ui:insert name="ui_footer" />
</div>
</f:view>
</h:body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论