0

I have created a session bean but it seems my <h:selectOneMenu> is not storing the value in the bean. Can someone tell me what I am doing wrong?

In the xhtml navigates to another xhtml that displays the results. I do not have any values in faces-config.xml other than the navigation rules since I think managed bean / session bean annotations can replace that

xhtml:

<!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">
       <!-- Using a custom header -->
       <ui:include src="/resources/ADTHeader.xhtml" />
    <h:body>
        <h1>Test</h1>
        <h:form>
        <h:selectOneMenu id="mypick"
            value="#{gridMaster_backing.pickedGrid}" 
            converter="#{categoryConverter}"
            title="ADTF" >
            <f:selectItems value="#{gridMaster_backing.gridList}" var="prog" itemValue="#{prog.gridid}" itemLabel="#{prog.gridid} - #{prog.program} - #{prog.project} - #{prog.ci}" />
        </h:selectOneMenu>
        <br /><br />
        <h:button  value="View Grid" outcome="result" /> 
        </h:form>
    </h:body>
</html>

Session bean:

package edu.adtf.web;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.ejb.EJB;

import com.ray.adtf.ejb.*;
import com.ray.adtf.jpa.Gridmaster;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@ManagedBean
@SessionScoped
public class gridMaster_backing {

    @EJB
    private GridMasterBean ejb;
    private Collection<Gridmaster> mgrid;

    private List gridList = new ArrayList();

    // pickedGrid holds value picked from the gridid drop down list
    private Long pickedGrid; 

    public Long getPickedGrid() {
        //System.out.println("getPicked Grid");
        return pickedGrid;
        //return (long) 100;
    }

    public void setPickedGrid(Long pickedGrid) {
        this.pickedGrid = pickedGrid;
    }

    // returns data to show grid form
    public Collection<Gridmaster> getGridmaster(Long vgridid){
        mgrid = ejb.getAllGrids(vgridid);
        return mgrid;
    }

    public void setGridList(List gridList) {
        this.gridList = gridList;
    }

    // list for grid list drop down
    public List getGridList() {
        List gridList2 = ejb.getDisplayGridList();
        return gridList2; 
        }
    }

converter:

package edu.adtf.web;
import javax.faces.bean.ManagedBean;
import edu.adtf.jpa.Gridmaster;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@ManagedBean(name = "categoryConverterBean") 
@FacesConverter(value = "categoryConverter")
public class CategoryConverter implements Converter{
    @PersistenceContext
    private transient EntityManager em;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        //System.out.println(em.find(Gridmaster.class, new Long(value)));
        return em.find(Gridmaster.class, new Long(value));
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        Gridmaster gridmaster;
        gridmaster = (Gridmaster) value;
        //System.out.println(String.valueOf(gridmaster.getGridid()));
        return String.valueOf(gridmaster.getGridid());

    }
}
GMan1973
  • 179
  • 3
  • 5
  • 26

1 Answers1

2

The problem is in the converter you're using. The field of your managed bean from where you're getting/setting the data is of type Long and the default value for <h:selectOneMenu> is of type String, so you only need to use a Long converter. There's a built-in converter for this case: javax.faces.Long. Make sure you're defining that the itemValue for the <h:selectItems> is type Long as well.

The code should look like this:

<h:selectOneMenu id="mypick"
    value="#{gridMaster_backing.pickedGrid}" 
    converter="javax.faces.Long"
    title="ADTF">
    <!-- #{prog.gridid} must return Long -->
    <f:selectItems
        value="#{gridMaster_backing.gridList}"
        var="prog"
        itemValue="#{prog.gridid}"
        itemLabel="#{prog.gridid} - #{prog.program} - #{prog.project} - #{prog.ci}" />
</h:selectOneMenu>
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I getting this error from eclipse kepler on the long converter. I am sure I am missing a library or something but I am not sure which one: Caused by: javax.faces.FacesException: Expression Error: Named Object: javax.faces.converter.Long not found. How can I add this converter ? – GMan1973 Feb 16 '15 at 18:37
  • @GMan1973 sorry, this is the correct name: `javax.faces.Long`. Post updated. – Luiggi Mendoza Feb 16 '15 at 18:42
  • This is the code I have in my results form but I do not see the value there: Not sure what is wrong...... – GMan1973 Feb 16 '15 at 19:04
  • 15:03:23,399 INFO [stdout] (default task-59) getPicked Grid null 15:03:29,843 INFO [stdout] (default task-60) getPicked Grid null 15:03:42,198 INFO [stdout] (default task-61) getPicked Grid null – GMan1973 Feb 16 '15 at 20:04
  • @GMan1973 make sure you initialize the fields properly. That error is from basic JSF and not directly related to the question. – Luiggi Mendoza Feb 16 '15 at 20:05
  • Hi Luiggi, I am not sure what you mean. The grid null if from a System.out.println("Picked Grid" + pickedGrid); I added to the bean. I am not sure why the bean seems not to get the value....... – GMan1973 Feb 16 '15 at 20:08
  • 1
    @GMan1973 I found your problem. You're reinitializing the list/collection or whatever on every call in the `getGridList`. You have a bigger issue there. Please read [Why JSF calls getters multiple times](http://stackoverflow.com/q/2090033/1065197) where this subject is analyzed and solved. – Luiggi Mendoza Feb 16 '15 at 20:10