0

...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")
        );

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

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

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>

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

enter image description here

  • There is a delete-button shown, but not the String itself?!
  • Is the String correctly added - and JSF doesnt rerender the view..?
  • ..Or is the String not added correctly?

please help! thanks.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Gewure
  • 1,208
  • 18
  • 31
  • @Kukeltje Im using JSF2 & Primefaces for this, why did you edit it out? – Gewure Aug 28 '17 at 08:24
  • 2
    Because there is nothing JSF-2 or PrimeFaces specific in your question. It is plain JSF. Just the fact that you are (somewhere) using PrimeFaces is no reason to use it as a tag – Kukeltje Aug 28 '17 at 10:58
  • @Kukeltje mkay, mkay... do you know an answer, though? :) – Gewure Aug 28 '17 at 11:33
  • I'll provide you with a link, but please read some good tutorials. Very basic thing wrong in your code: https://stackoverflow.com/questions/3681123/how-to-send-form-input-values-and-invoke-a-method-in-jsf-bean – Kukeltje Aug 28 '17 at 12:45
  • thanks @Kukeltje,... i got it now. I've read quite some tutorials - but as you may figure out its not always trivial, especially if JSF for some weird reason doesn't like parameterized method calls from the xhtml. I've learned quite a lot, though! – Gewure Aug 28 '17 at 14:52
  • @BalusC not a duplicated - i read the marked questions into detail and it didn't help me much either, because none involves lists or anything other than primitives. – Gewure Aug 28 '17 at 15:10
  • Your technical problem has got nothing to do with lists or primitives. The given links explain the correct approaches and the inner workings. By the way, I have the impression that you got the actual meaning of "primitives" wrong. – BalusC Aug 28 '17 at 15:26

1 Answers1

1

I fixed it, finally!

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' :)

The solution:

..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:

 <!-- 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>

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:

  • 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!

Greetings, Gewure

Gewure
  • 1,208
  • 18
  • 31