如何在JSF中禁用页面/表单

Som*_*Man 6 jsf

对于我的应用程序,我希望拥有不同权限的用户.一个权限允许用户查看我的数据库中的数据,而另一个权限允许他们编辑数据.在登录时,我会检查他们的许可,以确定他们是否可以编辑数据.如果用户具有只读权限,我正在寻找一些方法来禁用整个页面.是否有一种简单的方法来禁用<h:form>标签内的所有内容?我需要能够禁用多个页面,希望通过查看supbean中的一个布尔值来实现.任何帮助将不胜感激.

-EDIT-
是否有任何容器或类似物可以包裹我的输入可以设置为禁用?这样我只需要在一个地方引用diable,并且如果在我拥有的其他逻辑中需要,还可以让每个字段设置自己的禁用属性?

pak*_*ore 6

您可以禁用按钮,链接,输入字段(允许编辑的所有内容)等...将它们全部指向支持bean中的相同值.

<h:inputText disabled="#{!bean.permissionToWrite}">
Run Code Online (Sandbox Code Playgroud)

我不知道如何禁用表单中包含的所有内容,但您可能不希望这样做,因为您可能希望用户使用表单提交搜索查询(即搜索框).

更新:回答Alex Larzelere的评论.

在这种情况下,恕我直言更好地使用,disabled而不是rendered因为您可能想要在inputText框中向用户显示字段的值,但不允许他修改它.如果你不渲染它,他就看不到它.您也可以使用outputText它来显示它,但是您必须维护两个元素而不是一个元素.

更新回答问题中的编辑:您可以将组件包装在一个h:panelGroup例子中,然后根据您的逻辑进行渲染,但是您无法禁用内部的输入字段h:panelGroup,您必须单独执行此操作.

  • @Kishor你可以使用一个变量.或者你甚至可以在不使用大写字母的情况下改善我 (2认同)

Ove*_*eer 5

这个简单的自定义组件可用于包装其他组件,如果使用属性disabled ="true"或EL表达式评估为true,它将禁用它们.它的工作方式是,如果已经禁用了一个包装的组件,那么如果使用disablePanel(或者ajax重新呈现)且disabled ="true",它将不会被启用.该组件只尝试禁用UIInput和UICommand组件,我认为这是可以的但可以更改.

xmlns:fnc="http://myco.co.uk/fnc"
...
<fnc:disablePanel disabled="#{bean.isItDisabled}">
   <h:inputText/>
   ...
</fnc:disablePanel>
...
Run Code Online (Sandbox Code Playgroud)

UIDisablePanel.java

package uk.co.myco.component;

import java.io.IOException;
import javax.faces.component.*;
import javax.faces.context.FacesContext;

/*
 * @author Brendan Healey (Oversteer)
 */

@FacesComponent("uk.co.myco.component.UIDisablePanel")
public class UIDisablePanel extends UIComponentBase {

    private enum PropertyKeys {
        disabled;
    }

    public UIDisablePanel() {
        setRendererType(null);
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {

        boolean toDisable = isDisabled();
        processDisablePanel(this, toDisable);
        //super.encodeBegin(context);
    }

    public void processDisablePanel(UIComponent root, boolean toDisable) {

        /*
         * The key point here is that a child component of <x:disablePanel> may
         * already be disabled, in which case we don't want to enable it if the
         * <x:disablePanel disabled= attribute is set to true.
         */

        for (UIComponent c : root.getChildren()) {
            if (c instanceof UIInput || c instanceof UICommand) {
                if(toDisable) { // <x:disablePanel disabled="true">
                    Boolean curState = (Boolean) c.getAttributes().get("disabled");
                    if(curState == null || curState == false) {
                        c.getAttributes().put("UIPanelDisableFlag", true);
                        c.getAttributes().put("disabled", true);
                    }
                }
                else { // <x:disablePanel disabled="false">
                    if(c.getAttributes().get("UIPanelDisableFlag") != null) {
                        c.getAttributes().remove("UIPanelDisableFlag");
                        c.getAttributes().put("disabled", false);
                    }
                }
            }

            if (c.getChildCount() > 0) {
                processDisablePanel(c, toDisable);
            }
        }

    }

    @Override
    public String getFamily() {
        // Got to override it but it doesn't get called.
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isDisabled() {
        return (boolean) getStateHelper().eval(PropertyKeys.disabled, false);
    }

    public void setDisabled(boolean disabled) {
        getStateHelper().put(PropertyKeys.disabled, disabled);
    }
}
Run Code Online (Sandbox Code Playgroud)

disablepanel.taglib.xml

<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib version="2.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd">
    <namespace>http://myco.co.uk/fnc</namespace>
    <tag>
        <tag-name>disablePanel</tag-name>
        <component>
            <component-type>uk.co.myco.component.UIDisablePanel</component-type>
        </component>
        <attribute>
            <name>disabled</name>
        </attribute>
    </tag>
</facelet-taglib>
Run Code Online (Sandbox Code Playgroud)