According this article, I’ve implemented @ManagedProperty(value="#{settings}") to my backing bean:
BEAN Bde.java:
@Entity
@Table(name="bdeDATA")
@ViewScoped
@ManagedBean(name="BDE")
public class Bde implements Serializable
{
/**/
private static final long serialVersionUID = -705775502999920673L;
@Transient
@ManagedProperty(value = "#{settings}")
private Settings settings;
@Id
private Date create_date;
private Integer person_ID;
public Date getCreate_date() {
return create_date;
}
public void setCreate_date(Date create_date) {
this.create_date = create_date;
}
public Integer getPerson_ID() {
return person_ID;
}
public void setPerson_ID(Integer person_ID) {
this.person_ID = person_ID;
try
{
Settings.PWKITEM = (Pwk)Tools.find(person_ID);
if (Settings.PWKITEM != null) settings.setUserfound(true); /// PROBLEMATIC
}
catch (Exception e)
{
Tools.setErrorMessage("NOT FOUND "+e.getMessage());
}
}
// ManagedProperty settings ---------------------------------------------
public Settings getSettings() {
return settings;
}
public void setSettings(Settings settings) {
this.settings = settings;
}
public void setUserfound (boolean userfound){
settings.setUserfound(userfound);
}
public boolean isUserfound() {
return settings.isUserfound();
}
}
Settings.java:
@SessionScoped
@ManagedBean(name="settings")
public class Settings implements Serializable
{
/**/
private static final long serialVersionUID = 8613411699115714416L;
public static Pwk PWKITEM = new Pwk();
private boolean userfound = false;
public boolean isUserfound() {
return userfound;
}
public void setUserfound(boolean userfound) {
this.userfound = userfound;
}
}
XHTML (ajax call setPerson_ID):
<h:inputText id="persId" value="#{bean.bdeitem.persId}">
<f:ajax event="blur" render="name" execute="@this" />
</h:inputText>
<h:inputText id="name" value="#{bean.pwkitem.name}"/>
Problem is in try/catch:
- without the condition, object is found.
- when I change the condition for example to
if (Settings.PWKITEM != null) System.out.println("HELLO"),HELLOis writen to console. - if i try to add the
userfoundsetter, it is catched (“NOT FOUND”).
What I’m doing wrong?
Your question looks seriously confusing. You first show some bean code and then immediately say “I though that is an ajax problem,”, before even mentioning any kind of problem. The rest of the question is not much different.
To directly answer the last part of your question though:
You are accessing
Settingsstatically. The instance you have declared at the class level seems to be useless. It’s fully possible that ifTools.findthrows an exception and thus no new value is assigned, that there is still an old value in the staticSettings.PWKITEMfield. There is nothing strange about that.Do note that the log reads from top to bottom. So it’s not that “***” is printed and then the exception is thrown, but the exception is first thrown and “Not Found” is printed, and only thereafter “***” is printed.
Additionally, your approach to all of this looks problematic. Declaring an Entity to also be a (JSF) backing bean is rarely a good idea. Using references to some kind of Service or DAO classes from within an entity is also not always a good idea, but doing this in a method that is supposedly a simple setter for an ID simply looks wrong.
Then using static references is even more wrong and to top if off, using underscores in method and non-static variable names goes against the common Java code convention.