必须单击两次命令按钮

时间:2016-01-28 15:27:29

标签: jsf-2

我的服务器上有一堆html文件,当点击一个菜单按钮时会加载到我的JSF视图中。

其中许多文件都有一个操作按钮,如下所示:

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

单击该按钮时,该文件的名称将添加到该购物车中以供后续发布。

支持bean看起来像这样:

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();
            }
        }
    }

}

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;
    }

}

现在问题出现了:我必须单击两次按钮才能将文件名添加到列表中。这是为什么?当然,我只想点击按钮一次就可以了。

感谢您的帮助和提示!!

0 个答案:

没有答案