0

I have the following page xhtml where i have to get some values for populate a DB table. The problem are the selection menu that don't work. Actually, the values of the selections are chosen from the database and are displayed but the values aren't taken when i use the button:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">


<h:head>
<title>Add a Default Package</title>
</h:head>
<h:body>
    <h:form>
        <p:panel header="DefaultPackage Form">
            <h:panelGrid columns="3" id="regGrid">

                <h:outputLabel for="Name">Name:</h:outputLabel>
                <p:inputText id="Name" value="#{addDefaultPackageBean.defpackDTO.name}" />
                <p:message for="Name" />

                <h:outputLabel for="location">Location:</h:outputLabel>
                <p:inputText id="location" value="#{addDefaultPackageBean.defpackDTO.location}" />
                <p:message for="location" />

                <h:selectOneMenu value="#{addDefaultPackageBean.nameFlies}">
                <f:selectItems value="#{addDefaultPackageBean.elelisfly}" var="ElementDTO" itemValue="#{ElementDTO.location}" itemLabel="#{ElementDTO.location}"/>
                </h:selectOneMenu> 

                <h:selectOneMenu value="#{addDefaultPackageBean.nameHotels}">
                <f:selectItems value="#{addDefaultPackageBean.elelishotel}" var="ElementDTO" itemValue="#{ElementDTO.location}" itemLabel="#{ElementDTO.location}"/>
                </h:selectOneMenu> 

        </h:panelGrid>

            <p:commandButton value="Add" update="regGrid" action="#{addDefaultPackageBean.add()}" />
        </p:panel>
    </h:form>
</h:body>

</html>

The image displayed is: As shown in the image the selections are extracted from the db but when i submit with the button their values selected aren't taken

The bean page:

package beans;

import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import elementManagement.ElementMgr;
import elementManagementDTO.ElementDTO;
import DefaultPackageManagement.DefaultPackageMgr;
import DefaultPackageManagementDTO.DefaultPackageDTO;


@ManagedBean(name="addDefaultPackageBean") //come viene richiamato 
@RequestScoped
public class AddDefaultPackageBean {

    @EJB
    private DefaultPackageMgr defpackMgr;
    private DefaultPackageDTO defpackDTO;
    private ArrayList<ElementDTO> elelisfly;
    private ArrayList<ElementDTO> elelishotel;
    private String nameFlies;
    private String nameHotels;
    @EJB
    private ElementMgr elemMgr;



    public AddDefaultPackageBean() {


        defpackDTO = new DefaultPackageDTO();
        defpackDTO.setElem(new ArrayList<ElementDTO>());

    }
    @PostConstruct
    public void init()
    {
        setElelisfly(elemMgr.getAllFlights());
        setElelishotel(elemMgr.getAllHotels()); 
    }





    public String add() {

        this.AssignElemFlyFromSelection();
        this.AssignElemHotelFromSelection();
        defpackMgr.save(defpackDTO);
        return "/employee/index?faces-redirect=true";
    }



    public DefaultPackageDTO getDefpackDTO() {
        return defpackDTO;
    }
    public void setDefpackDTO(DefaultPackageDTO defpackDTO) {
        this.defpackDTO = defpackDTO;
    }
    public ArrayList<ElementDTO> getElelisfly() {
        return elelisfly;
    }
    public void setElelisfly(ArrayList<ElementDTO> elelisfly) {
        this.elelisfly = elelisfly;
    }
    public ArrayList<ElementDTO> getElelishotel() {
        return elelishotel;
    }
    public void setElelishotel(ArrayList<ElementDTO> elelishotel) {
        this.elelishotel = elelishotel;
    }
    public String getNameFlies() {
        return nameFlies;
    }
    public void setNameFlies(String nameFlies) {
        this.nameFlies = nameFlies;
    }
    public String getNameHotels() {
        return nameHotels;
    }
    public void setNameHotels(String nameHotels) {
        this.nameHotels = nameHotels;
    }

    private void AssignElemFlyFromSelection()
    {
        for (ElementDTO elem:this.elelisfly)
        {
            if(elem.getLocation()==this.nameFlies)
            {
                this.defpackDTO.getElem().add(elem);
            }
        }
    }

    private void AssignElemHotelFromSelection()
    {
        for (ElementDTO elem:this.elelishotel)
        {
            if(elem.getLocation()==this.nameHotels)
            {
                this.defpackDTO.getElem().add(elem);
            }
        }
    }



}

Thank you for the help!

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Rotom92
  • 696
  • 1
  • 7
  • 20

2 Answers2

2

You're comparing Java String objects using == operator instead of .equals() method. That causes a comparison between Object references to be performed, instead of implemented String comparison.

elem.getLocation()==this.nameFlies

and

elem.getLocation()==this.nameHotels

Change them for String#equals().

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
0

Add process to your commandobutton.

Like this:

<p:commandButton process="@form" value="Add" update="regGrid" action="#{addDefaultPackageBean.add()}" />
Aritz
  • 30,971
  • 16
  • 136
  • 217
Johan Nordli
  • 1,220
  • 3
  • 13
  • 26
  • still nothing..when i press "Add" nothing happens..maybe i am mistaking on passing the values from the selectionmenu to the bean..i'm new using Primefaces. – Rotom92 Dec 27 '13 at 13:41