• Post Reply Bookmark Topic Watch Topic
  • New Topic

Misery with h:selectOneMenu. PLEASE HELP  RSS feed

 
JSFJSF JSFJSF
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!