• Post Reply Bookmark Topic Watch Topic
  • New Topic

welcome  RSS feed

 
Dura Hurtado
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, could I call a method from a jsf file, this way:


<h:outputText value="#{Cliente.welcome}"/>

The method in Cliente file is this way:

public String welcome() {

String result = "";

List l;


l = this.list();

for (int i = 0; i < l.size(); i++) {

Cliente cliente = (Cliente) l.get(i);

if (cliente.getDni().equals(this.getDni())) {
result=cliente.getNombre();
}

}

return result;

}


When I run that app the error which appears is:

javax.el.PropertyNotFoundException: /success.xhtml @24,60 value="#{Cliente.welcome}": The class 'bean.Cliente' does not have the property 'welcome'.

Thanks
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSF expects that you use the proper naming convention. The property name is "welcome" so the getter should be named "getWelcome()" instead of just "welcome()".
 
Dura Hurtado
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , but it is not a property it´s a method.

¿Any idea?

Thanks

 
Bajrang Asthana
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are expecting wrong thing. h:outputText is used for single line formated text. The value attribute of h:outputText expects there should be a property in your backing bean which maps the value attribute/property(in your case this welcome property) that is defined on JSF page. when page is loaded, getter method for the welcome property is called to display output text .

So you need add one property i.e. welcome and getter method for this. If you want to initialize it you can use @postconstruct anotation.


!!Hope it will help you............
 
Dura Hurtado
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi in my bean I have the next method:

public String welcome() {

String result = "";

List l;


l = this.list();

for (int i = 0; i < l.size(); i++) {

Cliente cliente = (Cliente) l.get(i);

if (cliente.getDni().equals(this.getDni())) {
result = cliente.getNombre();
}

}

return result;

}


Then I try to call it from a jsf page, ¿is it posible? ¿Do you know any tag for that?.

Thanks
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My original reply applies. You did not even test it? Why?

I know you do not have a property called welcome but it does not matter. JSF expects to find a property and by normal java conventions you access properties via their setters and getters.
 
Dura Hurtado
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , when I test it the error is:

"#{Cliente.getWelcome()}": Method getWelcome not found

¿Any idea?

Thanks
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason you changed the xhtml/jsp instead of java code

Just use
and
 
Dura Hurtado
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies. It works now.
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to hear.

But in the long run (if you continue to make more complex applications etc) you should do as bajrang asthana suggested and set the values once and use the from there on (using the @postconstruct or some other way). It is not very efficient to always run the for loop (or do any other kind of complex logic) in the setters/getters since internally JSF may call the setters and getters multiple times during the execution. I have seen this behaviour when I have put logging inside a getter (or setter) and a getter may be called as many as ten times altough the value is used only once on page. I do not know the reason for that but since getters/setters should not contain complex logic the multiple calls do not affect performance normally.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!