JavaServer Faces(JSF)-ui:repeat不显示新元素 [英] JavaServer Faces (JSF) - ui:repeat not showing new elements

查看:117
本文介绍了JavaServer Faces(JSF)-ui:repeat不显示新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的JSF应用程序,但是又遇到了麻烦...

I'm developing a simple JSF application, but I'm stuck again...

基本上,我有一个已登录并想要发布新消息的人:

Basically, I have a person who is logged in and wants to post a new message:

<h:form id="messageform">
    <div style="width:900px; margin-top:-20px;">
        <div id="count">250</div>
        <div id="barbox"><div id="bar"></div></div>
        <h:inputTextarea id="textarea_message" cols="8" rows="5" value="#{messageBean.message}" />
    </div>
    <h:commandButton id="message_submit" value="" action="#{messageBean.postMessage(login.username)}" styleClass="input-submitbericht" />
</h:form>

还有messageBean中的postMessage方法.

And the postMessage method that is in the messageBean.

public void postMessage(String username) {
    Message.clearMessage();
    System.out.println(db.getAlleBerichten().size());
    if (Message.checkValidMessage(message) && Message.checkValidUsername(username)) {
        Gebruiker g = db.readGebruiker(username);
        db.addBerichtBijGebruiker(g, message);
        Message.setMessage("Uw bericht is succesvol gepost!");
        Message.setMessageType("info");
        Message.setRenderMessage(true);
        System.out.println(db.getAlleBerichten().size());
    }
}

此代码运行完美.我在messageBean中使用了一个简单的System.out.println(),并且该元素得到了完美的添加.

This code works perfectly. I used a simple System.out.println() in my messageBean, and the element gets added perfectly.

现在,当我尝试在屏幕上显示新消息时,根本不会检测到该消息.就像元素从未添加到ArrayList中一样.

Now, when I try to display the new message on the screen, it simply doesn't get detected. It's like the element never got added to the ArrayList...

我使用以下代码显示消息:

I use this code for displaying the messages:

<ui:repeat value="#{database.alleBerichten}" var="berichten">
    <div class="record">
        <div class="#{(rowCount % 2) != 0 ? 'posteven' : 'postodd'}">
            <div class="text_wrapper text_wrapperm#{berichten.id}">
                #{berichten.bericht}
            </div>
            <div class="edit editm#{berichten.id}" style="display:none; width:890px;">
                <textarea class="editbox editboxm#{berichten.id}" cols="23" rows="3" id="m#{berichten.id}" method="message"></textarea>
            </div>
            <div class="postinfo"> door #{berichten.gebruiker.gebruikersnaam}
                <c:if test="#{berichten.gebruiker.gebruikersnaam == login.username}">
                    <h:form>
                        <a style="cursor: pointer;" class="edit_link" title="Edit" id="m#{berichten.id}">Wijzigen</a> -
                        <h:commandLink value="Verwijderen" action="#{database.deleteBericht(berichten.id)}" styleClass="delbutton"/>
                    </h:form>
                </c:if>
            </div>
        </div>
    </div>
    <c:set var="rowCount" value="#{rowCount + 1}"/>
</ui:repeat>

所以我认为问题出在ui:repeat部分,但是我不知道该如何解决...

So I think the problem is with the ui:repeat part, but I don't know how I can fix this...

推荐答案

您在JSF <ui:repeat>标记内有一个JSTL <c:if>标记.重要的是要了解JSTL和JSF不会像您期望的那样同步运行.它是JSTL,它在视图构建期间首先运行,并且仅使用JSF标签生成视图.然后,JSF在视图渲染期间运行并生成HTML输出.

You have a JSTL <c:if> tag inside JSF <ui:repeat> tag. It's important to understand that JSTL and JSF doesn't run in sync as you'd expect from the coding. It's JSTL which runs first during view build time and generates a view with JSF tags only. Then JSF runs during view render time and generates HTML output.

当JSTL <c:if>标记运行时,从不不可用JSF <ui:repeat>属性的var后面的对象.您想改用JSF标记/属性.在这种情况下,rendered属性是合适的.

The object behind var attribute of JSF <ui:repeat> is thus never available when the JSTL <c:if> tag runs. You want to use a JSF tag/attribute instead. In this particular case, the rendered attribute is appropriate.

所以,替换

<c:if test="#{berichten.gebruiker.gebruikersnaam == login.username}">
    <h:form>
        <a style="cursor: pointer;" class="edit_link" title="Edit" id="m#{berichten.id}">Wijzigen</a> -
        <h:commandLink value="Verwijderen" action="#{database.deleteBericht(berichten.id)}" styleClass="delbutton"/>
    </h:form>
</c:if>

作者

<h:form rendered="#{berichten.gebruiker.gebruikersnaam == login.username}">
    <a style="cursor: pointer;" class="edit_link" title="Edit" id="m#{berichten.id}">Wijzigen</a> -
    <h:commandLink value="Verwijderen" action="#{database.deleteBericht(berichten.id)}" styleClass="delbutton"/>
</h:form>


此外,JSTL <c:set>标记在这里不起作用.如果您使用的是JSF 2.0,请改用<ui:repeat>varStatus属性.


Also the JSTL <c:set> tag isn't going to work here. If you're using JSF 2.0, use the varStatus attribute of the <ui:repeat> instead.

<ui:repeat value="#{database.alleBerichten}" var="berichten" varStatus="loop">
    <div class="record">
        <div class="#{(loop.index % 2) != 0 ? 'posteven' : 'postodd'}">

这篇关于JavaServer Faces(JSF)-ui:repeat不显示新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆