• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Convert birthdate into age in JSF

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I have this college project that is to calculate your BMI. I started learning JSF now, so I'm a newbie in the language.

The problem is in the index page you put your birthdate and in the next page it shows your age.

Index.xhtml



Calculate.xhtml



I found some functions getAge() in google, but with static values and this project works with input values. That's why I can't understand.
 
Bartender
Posts: 20842
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Marcus!

This snippet offends on multiple points:


First, because a JSF backing bean's name should not begin with an upper-case letter. So "bmiData" would be a better choice, although if I'm not mistaken, if you'd left the name to the CDI/JSF bean definition factory default, it would define the name as "bMIdata" Yuck.

Secondaly, because an action isn't logic, It's a reference to a backing bean method. The page doesn't call that method, JSF does the calling. So a more acceptable format would be like this:


The BMI calculate method is invoked by JSF after JSF has validated all the control values on the submitted JSF form and, where applicable, converted them. So, for example, bmiData.data.birthdate would be a property of type java.Util.Date or a compatible datatype and JSF would update it via the data bean's "public void setBirthdate(Date newdate)" method.

Since an action method only fires IF and ONLY IF ALL form properties are valid, and since all form properties have been updated to the backing bean, the age calculation becomes trivial. Just have the action method use data.getBirthdate() to retrieve the entered value and use the date/time manipulation API to compute the difference between now and the entered birthdate.
 
Marcus Vinícius
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome and for the help ! Yeah... I translated it to english, the code is actually in portuguese and didn't realize that it was in upper. But about the rest, thanks, it helped me a lot !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!