Showing posts with label JSF. Show all posts
Showing posts with label JSF. Show all posts

Friday, 12 August 2016

JSF Phase Linteners

In my previous post we learnt about JSF Request Lifecycle, where we studied all JSF request lifecycle phases. By using phase listeners we can get a very deep understanding of JSF request lifecycle.

JSF is a event and listeners based framework. JSF phase listeners are triggered with events fired at start and end of each request lifecycle phases. We can create our custom phase listener to fire events on start and end of each lifecycle phase.

To create a custom phase listener we needs to implements  PhaseListener interface and override its methods - beforePhase, afterPhase and getPhaseId.

Here below is a custom phase listener which I created by implementing PhaseListener interface and overriding its methods.


Custom Phase Listener

After creating our custom phase listener we need to add it to faces-config.xml to tell JSF about it. Below code we need to put in faces-config.xml.


<lifecycle>
<phase-listener>javaknowledgedirectory.listeners.MyPhaseListener</phase-listener>
</lifecycle>

Now we will run the application and check the outputs.
First we will load a page by putting link in browser, and output is -


Here above this was an initial request So After RESTORE_VIEW phase it directly redirected to RENDER_RESPONSE page.

Now we will put some data in text box and submit the button. Below is the output.


In above screen we can se that when we submitted the button, request went through all the phases and our beans method was called in INVOKE_APPLICATION phase.

I hope JSF request lifecycle is very clear. If you have any doubts please put in the comment box , I will try to resolve your queries ASAP.


_________________________________________________
***********************Keep studying***********************

Wednesday, 10 August 2016

First JSF Web Application

In this post I will expalin, How to create your first web application with JSF framework. 

We will use below tools and technologies -
  • JDK8 (To see the JDK installation check my post Java Installation Steps)
  • Eclipse mars (current latest version)
  • JSF 2.2.0
  • Apache Tomcat 8.0
I hope you have above tools installed on your machine and Apache Tomact server is configured in eclipse to deploy the application.

Step 1. Create a  new Dynamic Web Project. File -> New -> Dynamic Web Project.



 Step 2. Enter application name (MyFIrstJSFApp). Select target runtime (Apache tomcat v8.0). Click the modify button after that and it will open a new pop up window to modify the default configuration.




Step 3. In below pop window select Java Server Faces latest version and click OK. It will automatically download the selected JSF jars for your application.



Step 4. That's all. Your basic JSF web application structure is ready and now you need to create create web pages and managed beans. Modify the generated web.xml, add welcome file to add applications default home page.
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
  </welcome-file-list>
Step 5. Create an index.xhtml in MyFirstJSFApp/WebComponent .



Step 6. Create a file hello.xhtml in MyFirstJSFApp/WebComponent .


Step 7. Create a managed bean (using annotation, so no need to do any xml based configuration).



We are done with all the coding and configuration. Now its time to run the application. 

Run The Application -
To run the application Right click on App -> Run As -> Run On Server. Default page index.xhtml will come .



 Put the name in text box and click the button. You will be redirected to the result page with a greeting message.






This is all about first JSF application. Now we can add more pages and managed beans to add more features.

In next posts I will try to focus on JSF in more details. If you have any doubt please put a comment below and I will try to solve it ASAP.

_________________________________________________

***********************Keep studying***********************

Friday, 5 August 2016

JSF Request Life Cycle

Before starting work with JSF (Java Server Faces), the most important thing to understand is JSF request life cycle process. JSF request starts when a user request for a page and ends with rendering the response page. JSF life cycle consist six main phases to complete a request. All these six phases runs in a sequence, but some phases can be skipped based on some specific situations like conversation error, validation error etc. I will explain each phase in details.

JSF life cycle consist six phases -

  1. Restore view phase
  2. Apply request values phase
  3. Process validations phase
  4. Update model values phase
  5. Invoke application phase
  6. Render response phase

Restore View Phase - Restore view phase is the first phase of JSF request life cycle and it starts with a user request when user clicks any button or link. 
Apply request values -  In apply request value phase all values received from request parameters are converted and stored in corresponding UI components. All values are stored locally. This process is called decoding.
Update model values phase - After validation completed now in update model value phase conversion triggered that will convert the UI component values and set the values in corresponding backing bean properties. If any error occurred during this phase directly render response phase will be called with error message. 
Invoke application phase - Now this is the phase where actual user requested task performed. In this phase application events are handled and actual managed beans methods are executed.
Render response phase - This is always the final phase of JSF request life cycle. Outcome will be based on different situations.
So, this is all about JSF request life cycle. 




Below is the detailed explanations of each phase.

JSF controller receives the request and check if view is already present for requested page in faces context than use the same otherwise creates a new view for the page, register the event handlers, validators and saves the view in faces context.
If request is initial than an empty view is created returned back to user directly via render response phase. All other phases are skipped. 
If request is not initial, controller uses the view objects saved in faces context and continues for the next phase.

If some error occurred during conversion of parameter, an error message is generated and stored in faces context. All further phases will be skipped and directly go to render response phase with error message.
If immediate attribute is set true for some component than conversion, validation and events of that components are processed in this phase. I will explain immediate in more details in next section.  

Process validations phase - After apply request values phase all values are locally set in UI components. Now in process validation phase every components value is validated against configured validators. JSF provides a lot of inbuilt validators for Dates, Numbers and String. We can create our custom validators also. 
If any validation fails, validation error message is generated and queued in faces context. All further phases will be skipped and directly go to render response phase with error message.

If no error in this phase all backing bean properties are updated with latest vales and life cycles goes to next phase.

After method execution controller decide the outcome page for the request.

If request is initial a new empty page will be rendered.
If page is already in view and values of components are updated than the page will be re-rendered.
If any conversion or validation error occurred than page will be re-rendered with error messages.

I will write more on immediate attribute and JSF validators in next sections.
_________________________________________________
***********************Keep studying***********************