JSF2.0 中UICommand 以及UIInput的一些注意事项
来源:互联网 发布:大数据相关专业 编辑:程序博客网 时间:2024/06/07 08:17
用了一段JSF一段时间,基本原理算是有点明白了,可是涉及到前台的一些细节真的很让人头疼,比如说不通浏览器行为之间的差异,不通Server之间的差异.最近碰到个问题,库是基于JSF2.0的,当点击一个form中的按钮时,必须要点击两次才得到响应,在网上找了很多相关的解决方案,可是都不是很有效.好不容易找了一篇博客,有位大神对此类问题做了总结,在此对此总结进行分享(原文:http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked/2120183#2120183)
Whenever an UICommand
component fails to invoke the associated action method or anUIInput
element fails to update the model value, then verify the following:
UICommand
andUIInput
components must be placed inside anUIForm
component, e.g.<h:form>
.You cannot nest multiple
UIForm
components in each other. This is namely illegal in HTML. Watch out with include files!No
UIInput
value validation/conversion error should have been occurred. You can use<h:messages>
to show any messages which are not shown by any input-specific<h:message>
components. Don't forget to include theid
of<h:messages>
in the<f:ajax render>
, if any, so that it will be updated as well on ajax requests.If
UICommand
orUIInput
components are placed inside an iterating component like<h:dataTable>
,<ui:repeat>
, etc, then you need to ensure that exactly the samevalue
of the component is been preserved during the apply request values phase of the form submit request. JSF will namely reiterate over it to find the clicked link/button and submitted input values. Putting the bean in the view scope and/or making sure that you load the data model in (post)constructor of the bean (and thus not in the getter method!) should fix it.The
rendered
attribute of the component and all of the parent components should not evaluate tofalse
during the apply request values phase of the form submit request. JSF will namely recheck it then as part of safeguard against tampered/hacked requests. Putting the bean in the view scope and/or making sure that you're preinitializing the condition in (post)constructor of the bean should fix it. The same applies to thedisabled
attribute of the component, which should not evaluate totrue
during processing the form submit.If you're using JSF 2.x
<f:ajax>
on the command component, make sure that you have a<h:head>
in the master template instead of the<head>
. Otherwise JSF won't be able to auto-include the necessaryjsf.js
JavaScript file which contains the Ajax functions. This would result in a JavaScript error like "mojarra is not defined" in the browser's builtin JavaScript console.If a parent of the
<h:form>
with theUICommand
button is been rendered/updated by an ajax request beforehand, then the first action will always fail. The second and subsequent actions will work. This is caused by a bug in view state handling which is reported as JSF spec issue 790 and fixed in JSF 2.2. For JSF 2.0 and 2.1 you need to explicitly specify the ID of the<h:form>
in therender
of the<f:ajax>
.If the
<h:form>
hasenctype="multipart/form-data"
set in order to support file uploading, then you need to make sure that the servlet filter who is responsible for parsing multipart/form-data requests is properly been configured, otherwise theFacesServlet
will end up getting no request parameters at all and thus not be able to apply the request values. How to configure such a filter depends on the file upload component being used. For Tomahawk<t:inputFileUpload>
, check this answer and for PrimeFaces<p:fileUpload>
, check this answer. Or, if you're actually not uploading a file at all, then remove the attribute altogether.Be sure that the
ActionEvent
argument ofactionListener
is anjavax.faces.event.ActionEvent
and thus notjava.awt.event.ActionEvent
, which is what most IDEs suggest as 1st autocomplete option.Be sure that no
PhaseListener
or anyEventListener
in the request-response chain has changed the JSF lifecycle to skip the invoke action phase by for example callingFacesContext#renderResponse()
orFacesContext#responseComplete()
.Be sure that no
Filter
orServlet
in the same request-response chain has blocked the request fo theFacesServlet
somehow.
My bet that your particular problem is caused by point 2: nested forms. You probably already have a<h:form>
in the parent page which wraps the include file. The include file itself shouldnot have a <h:form>
. You can also fix it the other way round, ensure that the parent page doesnot have a <h:form>
around the place of the include file.
- JSF2.0 中UICommand 以及UIInput的一些注意事项
- java集合框架中List的定义以及一些注意事项
- mybatis中mapper的用法以及一些注意事项
- JSF2.0 中 实现上传下载
- primefaces和jsf2的一些技巧
- JSF2 ManagedBean使用注意事项
- 关于JSF2中自定义组件处理表单值的一些问题
- JSF2.0的新特性
- 关于JSF2.0的Resources
- JSF2.0的新特性
- ToolBar的title居中以及一些注意事项
- 闭包的理解以及一些注意事项
- ToolBar的title居中以及一些注意事项
- ToolBar的title居中以及一些注意事项
- JSF2.0
- JSF2.0
- JSF2.0
- JSF2.0中Facelets将取代JSP
- android 中获取logcat缓冲区信息
- LLVM 介绍
- Portal-Basic Java Web 应用开发框架:应用篇(十二) —— Action Convention
- 使用类库需要导入的架包总结
- Struts2知识积累(2)_核心概念:拦截器
- JSF2.0 中UICommand 以及UIInput的一些注意事项
- Hibernate Core Reference Manual学习笔记——Chapter 1. Tutorial
- 【双旦献礼】Portal-Basic Java Web 应用开发框架 v3.0.1 正式发布(源码、示例及文档)
- java文档注释
- 网址的组成及json文件结构
- 《易道客》源码剖析之一:模块的目录结构分析
- 基于移动平均的图像分割
- java配置文件问题
- Hibernate 如何使用count(*)