2

我对在 facelet 中解析 html 标签有一些疑问。让我们有包含以下内容的 facelet

<h:inputText id="username"
             title="My name is: "
             value="#{hello.name}"
             required="true"
             requiredMessage="Error: A name is required."
             rendered="false"
             maxlength="25" />
<h:commandButton id="submit" value="Submit" action="response">
            </h:commandButton>

提交点击后我没有Error: A name is required。为什么?username只是没有渲染,submit点击后没有任何价值username

4

1 回答 1

3

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')}" />

注意:组件readonlydisabled属性也以这种方式处理。出于您的特定目的,请改用 CSS display: none

<h:inputText ... style="display:none" />

(注意:这是一个启动示例,style在 HTML/CSS 透视图中使用属性是不好的做法,最好styleClass使用具体的 CSS 文件)

尽管我想知道这背后的具体功能要求,但这对 UX 不利。也许您只是在没有首先研究JSF 规范更不用说JSF 源代码的情况下随意试验?

于 2013-11-18T03:49:45.267 回答