添加String到ArrayList未显示/未更新

时间:2017-08-28 08:00:16

标签: jsf arraylist

...所以我的@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<String> 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”时,这就是结果:

enter image description here

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

请帮忙! 谢谢。

1 个答案:

答案 0 :(得分: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中每个Field的getter / setter
  • 原始字段!
  • 无参数方法'委托'原始字段,例如我的addEmail方法

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

问候, Gewure