I'm learning a bit of JavaServer Faces. Its paradigm for event handling is quite different from what I've seen in "classical" MVC frameworks like Yii, CodeIgniter, Rails, or even simple PHP.
I was looking for a simple example along the lines of :
If I click a button on a JSF page, it will invoke a method in a "model" class.
This is straight forward in POP (plain ole PHP). Set up an HTML form and bind the form to a PHP script via an ACTION=myscript.php entry in the form.
I found this reference which gives plenty of details about the whole request/response cycle in JSF, but doesn't give me a simple example example of binding a button to a method :
http://onjava.com/pub/a/onjava/excerpt/JSF_chap8/
Here was a simpler reference which met my needs :
http://www.vogella.de/articles/JavaServerFaces/ar01s04.html
It's geared to Eclipse, and I'm using NetBeans at the moment, but the sample is simple enough to work in NetBeans. In NetBeans, just create a basic web application with JavaServer Faces. Next add the TemparaturConvertor class. I put this class under "Source Packages" in the IDE in the same package as SessionBean1.java etc. which the web app wizard kicked out.
For folks like me who feel uncomfortable about all the "magic" the IDE's doing through all those GUI wizards, here's what I found to be the keys to binding the page events (button) to the class which is implementing your model :
First, edit the faces-config.xml and add the following :
<managed-bean> <managed-bean-name>temparaturConvertor</managed-bean-name> <managed-bean-class>devogellajsffirst.TemparaturConvertor</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean>
Next, in your JSP page, in addition to all the other markup from the above link, note the following which binds the "Calculate" button to the celsiusToFahrenheit method of your model class :
<h:commandButton action="#{temparaturConvertor.celsiusToFahrenheit}" value="Calculate"></h:commandButton>
Save the JSP, build and run, and you'll see the button click is bound to your class method. Pretty straight-forward.