【问题标题】:Fail to call method inside JSF 2.0 Composite Component无法在 JSF 2.0 复合组件中调用方法
【发布时间】:2011-12-14 05:34:42
【问题描述】:

我正在学习在 JSF 2.0 中使用复合组件。

首先我在下面创建了这个组件。 它声明了一个managedBean,并直接调用了managedBean的方法。

<h:commandButton 
                                action = "#{cc.attrs.managedBean.toogleEditing}"
                                 value = "#{cc.attrs.managedBean.editing?'Back':'Edit'}" 
                                update = "componentes"/>

这是组件的完整代码:

<ui:component          
              xmlns="http://www.w3.org/1999/xhtml" 
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:h="http://java.sun.com/jsf/html"
           xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:p="http://primefaces.prime.com.tr/ui"
    xmlns:composite="http://java.sun.com/jsf/composite">

    <composite:interface>
        <composite:attribute 
                name ="managedBean"         
                type = "java.lang.Object"
            required ="true">                   
        </composite:attribute>
    </composite:interface>

    <composite:implementation>
        <f:view contentType="text/html"> 
            <h:form id="componentes">  
                <h:panelGrid columns="3">
                    <h:panelGroup>              
                        <h:outputText 
                              escape = "false" 
                               value = "#{cc.attrs.managedBean['value']}"       
                            rendered = "#{!cc.attrs.managedBean['editing']}"/> 
                        <p:editor 
                            widgetVar = "editor" 
                                value = "#{cc.attrs.managedBean.value}" 
                             rendered = "#{cc.attrs.managedBean.editing}"/>
                    </h:panelGroup>

                    <h:commandButton 
                                action = "#{cc.attrs.managedBean.toogleEditing}" 
                                 value = "#{cc.attrs.managedBean.editing?'Back':'Edit'}" 
                                update = "componentes"/>

                    <p:commandButton 
                                action = "#{cc.attrs.managedBean.toogleEditing}" 
                                 value = "#{cc.attrs.managedBean.editing?'Back':'Edit'}" 
                                update = "componentes"/>
                </h:panelGrid>
            </h:form>                    
        </f:view>
    </composite:implementation>
</ui:component>

在第二个测试中,我创建了一个作为方法的属性。

<composite:attribute 
                        name = "action" 
            method-signature = "void action()"
                    required = "true"/>  

我使用属性调用方法,而不是上面从 managedBean 调用方法的其他版本。

<h:commandButton 
                                action = "#{cc.attrs.action}" 
                                 value = "#{cc.attrs.managedBean.editing?'Back':'Edit'}" 
                                update = "componentes"/>


This is the code of the second component:   

<ui:component          
              xmlns = "http://www.w3.org/1999/xhtml" 
            xmlns:f = "http://java.sun.com/jsf/core"
            xmlns:h = "http://java.sun.com/jsf/html"
           xmlns:ui = "http://java.sun.com/jsf/facelets"
            xmlns:p = "http://primefaces.prime.com.tr/ui"
    xmlns:composite = "http://java.sun.com/jsf/composite">

    <composite:interface>
        <composite:attribute 
                name = "managedBean"            
                type = "java.lang.Object"
            required = "true">                  
        </composite:attribute>
        <composite:attribute 
                        name = "action" 
            method-signature = "void action()"
                    required = "true"/>  
    </composite:interface>

    <composite:implementation>
        <f:view contentType="text/html"> 
            <h:form id="componentes">  
                <h:panelGrid columns="3">
                    <h:panelGroup>              
                        <h:outputText 
                              escape = "false" 
                               value = "#{cc.attrs.managedBean['value']}"       
                            rendered = "#{!cc.attrs.managedBean['editing']}"/> 
                        <p:editor 
                            widgetVar = "editor" 
                                value = "#{cc.attrs.managedBean.value}" 
                             rendered = "#{cc.attrs.managedBean.editing}"/>
                    </h:panelGroup>

                    <h:commandButton 
                                action = "#{cc.attrs.action}" 
                                 value = "#{cc.attrs.managedBean.editing?'Back':'Edit'}" 
                                update = "componentes"/>

                    <p:commandButton 
                                action = "#{cc.attrs.action}" 
                                 value = "#{cc.attrs.managedBean.editing?'Back':'Edit'}" 
                                update = "componentes"/>
                </h:panelGrid>
            </h:form>                    
        </f:view>
    </composite:implementation>
</ui:component>

这是我称之为两个组件的 xhtml 页面:

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html 
             xml:lang="pt" 
                 lang="pt"
                xmlns="http://www.w3.org/1999/xhtml" 
              xmlns:f="http://java.sun.com/jsf/core"
              xmlns:h="http://java.sun.com/jsf/html"
             xmlns:ui="http://java.sun.com/jsf/facelets"
              xmlns:p="http://primefaces.prime.com.tr/ui"
    xmlns:components="http://java.sun.com/jsf/composite/components">

    <h:head id="head">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Editable HTML Text Sample</title>
    </h:head>

    <h:body id="body">
        <f:view contentType="text/html"> 
            <h:form>       
                <p:panel header="Editable HTML Text Sample">

                    <components:EditableHTMLText 
                        managedBean = "#{editableHTMLText}"/>

                </p:panel>       

                <p:panel header="Editable HTML Text Sample Direct Action">

                    <components:EditableHTMLTextPrimeFaces 
                        managedBean = "#{editableHTMLText}" 
                             action = "#{editableHTMLText.toogleEditing}"/>

                </p:panel>                                                                  
            </h:form>                    
        </f:view>
    </h:body>
</html>

问题是,在这个 index.xhtml 中,如果我调用两个组件,第二个组件(具有在属性标记中声明的方法的那个)不起作用。 如果在 index.xhtml 我评论第一个组件。比第二个工作正常。 一定是某种冲突,但我不明白为什么。 有什么提示吗?

这是豆子:

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


@ManagedBean
@SessionScoped
public class EditableHTMLText implements Serializable{

    /**
     * 
     */
    private static final long   serialVersionUID    = 8439126615761864409L;

    private String              value               = "Test<br>of<br>HTML<br>text<br><b>ITEM</b><br>";
    private boolean             editing             = false;


    public void toogleEditing(){

        this.setEditing(!this.isEditing());
        System.out.println("Editing State: " + this.editing);
    }


    public String getValue(){

        return value;
    }


    public void setValue(String value){

        this.value = value;
    }


    public boolean isEditing(){

        return editing;
    }


    public void setEditing(boolean editing){

        this.editing = editing;
    }

}

【问题讨论】:

    标签: jsf-2 composite-component


    【解决方案1】:

    快速思考一下:我看到您将 f:view 放在两个组件中。我的理解是只需要/允许一个。我什至在整个项目中都没有。

    【讨论】:

      猜你喜欢
      • 2012-10-15
      • 1970-01-01
      • 2011-10-14
      • 2012-06-08
      • 2011-11-09
      • 2013-02-02
      • 2016-08-18
      • 2013-02-27
      • 2014-03-03
      相关资源
      最近更新 更多