代码之家  ›  专栏  ›  技术社区  ›  SomeFatMan

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

jsf
  •  6
  • SomeFatMan  · 技术社区  · 16 年前

    <h:form> 标签?我需要能够禁用多个页面,希望只通过查看backingbean中的一个布尔值。任何帮助都将不胜感激。

    -编辑-
    是否有任何容器或类似的东西,我可以包装我的输入,可以设置为禁用?这样我只需要在一个地方引用diable,也可以让每个字段设置自己的disable属性,如果在其他逻辑中需要的话?

    6 回复  |  直到 16 年前
        1
  •  6
  •   pakore    16 年前

    您可以禁用按钮,链接,输入字段(一切允许编辑)等。。。将它们都指向backingbean中的相同值。

    <h:inputText disabled="#{!bean.permissionToWrite}">
    

    :回答Alex Larzelere的评论。

    在这种情况下最好使用IMHO disabled rendered 因为您可能希望在 inputText 盒子,但不让他修改它。如果你不渲染,他就看不到。你可以用 outputText

    更新 您可以将组件包装在 h:panelGroup 例如,是否渲染取决于逻辑,但不能从中禁用内部的输入字段 h:面板组 ,你必须单独做。

        2
  •  5
  •   Oversteer    14 年前

    这个简单的自定义组件可以用来包装其他组件,如果与disabled=“true”属性或计算为true的EL表达式一起使用,它将禁用这些组件。它的工作方式是,如果已经禁用了一个包装组件,那么如果使用disablePanel(或ajax重新呈现)并使用disabled=“true”,它就不会被启用。组件仅尝试禁用UIInput&UICommand组件,我认为可以,但可以更改。

    xmlns:fnc="http://myco.co.uk/fnc"
    ...
    <fnc:disablePanel disabled="#{bean.isItDisabled}">
       <h:inputText/>
       ...
    </fnc:disablePanel>
    ...
    

    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);
        }
    }
    

    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>
    
        3
  •  1
  •   Kukeltje    8 年前

    Omnifaces massAttribute taghandler具有多个选项。最接近你需要的是

    <o:massAttribute name="disabbled" value="true" target="javax.faces.component.UIInput">
        <h:outputLabel for="input1" />
        <h:inputText id="input1" />
        <h:outputLabel for="input2" />
        <h:inputText id="input2" />
        <h:outputLabel for="input3" />
        <h:inputText id="input3" />
    </o:massAttribute>
    

    这将禁用从 javax.faces.component.UIInput .

        4
  •  0
  •   Slava Semushin    16 年前

    希望以下链接对您有用:

        5
  •  0
  •   Ignacio Rubio    8 年前

    这是一个禁用页面中所有组件的解决方案。不是这样的, 这样,您还可以一次仅禁用一组组件

    我把方法 禁用UIComponent 在一个实用类中,因为我想在几个jsfbean中使用它。

    public class UtilsPrimefaces { 
    
    /**
     * Disable all the children components
     * @param uiComponentName
     */
    public static void disableUIComponent(String uiComponentName) {  
        UIComponent component = FacesContext.getCurrentInstance()  
                .getViewRoot().findComponent(uiComponentName);
        if(component!=null) {
            disableAll(component.getChildren());
        } 
    }  
    
    /**
     * Recursive method to disable the list
     * @param components Widget PD list
     */
    private static void disableAll(List<UIComponent> components) {  
    
        for (UIComponent component : components) {  
            logger.info(component.getClass().getTypeName());            
    
            if (component instanceof InputText) {  
                ((InputText) component).setDisabled(true);
    
            } else if (component instanceof InputNumber) {  
                ((InputNumber) component).setDisabled(true);
    
            } else if (component instanceof InputTextarea) {  
                ((InputTextarea) component).setDisabled(true);
    
            }  else if (component instanceof HtmlInputText) {  
                ((HtmlInputText) component).setDisabled(true);
    
            }  else if(component instanceof SelectOneMenu) {  
                ((SelectOneMenu) component).setDisabled(true);
    
            } else if(component instanceof SelectBooleanCheckbox) {  
                ((SelectBooleanCheckbox) component).setDisabled(true);
    
            } else if(component instanceof CommandButton) {  
                ((CommandButton) component).setDisabled(true);              
            }
            disableAll(component.getChildren());  
        }  
    } 
    

    然后你可以在你的豆子里用。这是一个有3个滚动面板的页面示例,我只想禁用panel1和panel3:

    @PostConstruct
    public void init() {        
        super.init();
        if(userHasPermissions() || loadPageInReadOnly()) {
             Utils.disableUIComponent(":form:panel1");
             Utils.disableUIComponent(":form:panel3");
        }
    }
    

    它对我有用:) 我用的是PrimeFaces2.2

        6
  •  -1
  •   Adityaz7    8 年前

    如果您想检查性能,请使用JQuery。 使用窗体中所有元素的prop属性将disabled属性设置为true。

    $(document).ready(function(){
            $("#formName :input").prop("disabled", true);
        });
    

    您也可以在这里使用backingbean的布尔属性。例如

    $(document).ready(function(){
            if(#{bean.property} === true)
            $("#formName :input").prop("disabled", true);
        });
    
    推荐文章