Hey,
I seem to be having a problem and I need some help sorting it out.
I'm using a single Bean for use with several forms. I know this isn't great but I started the whole thing when I was relatively new to the whole JSF deal. I'll post the bean code, which is getting very long due to the large amount of data processed by the bean.
When the problematic page gets rendered it gets data from the persistence layer and displays it, so far so good. But then when I change or add data to that page the property of the Object referenced by the bean isn't set when I press the add button. I added output lines when the set properties are used to check. So far I've noticed that there aren't any setters used during adding. But when I change from this current page to the next, THEN the objects setter do get used. I'm at a loss to how to solve this.
The idea is to fill in data in the fields backed by the object. This object is referenced by a reference in the bean. Then when I press the add button those fields should be put in the objects properties and added to a selectOneListBox. However when the "add" button is pressed the object does not contain those values.
Look at the code, JSF code first.
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>CV - Work Experience</title>
<link rel="stylesheet" type="text/css" href="../../css/own/main.css" />
</head>
<body>
<f:view>
<h:messages errorClass="errorMessage" infoClass="infoMessage" globalOnly="false" showDetail="true"/>
<br/>
<h:form>
<div class="top_navigation">
<ul>
<li><h:commandLink action="personal"><h:outputText style="font-size:18px" value="Personal Info"/></h:commandLink></li>
<li><h:commandLink action="ict"><h:outputText style="font-size:18px" value="ICT Knowledge"/></h:commandLink></li>
<li id="selected"><h:outputText style="font-size:18px;font-weight:bold" value="Work Experience"/></li>
<li><h:commandLink action="education"><h:outputText style="font-size:18px" value="Education"/></h:commandLink></li>
</ul>
</div>
<div class="content">
<br/>
<h:panelGrid columns="2">
<h:outputText value="Company Name"/><h:inputText value="#{cvbean.experienceData.companyName}" style="width:180px"/>
<h:outputText value="Company Profile"/><h:inputTextarea value="#{cvbean.experienceData.companyProfile}" rows="3" style="width:180px"/>
<h:outputText value="Period"/><h:inputText value="#{cvbean.experienceData.timeFrame}" style="width:180px"/>
<h:outputText value="Project Name"/><h:inputText value="#{cvbean.experienceData.projectName}" style="width:180px"/>
<h:outputText value="Project Description"/><h:inputTextarea value="#{cvbean.experienceData.projectDescription}" rows="6" style="width:180px"/>
<h:outputText value="Role"/><h:inputText value="#{cvbean.experienceData.roles}" style="width:180px"/>
</h:panelGrid>
<br/>
<h:commandButton action="workexperience" binding="#{cvbean.addButton}" immediate="true" rendered="true" actionListener="#{cvbean.addWorkExperience}" value="Add" onchange="this.form.submit();" />
<h:commandButton binding="#{cvbean.updateButton}" immediate="true" rendered="true" actionListener="#{cvbean.updateWorkExperience}" value="Update"/>
<h:commandButton binding="#{cvbean.deleteButton}" immediate="true" rendered="true" actionListener="#{cvbean.deleteWorkExperience}" value="Delete"/>
<h:commandButton binding="#{cvbean.clearButton}" immediate="true" rendered="true" actionListener="#{cvbean.clearWorkExperience}" value="Clear"/>
<br/>
<br/>
<br/>
<h:selectOneListbox id="xplistbox" immediate="true" rendered="true" style="width:310px" size="10" value="#{cvbean.workExperienceID}" valueChangeListener="#{cvbean.workChanged}" >
<f:selectItems value="#{cvbean.workExperience}"/>
</h:selectOneListbox>
<br/>
<h:commandButton action="#{cvbean.writeData}" value="Save"/>
<h:commandButton action="#{cvbean.readData}" value="Load Debug"/>
<h:commandButton action="#{cvbean.writeData}" value="Generate / Save"/>
</h:form>
</div>
</f:view>
</body>
</html>
Now the relevant Bean code.
public class CVBean implements ValueChangeListener //implenting interface has only been added to try and solve the prob, no cigar..
{
private String objectmap;
private String language;
private List<String> levelIDs;
private PersonalData personalData;
private ICTData ictData;
private List<ExperienceData> workExperience;
private ExperienceData experienceData = new ExperienceData();
private Integer workExperienceID=0;
private String languageID;
private EducationData educationData;
private Study study;
private List<GenericData> objectList;
private List<Language> languages;
private Language lang;
private FacesContext context;
private HtmlDataTable myDataTable;
private HtmlCommandButton addButton=new HtmlCommandButton(), updateButton=new HtmlCommandButton(), deleteButton=new HtmlCommandButton(), clearButton=new HtmlCommandButton();
private AuthCheck authcheck = new AuthCheck();
private boolean enabledField=true;
public void processValueChange(ValueChangeEvent vce)
{
System.out.println(vce.getSource()+" In the ValueChangeStotenOverridenMethod");
}
public CVBean()
{
PhaseListenerImpl.setCVBean(this);
this.context=FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext)context.getExternalContext().getContext();
objectmap=(String)servletContext.getInitParameter("objectmap");
personalData=new PersonalData();
ictData=new ICTData();
//experienceData=new ExperienceData();
workExperience=new ArrayList<ExperienceData>();
languages=new ArrayList<Language>();
educationData=new EducationData();
study=new Study();
String username=null;
try {
System.out.println("Username is null, Using repository");
//AuthCheck authcheck = new AuthCheck();
username = authcheck.getCurrentUserName();
this.personalData.setUsername(username);
this.personalData.setFirstName(authcheck.getFirstName());
this.personalData.setName(authcheck.getLastName());
System.out.println("CVBean retrieved username: "+username);
System.out.println("CVBean received: "+this.personalData.getFirstName()+" "+this.personalData.getName());
this.readData(); // this is a persistence retrieval method. It gets serialized objects, builds them and adds them to the beans properties. This works.
//this.refresh();
}
catch(NullPointerException npe)
{
npe.printStackTrace();
}
}
...
public void refresh() {
FacesContext context = FacesContext.getCurrentInstance();
Application application = context.getApplication();
ViewHandler viewHandler = application.getViewHandler();
UIViewRoot viewRoot = viewHandler.createView(context, context.getViewRoot().getViewId());
context.setViewRoot(viewRoot);
context.renderResponse(); //Optional
}
...
public HtmlCommandButton getAddButton()
{
return addButton;
}
public void setAddButton(HtmlCommandButton addButton)
{
this.addButton=addButton;
}
public HtmlCommandButton getUpdateButton()
{
return updateButton;
}
public void setUpdateButton(HtmlCommandButton updateButton)
{
this.updateButton=updateButton;
}
public HtmlCommandButton getDeleteButton()
{
return deleteButton;
}
public void setDeleteButton(HtmlCommandButton deleteButton)
{
this.deleteButton=deleteButton;
}
public HtmlCommandButton getClearButton()
{
return clearButton;
}
public void setClearButton(HtmlCommandButton clearButton)
{
this.clearButton=clearButton;
}
public Integer getWorkExperienceID()
{
System.out.println("getWorkXPID = "+this.workExperienceID);
if(this.workExperienceID==null)
this.workExperienceID=0;
return new Integer(workExperienceID);
}
public void setWorkExperienceID(Integer weID)
{
this.workExperienceID=weID;
if(workExperienceID>=0 && workExperienceID<workExperience.size())
{
experienceData=workExperience.get(workExperienceID);
disableButtons(false);
}
else
{
experienceData=new ExperienceData();
//workExperienceID=-1;
disableButtons(true);
}
System.out.println("setWorkExperienceID: "+experienceData+" "+this.getWorkExperienceID()+" Integer set was: "+weID);
}
public List<SelectItem> getWorkExperience()
{
if(workExperience==null)
{
System.out.println("XP List is null");
List<SelectItem> alt = new ArrayList<SelectItem>();
alt.add(new SelectItem("N/A","N/A"));
return alt;
}
if(workExperience.size()==0)
{
System.out.println("XP List is empty");
List<SelectItem> alt = new ArrayList<SelectItem>();
alt.add(new SelectItem("N/A","N/A"));
return alt;
}
ExperienceData exp=this.experienceData;
List<SelectItem> list=new ArrayList<SelectItem>();
Iterator<ExperienceData> xpIter = this.workExperience.iterator();
Integer integer = 0;
while(xpIter.hasNext())
{
exp=xpIter.next();
Iterator<SelectItem>items = list.iterator();
SelectItem select = new SelectItem(exp.toString(),this.workExperienceID.toString());
boolean alreadyIn=false;
while(items.hasNext())
{
alreadyIn=false;
SelectItem listItem = items.next();
if((select!= null && select.equals(listItem)) && (select.getDescription().equals(listItem.getDescription()) && select.getValue().equals(listItem.getValue()) ))
{
alreadyIn=true;
}
}
if(alreadyIn==false)
{
list.add(new SelectItem(exp.toString(),this.workExperienceID.toString()));
exp.setId(this.workExperienceID);
}
}
if(workExperience.size()==0 || workExperience==null)
{
System.out.println("no WorkXP elements in collection. ");
list.add(new SelectItem("n/a","n/a"));
workExperience.add(new ExperienceData());
return list;
}
for(int i=0; i<workExperience.size(); i++)
{
exp=workExperience.get(i);
list.add(new SelectItem(i,exp.getTimeFrame()+": "+exp.getCompanyName()));
}
return list;
}
public void addWorkExperience(ActionEvent ae)
{
try {
this.refresh();
Iterator<ExperienceData> i = workExperience.iterator();
ExperienceData xpdata;
boolean alreadyIn=false;
while(i.hasNext())
{
xpdata=i.next();
if(alreadyIn=xpdata.equals(experienceData))
{
System.out.println("XPData already exists!");
//throw new NullPointerException();
}
}
if(alreadyIn==false)
{
ExperienceData xpd;
xpd = experienceData.clone();
this.workExperience.add(xpd);
this.setWorkExperienceID(workExperience.size()-1);
this.experienceData=new ExperienceData();
}
System.out.println(this.workExperienceID+" = nieuwe XPCountervalue");
//workExperienceID=workExperience.size()-1;
System.out.println("Work Experience added: "+experienceData+" "+getWorkExperienceID());
//setWorkExperienceID(workExperience.size()-1);
}catch(NullPointerException npe)
{
npe.printStackTrace();
}
}
public ExperienceData getExperienceData()
{
return this.experienceData;
}
public void setExperienceData(ExperienceData experienceData)
{
System.out.println(this.experienceData.toString()+" --XPDataSetter--- "+ experienceData.toString());
this.experienceData=experienceData;
}
public void workChanged(ValueChangeEvent event)
{
Object obj=event.getNewValue();
if(obj!=null && obj instanceof Integer)
{
System.out.println(obj.toString()+" -ValueChangeListener- "+ obj.getClass()+" "+event.getPhaseId());
System.out.println("Object: "+obj);
Integer workExperienceID=((Integer)obj).intValue();
setWorkExperienceID(workExperienceID);
System.out.println("workChanged: "+experienceData+" "+workExperienceID);
}
else
{
System.out.println("Nothing to change");
}
}
...
This is not everything, just the relevant code (I think). The other buttons' methods are not included. First I need to get the add sorted out. And don't mind stupid stuff in the code, I have been trying to get this problem sorted out for several days now and I tried more and more unconventional means over time.
There is a Spring context, the bean is set as managed bean and I tried both request as session for scope. This page is also embedded within another framework called Alfresco, which does not allow me to use JSF extentions that are not supported by its own JSF implementation.
This project is for my graduation thesis, btw.
Edited by: fhomasp on Apr 11, 2008 3:01 AM