【问题标题】:Why is required="true" not triggered when rendered="false"为什么渲染=“false”时未触发required=“true”
【发布时间】:2013-11-18 03:42:00
【问题描述】:

我对 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 没有值。

【问题讨论】:

  • 您使用的是哪个 JSF 版本和 impl?
  • @Luiggi:行为符合 JSF 规范,因此不太可能是 impl/version 特定问题。
  • @BalusC 我误读了 I haveI didn't,因此发现这种行为真的很奇怪。
  • @Luiggi:啊,我完全可以想象,我还得把问题读两遍。

标签: validation jsf required rendered-attribute


【解决方案1】:

rendered 属性也在验证和更新模型值阶段进行评估。如需证据,请查看javax.faces.component.UIInput source code(行号根据 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" />

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

虽然我想知道这背后的具体功能要求,但这对用户体验不利。也许您只是在没有先研究JSF specification 更不用说JSF source code 的情况下随意尝试?

【讨论】:

    猜你喜欢
    • 2020-06-19
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2013-09-18
    • 1970-01-01
    • 2015-03-22
    相关资源
    最近更新 更多