This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JSF and the fly likes Misery with h:selectOneMenu.  PLEASE HELP Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSF
Reply Bookmark "Misery with h:selectOneMenu.  PLEASE HELP" Watch "Misery with h:selectOneMenu.  PLEASE HELP" New topic
Author

Misery with h:selectOneMenu. PLEASE HELP

JSFJSF JSFJSF
Greenhorn

Joined: Apr 24, 2006
Posts: 1
I am using MyFaces and I am getting an empty Dropdown with h:selectOneMenu. I have spent the whole day today trying to get this to work but with no success.... Please HELP!!!

I made sure I converted the department Id which is an int to a String for the SelectItem.

I have been reading/googling...some talk about Converters...Do I really need a converter? If so, why?

PLEASE HELP!!! THANKS MUCH

jsp:
<h:selectOneMenu id="dept" value="#{AccountBean.department}"> <f:selectItems value="#{AccountBean.valuesForDropDown}" />
</h:selectOneMenu>

MANAGED BEAN:

public SelectItem[] getValuesForDropDown() {

Department[] departments = null;

try {
departments = staticContentDAO.getDepartmentsBySiteId(1);
}
catch (WebApplicationException fe) {
fe.printStackTrace();
//Log exception
// Bubble it up as a runtime exception
// send email alert with the stack trace
// send pager alert
}

SelectItem[] valuesForDropDown = new SelectItem[departments.length];

for (int i=0; i <= valuesForDropDown.length; i++) {
valuesForDropDown[i] = new SelectItem(new Integer(departments[i].getDepartmentId()).toString(), departments[i].getDepartmentName());
}
return valuesForDropDown;
}


Department.java
-------------------

private int departmentId;

private String departmentName;

/**
* This method returns the userInfo
* @return
*/
public String getDepartmentName() {
return departmentName;
}

/**
* This method returns userName
* @return
*/
public int getDepartmentId() {
return departmentId;
}

/**
* This method sets the userName
* @param userName
*/
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}


/**
* This method sets the password
* @param password
*/
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;

}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17233
    
    1

"JSFJSF JSFJSF"

Please click on the My Profile link above to change your display name to match the JavaRanch Naming Policy of using your real first and real last names.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17233
    
    1

My question would be is how does JSF know what a SelectItem[] objects are and how to display them. That is what I believe is what a converter will give you, it will convert a SelectItem object into a String.

Or does JSF, by defualt, in these cases always call the toString() method.

Mark
A Saari
Greenhorn

Joined: Mar 31, 2006
Posts: 26
I used an ArrayList of Objects (ProjectsListItem) that extend SelectItem

public class ProjectsListItem extends javax.faces.model.SelectItem implements java.io.Serializable {

private String projID;
private String projName;

/** Creates a new instance of ProjectsListItem */
public ProjectsListItem() {
}

public ProjectsListItem(String myID, String myStr) {
super(myID,myStr);
}

public String getProjID() { return projID; }
public String getProjName() { return projName; }

public void setProjID(String s) { projID = s; }
public void setProjName(String s) { projName = s; }

}

Then in the managed bean

public synchronized java.util.ArrayList getProjectNames() throws SQLException, Exception {
�get the data in a resultset �
projectsList = new java.util.ArrayList();
while( rs.next() ) {
Integer myInt = new Integer(rs.getInt("projectid"));
// Store the projectid as a STRING - if you don't the decode() method
// called to process the response from the browser will not know
// how to convert the value and you'll throw a validation error
ProjectsListItem myObject = new ProjectsListItem(new String(myInt.toString()),rs.getString("projectname").trim());
projectsList.add(myObject);
}
return projectsList;


Hope this helps...

Amy
madhuri madhuri
Ranch Hand

Joined: Jan 18, 2006
Posts: 58
I do not find any mistake in the code. Just check and see if you are getting data from the DB or not.

for (int i=0; i <= valuesForDropDown.length; i++) {
valuesForDropDown[i] = new SelectItem(new Integer(departments[i].getDepartmentId()).toString(), departments[i].getDepartmentName());
}


There is no need to convert departments[i].getDepartmentId() into String. I never converted it to String.


~Madhuri
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14480
    
    7

Originally posted by madhuri madhuri:


There is no need to convert departments[i].getDepartmentId() into String. I never converted it to String.


Usually the BeanUtils subsystem can handle String conversions automatically. It's when you want an off-standard conversion (such as Oracle Date string format to Date Object) that you have to worry about such things.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Misery with h:selectOneMenu. PLEASE HELP
 
Similar Threads
Joining tables
Need HELP with LOOPS
Hibernate mapping no good?
In hibernate Follwowing error occured "Error reading resource: Department.hbm.xml"
composite key and Criteria API [Hibernate]