0

I have a bunch of html files on my server that get loaded into my JSF view when a menu button is clicked.

Many of those files have an action button in them, like so:

<?xml version="1.0" encoding="utf-8"?>
<div xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns="http://www.w3.org/1999/xhtml" id="d10e780" class="content">
    <h:form id="publicationForm">
        <h:panelGrid columns="2" width="45%">
            <h:commandButton id="addToCart" value="Add to publishing Cart" actionListener="#{cartController.addToCart('d10e780.html')}">
                <f:ajax execute="@form" render="@none"/>
            </h:commandButton>
            <h:commandButton id="viewCart" value="View publishing cart" action="#{cartController.viewCart()}"/>
        </h:panelGrid>
    </h:form>
    <div id="d10e780" class="content">
        <ul class="level_two">
            <li class="level_two">SomeStuff</li>
        </ul>
    </div>
</div>

when the button is clicked, the name of the file is added to that shopping cart for subsequent publication.

The backing beans look like this:

package controllers;

import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

import org.xml.sax.SAXException;

import utils.PDFCreator;

@Named
@SessionScoped
public class Publication implements Serializable {

    private static final long serialVersionUID = -2404153211000142338L;

    private List<String> documents = new ArrayList<>();
    private Map<Long, Boolean> selected = new HashMap<Long, Boolean>();

    public Publication() {

    }

    public Map<Long, Boolean> getSelected() {
        return selected;
    }

    public void setSelected(Map<Long, Boolean> selected) {
        this.selected = selected;
    }

    public List<String> getDocuments() {
        return documents;
    }

    public void setDocuments(List<String> documents) {
        this.documents = documents;
    }

    public void addDocument(String dokumentId) {
        if (!documents.contains(dokumentId)) {
            documents.add(dokumentId);
        }

    }

    public void publishPDF() {
        List<String> checkedItems = new ArrayList<>();
        for (String doc : documents) {
            if (selected.get(doc)) {
                checkedItems.add(doc);
                System.out.println("BEING PUBLISHED " + doc);

            }
        }

        selected.clear();

        for (String item : checkedItems) {
            try {
                PDFCreator.makePDF(Paths.get(item));
            } catch (SAXException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

and

package controllers;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import views.Pages;

@SessionScoped
@Named
public class CartController implements Serializable {

    @Inject
    private Publication publication;

    private static final long serialVersionUID = 7768738867325966926L;

    public CartController() {
    }

    public void addToCart(String dokumentId) {
        System.out.println("CARTCONTROLLER: ADDTOCART: " + dokumentId);
        if (getPublication() == null) {
            publication = new Publication();
            getPublication().addDocument(dokumentId);
        } else {
            getPublication().addDocument(dokumentId);
        }

    }

    public String viewCart() {
        return Pages.PUBLICATION + "?faces-redirect=true";
    }

    public void editItem(String title) {
    }

    public void updateCart(String title) {
    }

    public Publication getPublication() {
        return publication;
    }

    public void setPublication(Publication publication) {
        this.publication = publication;
    }

}

Now here's the problem: I have to click the button twice for the file name to be added to the list. Why is that? I'd like, of course, to just click the button once and have it work.

Thanks for help and tips!!

user3629892
  • 2,494
  • 4
  • 26
  • 51
  • Why did you not search for that last statement: _"I have to click the button twice"_ (... before an action is executed) and why did you not make that your title? THAT is your actual technical problem (please correct the title) – Kukeltje Jan 28 '16 at 16:05
  • Sorry, I thought it had to do with my backing beans. Had I known that it could be an issue with the command button, I would have chosen a better title. – user3629892 Jan 28 '16 at 16:51
  • Related: http://stackoverflow.com/q/7108668 – BalusC Jan 28 '16 at 18:34

0 Answers0