首页 文章

如何在jsf / primefaces中从托管bean向页面添加组件[复制]

提问于
浏览
0

这个问题在这里已有答案:

单击commandButton应该在ManagedBean中触发一个动作:向当前页面添加一个新的“outputText”组件 .

总体思路是通过用户操作动态更改页面,使用服务器端操作,因为添加到页面的新元素需要布置数据库中的数据 .

  • 如何在jsf / primefaces中从托管bean向页面添加组件?假设元素应该添加到现有的div中,例如:

<div id="placeHolder">
</div>

(如果需要,可以将此div更改为jsf面板)

注意:如果替代方法更好地达到相同的效果,我很乐意了解它们 .

2 回答

  • 2

    除了您发布的解决方案之外,我会为您提供另一种解决方案 . 基本上它有一个给定的输出 List ,每按一次按钮就会增加 . 这应该呈现与您声明的解决方案完全相同的DOM树:

    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Tiles</title>
        <h:outputStylesheet name="css/320andup_cle.css" />
    </h:head>
    <h:body>
        <h:form>
            <h:commandButton actionListener="#{bean.createNewTile}" title="new"
                value="new" />
        </h:form>
    
        <h:panelGroup layout="block" id="tiles">
            <ui:repeat var="str" value="#{bean.strings}">
                <h:panelGroup>
                    <h:outputText styleClass="tile" value="#{str}" />
                </h:panelGroup>
            </ui:repeat>
        </h:panelGroup>
    </h:body>
    </html>
    
    @ManagedBean
    @SessionScoped
    public class Bean {
    
        List<String> strings = new ArrayList<String>();
    
        public List<String> getStrings() {
            return strings;
        }
    
        public void createNewTile() {
            strings.add("output");
        }
    }
    

    除了更简单的恕我直言,它有一个主要的优势: it doesn't couple your server side code to JSF implicit API . 如果希望它是CDI托管bean,则可以更改 @ManagedBean@ManagedBean 注释 .

  • 1

    解决方案:

    这是一个jsf页面,每次单击时都会创建一个新的div:

    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html">
        <h:head>
            <title>Tiles</title>
            <h:outputStylesheet name="css/320andup_cle.css" />
        </h:head>
        <h:body>
            <h:form>
                <h:commandButton actionListener="#{bean.createNewTile()}" title="new" value="new"/>
            </h:form>
    
            <h:panelGroup layout="block" id="tiles">
            </h:panelGroup>    
        </h:body>
    </html>
    

    托管Bean:

    @Named
    @SessionScoped
    public class Bean implements Serializable {
    
        private UIComponent found;
    
        public void createNewTile() {
    
            HtmlPanelGroup div = new HtmlPanelGroup();
            div.setLayout("block");
    
    
            HtmlOutputText tile = new HtmlOutputText();
            tile.setValue("heeeeeRRRRRRRRRRRRRR         ");
            tile.setStyleClass("tile");
            div.getChildren().add(tile);
    
            doFind(FacesContext.getCurrentInstance(), "tiles");
            found.getChildren().add(div);
    
        }
    
        private void doFind(FacesContext context, String clientId) {
            FacesContext.getCurrentInstance().getViewRoot().invokeOnComponent(context, clientId, new ContextCallback() {
                @Override
                public void invokeContextCallback(FacesContext context,
                        UIComponent component) {
                    found = component;
                }
            });
        }
    }
    

    看到这个使用动态生成组件逻辑构建的应用程序:https://github.com/seinecle/Tiles

相关问题