该rendered
属性还在验证和更新模型值阶段进行评估。作为证据,请检查javax.faces.component.UIInput
源代码(行号根据 Mojarra 2.2.0):
696 public void processValidators(FacesContext context) {
697
698 if (context == null) {
699 throw new NullPointerException();
700 }
701
702 // Skip processing if our rendered flag is false
703 if (!isRendered()) {
704 return;
705 }
...
...
...
735 public void processUpdates(FacesContext context) {
736
737 if (context == null) {
738 throw new NullPointerException();
739 }
740
741 // Skip processing if our rendered flag is false
742 if (!isRendered()) {
743 return;
744 }
解释很简单:这是针对被篡改(欺骗/黑客攻击)HTTP 请求的保护措施,其中最终用户有目的地操纵 HTTP 请求以尝试设置值和/或调用隐藏输入/命令的操作,而这些操作很可能根本不允许更新或调用,例如仅当用户具有管理员角色时才显示的删除按钮:
<h:commandButton value="Delete" ... rendered="#{request.isUserInRole('ADMIN')}" />
注意:组件readonly
和disabled
属性也以这种方式处理。出于您的特定目的,请改用 CSS display: none
。
<h:inputText ... style="display:none" />
(注意:这是一个启动示例,style
在 HTML/CSS 透视图中使用属性是不好的做法,最好styleClass
使用具体的 CSS 文件)
尽管我想知道这背后的具体功能要求,但这对 UX 不利。也许您只是在没有首先研究JSF 规范更不用说JSF 源代码的情况下随意试验?