未将字符串添加到ArrayList中/未更新 [英] Added String to ArrayList not shown / not updated

查看:118
本文介绍了未将字符串添加到ArrayList中/未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...所以我有@ApplicationScoped Bean应用程序" ..

...So i've got my @ApplicationScoped Bean "Application"..:

    @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<String> emails:

...and want to add a String to ArrayList<String> emails using the method:

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

Facelet如下:

The Facelet goes as follows:

 <!-- 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"时,结果如下:

...so when i try to add "blabla@bla.com", this is the Result:

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

请帮助! 谢谢.

推荐答案

我终于修复了它!

感谢@Kukeltje用户,他向我提示了我做错的基本事情-和@Wietlol用户,通过道德支持和相信我"来激励我进行聊天:)

Thanks to User @Kukeltje, who hinted me at basic things that i got wrong - and User @Wietlol who motivated me in the chat through moral support and 'believes in me' :)

解决方案:

..在Application.java中:

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

.. and gui.xhmtl:

..and 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}"将参数传递给方法.

Notice how action="#{Application.addEmail}" does not make use of arguments- rather the parameter is handed to the method via value="#{Application.currentEmail}".

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

If you, reader, have the same problem please consider these points:

  • 获取/设置Bean中的每个字段
  • 原始字段!
  • 无需参数的方法即可委托"原始字段,例如我的addEmail方法
  • getter/setter for each Field in the bean
  • primitive fields!
  • argument-less methods to 'delegate' the primitive Fields, e.g. my addEmail method

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

Hope this answer is usefull to ppl having the same issue!

问候, 皮革

这篇关于未将字符串添加到ArrayList中/未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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