my dog learned polymorphism*
The moose likes JSF and the fly likes JSF h:selectOneMenu concern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF h:selectOneMenu concern" Watch "JSF h:selectOneMenu concern" New topic
Author

JSF h:selectOneMenu concern

Andriy Burachinskiy
Greenhorn

Joined: Dec 31, 2008
Posts: 17
Hi, guys. Could you please help me with JSF h:selectOneMenu? Huge thanks in advance. I am a newbie to JSF, but I write a tool using it. I Have to extract projectStatuses from Database and display it.
I successfully managed to do it. But when I try to bind the value projectStatus, which will be selected by a user in browser, the method setProjectStatus(String projectStatus) is never invoked.

Here is my Backig Bean for JSF:

public class SubmitReport {
...
private String projectStatus;
...
private ArrayList<SelectItem> projectStatuses;
...

public ArrayList<SelectItem> getProjectStatuses() {
return projectStatuses = extractAllProjectStatusesFromDB();
}
public void setProjectStatuses(ArrayList<SelectItem> projectStatuses) {
this.projectStatuses = projectStatuses;
}

public String getProjectStatus() {
return projectStatus;
}
public void setProjectStatus(String projectStatus) {
this.projectStatus = projectStatus;
}

Here's part of my submit.jsp:

<h:selectOneMenu id="projectStatus" value="#{SubmitReport.projectStatus}"
styleClass="common-dropdown" immediate="true">
<f:selectItems value="#{SubmitReport.projectStatuses}"/>
</h:selectOneMenu>

faces-config.xml is OK, as I successfully retreived the logged in user's projects from DB and displayed them. Please, I NEED help!!
Smitha H Rao
Ranch Hand

Joined: Oct 20, 2007
Posts: 50
This is because you have immediate="true" set. Remove it.


Free Mock tests Interview questions
Andriy Burachinskiy
Greenhorn

Joined: Dec 31, 2008
Posts: 17
Thanks for reply, Smitha. I removed immediate="true", but result is completely same. Besides one more paculiarity:

<h:commandButton value="Submit" id="SubmitCommand" action="#{SubmitReport.submitReport}"
immediate="true"/>

If I remove submit="true", my submitReport() method of SubmitReport backingBean seems never to be invoked.
I appreciate your responses.
Smitha H Rao
Ranch Hand

Joined: Oct 20, 2007
Posts: 50
This was one of the problems I used to face during my initial days of JSF learning.
This means there is some problem occuring during some setter operations. When you are keeping the immidiate="true" attribute, the setter method calling will be skipped so your code runs properly.

Please copy the bean and the JSP page. I will try to figureout the problem.

Thanks
Smitha
Andriy Burachinskiy
Greenhorn

Joined: Dec 31, 2008
Posts: 17
Thanks for helping! Your prompting is very valuable for me.
------------------------------------------------------
-Here is my BackingBean:
------------------------------------------------------
package reportingtool.actions;

import com.sun.jsfcl.app.AbstractPageBean;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.faces.model.SelectItem;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import reportingtool.*;


/**
* This class is primarily intended for automating of processing JSF (JSP) forms &
* values
* @author aburachi
*/
public class SubmitReport extends AbstractPageBean {
private String projectName;
private String projectStatus;
private String week;

private java.sql.Clob repContent;
private String projReportAddNotes;

//the 2 vars highlighted are used in relity
private ArrayList<SelectItem> loggedUserProjects;
private ArrayList<SelectItem> projectStatuses;
private ArrayList<SelectItem> weeks;

public ArrayList<SelectItem> getLoggedUserProjects() {
return loggedUserProjects = extractUserProjectsFromDB();

}
public ArrayList<SelectItem> getProjectStatuses() {
return projectStatuses = extractAllProjectStatusesFromDB();
}
public ArrayList<SelectItem> getWeeks() {
return weeks = extractWeeksFromDB();
}

public void setLoggedUserProjects(ArrayList<SelectItem> loggedUserProjects) {
this.loggedUserProjects = loggedUserProjects;
}
public void setProjectStatuses(ArrayList<SelectItem> projectStatuses) {
this.projectStatuses = projectStatuses;
}
public void setWeeks(ArrayList<SelectItem> weeks) {
this.weeks = weeks;
}

public SubmitReport() {}

public String getProjReportAddNotes() {
return projReportAddNotes;
}
public void setProjReportAddNotes(String projReportAddNotes) {
this.projReportAddNotes = projReportAddNotes;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectStatus() {
return projectStatus;
}
public void setProjectStatus(String projectStatus) {
this.projectStatus = projectStatus;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public java.sql.Clob getRepContent() {
return repContent;
}
public void setRepContent(java.sql.Clob repContent) {
this.repContent = repContent;
}

public static ArrayList<SelectItem> extractUserProjectsFromDB() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Integer usrIdStoredInSession = (Integer)Contexts.getSession().getAttribute("userID_stored");
List userProjectsList = session.createQuery(
"FROM Project as p where p.user = " + usrIdStoredInSession).
list();

ArrayList<SelectItem> usrProjectsSelItems = new ArrayList<SelectItem>(userProjectsList.size());
for (int i =0; i < userProjectsList.size(); i++) {
usrProjectsSelItems.add( new SelectItem(userProjectsList.get(i)) );
}
return usrProjectsSelItems;
}

public static ArrayList<SelectItem> extractAllProjectStatusesFromDB() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
List allProjectStatuses = session.createQuery("FROM ProjectStatus as ps")
.list();

ArrayList<SelectItem> projStatusesSelItems = new ArrayList<SelectItem>(allProjectStatuses.size());
for (int i = 0; i < allProjectStatuses.size(); i++) {
projStatusesSelItems.add( new SelectItem(allProjectStatuses.get(i)) );
}
return projStatusesSelItems;
}
public static ArrayList<SelectItem> extractWeeksFromDB() {
//TODO: consider & implement
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();

//get curWeekNumbInYear
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int aWeek = cal.get(Calendar.WEEK_OF_YEAR);

// currently I extract from DB 2 PREVIOUS weeks => later it should be corrected.
// Here JOIN is needed => check if for current week in WeeklyReport Table
// weekly_rep_send_date is null (it means WeeklyReport, i.e.Email has not
// yet been sent
List weeksList = session.createQuery("FROM Week as w WHERE w.weekId >= '" +
(aWeek - 2) + "' AND w.weekId <= '" + aWeek + "'" ).list();

ArrayList<SelectItem> recentWeeksSelItems = new ArrayList<SelectItem>(weeksList.size());
for (int i = 0; i < weeksList.size(); i++) {
recentWeeksSelItems.add(new SelectItem(weeksList.get(i)) );
}
return recentWeeksSelItems;
}


public String submitReport() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();

WeeklyProjectReport wProjReport = new WeeklyProjectReport();
wProjReport = _initWeeklyProjectReportInstanceFields(wProjReport);

return "submit_success";
}

/**
* @param projName, extracted from JSF (exacter JSP's) SelectItem element
* @return
*/
public Project getProjectByItsName () {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
List queriedProj = session.createQuery("FROM Project as p WHERE p.projName = '" +
this.projectName + "'").list();
System.out.println(queriedProj.size());
Project projectInst = (Project)queriedProj.get(0);
return projectInst;
}

/**
* @param projStatusName, extracted from JSF (exacter JSP's) SelectItem element
* @return
*/
public ProjectStatus getProjectStatusByItsName() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
List queriedProjStatus = session.createQuery("FROM ProjectStatus as ps WHERE " +
"ps.projStatusName = '" + this.projectStatus + "'").list();
ProjectStatus projectStatusInst = (ProjectStatus)queriedProjStatus.get(0);

return projectStatusInst;
}
public Week getWeekByItsBorders() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();

List queriedWeek = session.createQuery("FROM Week as w WHERE w.weekId = '" +
DateHelper.getWeekIdByItsBorders(this.week) + "'").list();
Week weekInstance = (Week)queriedWeek.get(0);
return weekInstance;
}

/**
* Method called only before persisting WeeklyProjectReport instance to DB.
* @param wProjReport is instance, who's fields are to be initialized with values
* from JSF forms
*/
private WeeklyProjectReport _initWeeklyProjectReportInstanceFields(WeeklyProjectReport wProjReport) {

wProjReport.setWProjRepContent(repContent);
wProjReport.setWProjRepNotes(projReportAddNotes );

//projectName is contained in JSF form as SelectItem. SelectItems(projectNames)
//are inserted there from DB
wProjReport.setProject( getProjectByItsName());

//same exactly as projectName(see above)
// wProjReport.setProjectStatus( getProjectStatusByItsName());

// wProjReport.setWeek(this.getWeekByItsBorders());
return wProjReport;
}

}

----------------------------------------
-here is my jsp page:
----------------------------------------
<%--
Document : submit1
Created on : 16.08.2009, 19:15:25
Author : aburachi
--%>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<f:view>
<html>
<head>
<title>Submitting Your project activities report</title>
</head>
<body>
<h:form>
<!-- <td> <% //reportingtool.DateHelper.getPreviousWeeksStartEndDate() %> </td> -->
Project: <!-- <option>Gplus Adapter SAP ICI MultiChannel</option> --> <!--in JSF instead of mixing Java code with HTML(for JSP) we do the following:--> <h:selectOneMenu id="projectName" value="#{SubmitReport.projectName}" styleClass="common-dropdown"> <f:selectItems value="#{SubmitReport.loggedUserProjects}"/> </h:selectOneMenu>
weekly project status: <h:selectOneMenu id="projectStatus" value="#{SubmitReport.projectStatus}" styleClass="common-dropdown"> <f:selectItems value="#{SubmitReport.projectStatuses}"/> </h:selectOneMenu>
through: <h:selectOneMenu id="week" value="#{SubmitReport.week}"> <f:selectItems value="#{SubmitReport.weeks}"/> </h:selectOneMenu>

Type in Your report:



<h:inputTextarea id="repContent" value="#{SubmitReport.repContent}" required="true"
styleClass="input" rows="22" cols="75"/>

Additional notes:



<!--textarea rows="5" cols="75" name="ReportAddNotes"></textarea-->
<h:inputTextarea id="projReportAddNotes"
value="#{SubmitReport.projReportAddNotes}" required="true"
styleClass="input" rows="5" cols="75"/>



<h:commandButton value="Submit" id="SubmitCommand" action="#{SubmitReport.submitReport}"
/>


</h:form>
</body>
</html>
</f:view>

-----------------------------------
-and here's part of faces-config.xml:
-------------------------------------
<managed-bean>
<managed-bean-name>SubmitReport</managed-bean-name>
<managed-bean-class>reportingtool.actions.SubmitReport</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Thank You very much for trying to help me.
Smitha H Rao
Ranch Hand

Joined: Oct 20, 2007
Posts: 50
I think the main problem here is
you do list() on session for some query.
This returns List of Objects.

List userProjectsList = session.createQuery( "FROM Project as p where p.user = " + usrIdStoredInSession). list();

here userProjectsList is List of Objects and you are creating SelectItem directly from it.
ArrayList<SelectItem> usrProjectsSelItems = new ArrayList<SelectItem>(userProjectsList.size());
for (int i =0; i < userProjectsList.size(); i++) {
usrProjectsSelItems.add( new SelectItem(userProjectsList.get(i)) );
}

The value of h:selectOneMenu is a instance of String. I think th eproblem is here. try something like:

ArrayList<SelectItem> usrProjectsSelItems = new ArrayList<SelectItem>(userProjectsList.size());
for (int i =0; i < userProjectsList.size(); i++) {
usrProjectsSelItems.add( new SelectItem(userProjectsList.get(i).toString()) );
}

Let me know if this works.

Thanks
Smitha




Andriy Burachinskiy
Greenhorn

Joined: Dec 31, 2008
Posts: 17
Smitha, I successfully retreive from DataBase and display loggedUserProjects: see
-------------------------------------
<h:selectOneMenu id="projectName" value="#{SubmitReport.projectName}"
styleClass="common-dropdown">
<f:selectItems value="#{SubmitReport.loggedUserProjects}"/>
</h:selectOneMenu>
-------------------------------------

For persisting ProjectReport to DataBase I need projectName (other fields, i.e. projectStatus etc are commented now). When I press "Submit" button in browser, JSF MUST initialize the binded #{SubmitReport.projectName} projectName field of SubmitReport bean and INITIALIZE IT with selected value (if I understand everything right). But this doesn't happen!!! THIS IS THE PROBLEM...

Thanks for helping & your patience.
Smitha H Rao
Ranch Hand

Joined: Oct 20, 2007
Posts: 50
Hi Andriy,

What I wanted to tell in my last post was SubmitReport.projectName is a String object. and SubmitReport.loggedUserProjects is a list of SelectItems which have Object values Not String values.

My suggestion was to input String values into SelectItem() constructor. something like below:
usrProjectsSelItems.add( new SelectItem(userProjectsList.get(i).toString()) );

Can you try this way?
Andriy Burachinskiy
Greenhorn

Joined: Dec 31, 2008
Posts: 17
When I debug application, setProjectName() method is never invoked; only getProjectName()
-----------------------------------
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
--------------------------------------
WHAT MAY BE THE EPROBLEM???
Andriy Burachinskiy
Greenhorn

Joined: Dec 31, 2008
Posts: 17
YES, i TRIED IT DOESN'T HELP..
Smitha H Rao
Ranch Hand

Joined: Oct 20, 2007
Posts: 50


One last suggestion. Try putting <h:messages /> tag inside h:form and submit. If there is any problem it will print it on your page.


Thanks
Smitha
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSF h:selectOneMenu concern