如何在JSF中的不同页面和beas之间传递对象?

时间:2019-01-18 11:46:19

标签: jsf xhtml managed-bean

当我需要在两个不同的bean /页面之间传递对象时,在JSF中使用bean和页面时遇到了一些问题。想法是要有一个页面,其中包含所有订单的清单,并可以单击“编辑命令”按钮进行编辑或创建一个新命令。因此,这是orders.xhtml部分,在表单中有一个dataTable:

<p:column style="width:32px;text-align: center">
                            <p:commandButton icon="ui-icon-edit" title="View"
                                action="#{createOrderSelectionView.onEditProductionOrder(productionOrder)}">
                                <f:param name="productionOrder" value="#{productionOrder.pk}"></f:param>
                            </p:commandButton>

这是创建一个新的部分:

f:facet name="left">
                            <p:button value="Nuova bolla" icon="ui-icon-plus"
                                outcome="#{createOrderSelectionView.onNewProductionOrder()}" />
                        </f:facet>

这是目标bean:

    @Named
@RequestScoped
public class CreateOrderSelectionView implements Serializable {

    @Inject
    CompanyController companyController;
    @Inject
    MachineController machineController;
    @Inject
    ProductionOrderController productionOrderController;

    private ProductionOrder productionOrder;
    private DualListModel<Machine> machines;

    @PostConstruct
    public void init() {
        System.out.println(productionOrder == null);
        productionOrder = new ProductionOrder();
        List<Machine> machinesSource = machineController.getAllMachines();
        List<Machine> machinesTarget = new ArrayList<Machine>();
        machines = new DualListModel<Machine>(machinesSource, machinesTarget);
    }

    public String onCompletedSave() {
        productionOrder.setMachines(machines.getTarget());
        productionOrderController.store(productionOrder);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                "La bolla n° " + productionOrder.getOrderNumber() + " è stata correttamente salvata.", null));
        FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
        productionOrder = null;
        return "orders?faces-redirect=true";
    }

    public ProductionOrder getProductionOrder() {
        return productionOrder;
    }

    public void setProductionOrder(ProductionOrder productionOrder) {
        this.productionOrder = productionOrder;
    }

    public String onNewProductionOrder() {
        this.productionOrder = new ProductionOrder();
        productionOrder.setCompany(new Company());
        List<Machine> machinesSource = machineController.getAllMachines();
        List<Machine> machinesTarget = new ArrayList<Machine>();
        machines = new DualListModel<Machine>(machinesSource, machinesTarget);
        return "createProductionOrder?faces-redirect=true";
    }

    public String onEditProductionOrder(ProductionOrder productionOrder) {
        this.productionOrder = productionOrderController.findProductionOrderByPk(productionOrder);
        List<Machine> machinesSource = new ArrayList<>();
        List<Machine> machinesTarget = productionOrder.getMachines();
        System.out.println(machinesTarget.size());
        for (Machine machine : machineController.getAllMachines()) {
            boolean found = false;
            for (Machine machine2 : machinesTarget) {
                if (machine.getPk().equals(machine2.getPk())) {
                    found = true;
                }
            }
            if (!found) {
                machinesSource.add(machine);
            }
        }

        machines = new DualListModel<Machine>(machinesSource, machinesTarget);
        return "createProductionOrder";
    }

    public DualListModel<Machine> getMachines() {
        return machines;
    }

    public void setMachines(DualListModel<Machine> machines) {
        this.machines = machines;
    }

    public List<ShoeSizeAndQuantity> getShoeSizeAndQuantities() {
        /*
         * if (productionOrder.getShoeSizeAndQuantities() == null) {
         * List<ShoeSizeAndQuantity> shoeSizeAndQuantities = new ArrayList<>(); for (int
         * i = 38; i <= 47; i++) { shoeSizeAndQuantities.add(new
         * ShoeSizeAndQuantity(String.valueOf(i), productionOrder));
         * shoeSizeAndQuantities.add(new ShoeSizeAndQuantity(String.valueOf(i + 0.5),
         * productionOrder)); }
         * productionOrder.setShoeSizeAndQuantities(shoeSizeAndQuantities); } return
         * productionOrder.getShoeSizeAndQuantities();
         */
        return new ArrayList<>();
    }

    public void setShoeSizeAndQuantities(List<ShoeSizeAndQuantity> shoeSizeAndQuantities) {
        productionOrder.setShoeSizeAndQuantities(shoeSizeAndQuantities);
    }

    public List<Company> completeTextCompany(String query) {
        String lower = query.toLowerCase();
        List<Company> companies = companyController.getAllClients();
        List<Company> result = new ArrayList<>();
        for (Company company : companies) {
            if (company.getName().toLowerCase().startsWith(lower) || company.getVatNumber().startsWith(lower)) {
                result.add(company);
            }
        }
        return result;
    }

    public boolean canDelete() {
        return productionOrder != null && productionOrder.getPk() != null;
    }

    public String remove() {
        System.out.println(productionOrder.getPk());
        productionOrderController.remove(productionOrder);
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "La bolla n° " + productionOrder.getOrderNumber() + " è stata eliminata correttamente.",
                        "Bolla cancellata!"));
        productionOrder = null;
        return "orders?faces-redirect=true";
    }

}

问题在于没有重定向,我无法将数据传递给新bean。我的代码有问题吗?

0 个答案:

没有答案