我正在使用 JSF 2.0 和 Primefaces 3.2 开发应用程序,遇到以下问题: 如果使用 ajax 请求动态加载内容,则不会调用 ActionListener。
主要布局:
<div id="content">
<h:form name="content_form" id="content_form">
<ui:insert name="content" />
</h:form>
</div>
索引页面(内容根据菜单操作动态加载):
<ui:composition template="layout.xhtml">
<ui:define name="content">
<ui:include src="#{ajaxViewBean.viewName}" />
</ui:define>
</ui:composition>
菜单操作生成如下:
FacesContext context = FacesContext.getCurrentInstance();
MethodExpression methodExpression = context
.getApplication()
.getExpressionFactory()
.createMethodExpression(
context.getELContext(),
"#{ajaxViewBean.setViewName('" + navItem.getUrl()
+ "')}", null,
new Class[] { ActionEvent.class });
displayMenuItem.setUpdate(":content_form");
displayMenuItem
.addActionListener(new MethodExpressionActionListener(
methodExpression));
单击菜单项后,它会调用 AjaxViewBean“请求范围”来设置当前 View 页面并更新主布局中的“content_form”
包含的页面(其中操作监听器不起作用)
首次尝试 - 文件上传 (uploadFile.xhtml)
<h:body> <h:form enctype="multipart/form-data"> <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}" mode="advanced" update="messages" multiple="true" sizeLimit="100000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/> <p:growl id="messages" showDetail="true"/> </h:form>
fileUploadController 永远不会被调用。 如果我直接请求 uploadfile.xhtml,或者将其包含在第一次加载中,我可以上传该文件,这样我确信上传是工作文件,并且 web.xml 中的所有内容都已正确配置。
第二次尝试 - 包含页面中的菜单操作监听器
<h:body> <h:form> <p:menu type="tiered" style="width:180px"> <p:submenu label="Ajax Menuitems" icon="ui-icon-refresh"> <p:menuitem value="Save" actionListener="#{ajaxViewBean.setViewName()}" update="messages" icon="ui-icon-disk" /> <p:menuitem value="Update" actionListener="#{buttonBean.update}" update="messages" icon="ui-icon-arrowrefresh-1-w" /> </p:submenu> </p:menu> </h:form>
此外, Action 监听器中的事件永远不会触发,尽管它是在主页中触发的,并且是从上面提到的代码动态生成的
Maven 依赖项:
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
<version>1.1.14</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
如果我遗漏了什么,有人可以提供建议吗?或者我该如何解决这个问题?
实际上这是我在 Stackoverflow 上的第一篇文章,所以我希望我说清楚了并且格式没有搞乱
最佳答案
已修复!,我正在发布答案(也许对某人有用) 步骤如下:
- 确保此网址不存在违规行为:commandButton/commandLink/ajax action/listener method not invoked or input value not updated
按照以下 URL 使用部分状态保存方法为 false:http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html
<context-param> <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name> <param-value>false</param-value> </context-param>
需要强调的一点:
- 但是,全局禁用部分状态保存的缺点是 View 状态大小会增大。因此,如果您使用服务器端状态保存方法,那么服务器的内存使用量将会增加,或者如果您使用客户端状态保存方法,那么网络带宽使用量将会增加。基本上,您将获得与 JSF 1.x 中相同的 View 状态信息。如果可能,您还可以通过 web.xml 中的以下上下文参数禁用每个 View 的部分状态保存
关于java - JSF 2.0 - 使用 AJAX 动态加载时不调用 Primefaces ActionListeners,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10600597/