【问题标题】:Added String to ArrayList not shown / not updated [duplicate]将字符串添加到 ArrayList 未显示/未更新 [重复]
【发布时间】:2018-02-05 10:42:59
【问题描述】:

...所以我有我的@ApplicationScoped Bean“应用程序”..:

    @ManagedBean(name = "Application")
    @ApplicationScoped
    public class Application implements Serializable {

        private boolean isRunning = false;

        private ArrayList<Feed> sentNotifications = new ArrayList<>();

        private ArrayList<String> emails = new ArrayList<>(
                Arrays.asList("f00@b4r.com", "test@test.com")
        );

        private LinkedList<String> words = new LinkedList<>(
                Arrays.asList("vuln","banana","pizza","bonanza")
        );

        private LinkedList<String> feeds = new LinkedList<>(
                Arrays.asList("http://www.kb.cert.org/vulfeed",
                "https://ics-cert.us-cert.gov/advisories/advisories.xml",
                "https://ics-cert.us-cert.gov/alerts/alerts.xml")
        );

...并希望使用以下方法将字符串添加到ArrayList&lt;String&gt; emails

public String addEmail(String email) {
        emails.add(email);
        return null;
}

Facelet 如下:

 <!-- EMAILS -->
    <h3>Configured Emails</h3>
    <h:form>
        <h:inputText value="Email" var="email"/>
        <h:commandButton value="Add Email" action="#{Application.addEmail(email)}"/>
    </h:form>
    <h:form>
        <ui:repeat var="email" value="#{Application.emails}">
            <tr>
                <td>#{email}</td>

                <td>
                    <f:facet name="header">Action</f:facet>
                    <h:commandLink value="Delete" action="#{Application.rmEmail(email)}"/>
                </td>
            </tr>
            <br></br>
        </ui:repeat>
    </h:form>

...所以当我尝试添加“blabla@bla.com”时,结果如下:

  • 显示了一个删除按钮,但没有显示字符串本身?!
  • 是否正确添加了字符串 - 并且 JSF 没有重新呈现视图..?
  • ..还是没有正确添加String?

请帮忙! 谢谢。

【问题讨论】:

  • @Kukeltje 我为此使用 JSF2 和 Primefaces,你为什么要编辑它?
  • 因为您的问题中没有特定的 JSF-2 或 PrimeFaces。它是普通的 JSF。只是您(在某处)使用 PrimeFaces 的事实没有理由将其用作标签
  • @Kukeltje mkay, mkay... 你知道答案吗? :)
  • 我会给你一个链接,但是请阅读一些好的教程。您的代码中有非常基本的错误:stackoverflow.com/questions/3681123/…
  • 感谢@Kukeltje,...我现在明白了。我已经阅读了很多教程 - 但您可能会发现它并不总是微不足道的,特别是如果 JSF 出于某种奇怪的原因不喜欢来自 xhtml 的参数化方法调用。不过,我学到了很多东西!

标签: jsf arraylist


【解决方案1】:

我终于修好了!

感谢用户@Kukeltje,他向我暗示了我做错的基本事情 - 以及用户@Wietlol,他通过精神上的支持和“相信我”在聊天中激励了我:)

解决办法:

..在 Application.java 中:

public List<Feed> sentNotifications = new ArrayList<>();

public List<String> emails = new ArrayList<>();

public List<String> words = new LinkedList<>(Arrays.asList("vuln", "banana", "pizza", "bonanza"));

public List<String> feeds = new LinkedList<>(
        Arrays.asList("http://www.kb.cert.org/vulfeed",
                "https://ics-cert.us-cert.gov/advisories/advisories.xml",
                "https://ics-cert.us-cert.gov/alerts/alerts.xml")
);

private String currentEmail;
private String currentFeed;
private String currentWord;

[...]

 public void addEmail() {
        emails.add(currentEmail);
 }

..和 gui.xhmtl:

 <!-- EMAILS -->
    <h3>Configured Emails</h3>
    <h:form>
        <h:inputText value="#{Application.currentEmail}" var="email"/>
        <h:commandButton value="Add Email" action="#{Application.addEmail}"/>
    </h:form>
    <h:form>
        <ui:repeat var="email" value="#{Application.emails}">
            <tr>
                <td>#{email}</td>

                <td>
                    <f:facet name="header">Action</f:facet>
                    <h:commandLink value="Delete" action="#{Application.rmEmail(email)}"/>
                </td>
            </tr>
            <br></br>
        </ui:repeat>
    </h:form>

注意action="#{Application.addEmail}" 没有使用参数——而是通过value="#{Application.currentEmail}" 将参数传递给方法。

如果你,读者,有同样的问题,请考虑以下几点:

  • bean 中每个字段的 getter/setter
  • 原始字段!
  • “委托”原始字段的无参数方法,例如我的 addEmail 方法

希望这个答案对遇到同样问题的人有用!

您好, 格乌雷

【讨论】:

    猜你喜欢
    • 2016-08-02
    • 2022-11-30
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多