JSF Elements

Ratings:
(4)
Views:638
Banner-Img
  • Share this blog:

To design JSF application we have to use the following elements User interface[View] Web.xml file[Deployment Descriptor] Faces servlet Managed Bean[model1] Facesconfig.xml[Faces configuration file]   Screenshot_7   Loginfrom.jsp <%@taglib usre= “http://Java.sun.com/JSF/Core” prefix=”f”%core tag library <%@taglib usre= “http://Java.sun.com/JSF/ http”prefix=”h”%> <f:view id=”loginview”> <html> <body> <h:form> <h:panel Grid columns=”2”> <h:output text value=”user name”/> <h: input text id=”u name” value =”#. property name u name log cal name”/> <h:output text value=”password”/> <h: input secrete id=”upwd” value=#{log in bean. upwd}”/> <h: command button value=”Log in” action=”#{log in bean, check log in}”/> </h:panel Grid> </h:from> </body > </html> </fi view>   1 - When we click on a button in JSF enabled web page, automatically, action event will be raised, to handle this action event, event handler will, send a request to server 2 - Server will take the request and identify the target resource through web.xml file, in JSF for every request the target resource is faces servlet. 3 - From url pattern (dss) it indicate faces servlet 4 - After getting faces servlet is the target resource through web.xml file, container will  execute faces servlet provided service oriented methods. 5 - upon receiving request from client, faces servlet has to identify the name and location of managed bean component in order to instantiate, for this, faces servlet will go to bases- config.xml file. 6 - After getting the respective managed bean component name and location faces servlet will identify it and it will perform managed bean loading, instantiation and data population[getting request parameter values from request object and storing in managed] 7 - After managed bean instantiation, faces servlet will identify business method from action event and faces servlet will execute that business method 8 - By executing B.method in managed bean, faces servlet is able to get the status value in the form at a strong 9 - Faces servlet has to forward request to a particular JSP page after performing an action in managed bean to identify the target JSP page to forward request, faces servlet will go to faces config.xml file 10 - After getting the target JSP page url from faces config .xml file, faces servlet forward request to target JSP page 11 -After executing the target JSP page, container will dispatch the generated dynamic response to client.   User Interface * The main purpose of user interface in web application is, To improve look and feel the web application To set data from user in order to execute web applications To perform data validations with java script functions To specify different types of requests like GET, POST, Head,…. In general web applications, we will use the technologies, like html, JSP, velocity, free market to prepare user inter face. In case of JSF based web applications, it is not suggestible to use html, tags directly, always it is suggestible to use JSF provided tag library JSF has provided the complete tag library implementation in the form of a jar file i.e-“JSF-api.jar”. If we want to use JSP provided tag library in web application then we have to keep JSF-api. Jar file in web app n lib folder If we want to use JSF tags in JSP tags in JSP page than we have use the following tag lib directives %@taglib =”http://Java.sun.com/JSP/html”prefix=”h”%   Loginform.JSP %@TAGLIB url=”http://java.sun.com/jst/core”prefix=”f”% /html ”prefix=”h”%> <f:view> <html> <body> <h:form> <h:panel Grid column=”2”> <h:out put test value=”username”/> <h:in put text id=”u name” value=”#{login bean u name}”/> <h:out put test value=”password”/> <h:in put secrete id=”u pwd” value=”#{login bean, login }”/> <h:panel Grid>   <f:view>2 it will represent a JSF view <h:panel grid> 2it will represent a grid layout to arrange all the JSF Components. <h: form> 2It is same html<form> tag to represent an unit of GUI Components <h:outputtext>2it will represent a label specified along with ”value” attribute. <h: input text> 2it will represent a text field, where “id” attribute will take component identify to manage components states in component tree where ”value” attribute. Will take an expression to represent a particular property in the respective managed bean  to store the value <h:input secrete>2 It will represent a password field to enter secrete data like passwords, in number where id attribute will take compo not identify to represent a particular property declare d in the respective managed bean in order to store its value, <h:command buttons> 2 it will represent a button to rise action event. Where ”value” attribute will take button label. Where ”action” attribute will represent action event and it able to specify the respective b method at managed bean in order to execute upon button click. 2Request url:- http://local host:-/app1/log in form   Web xml  <web-app> <servlet> < servlet  name faces servile</servlet name> < servlet .class>javax.faces. web app. Faces servlet</servlet class> <load-on-startup>1</load-on-start up> </servlet> <servlet-mapping> <servlet-name>faces servlet </servle -name> </servle -mapping> </web-app>   Deployment Description Deployment description is web.xml file, it will provide meta data or description about our web application required by the container in order to identify and execute server side resources In general, in web application web.xml file can be used to provide the following configuration details Welcome files configuration Display name configuration Context parameter configuration Servlet configuration Filters configuration Listeners configuration Load on startup configuration Session timeout configuration Initialization parameters configuration Error pages configuration Tag library configuration Security configuration   In JSF based web application , web.xml file has to include faces servlet configuration. 2Faces servlet is JSF Provided serviette, it is acting as controller in JSF based web application, to activate this controller we must request faces servlet cfg in web.xml file   Example <Web app> <servlet> < servlet -name>faces servlet<1servket bane> < servlet -class>Javax. faces .web app .faces servlet </servlet-class> <load- on-startup>1</load-on-startup> </servlet > </servlet-mapping > </servlet-name >faces servlet</ servlet-name> <url-pattern>*.dss</url-pattern> <\servlet-mapping> <\web-app> In the above faces servlet configuration, the main requirement of<load-on-startup>configuration is to perform faces servlet loading, instantiation and initialization at the time of servers startup As part of faces servlet initialization, the complete JSF Configurations will be loaded, with this, JSF F/W Will ready at the time of server start up. In general, in web.xml file, we are able to define url patterns fore the servlet in the following 3 approaches

Exact match method[/pattern-name]

Directory match method[/prefix-name/*]

Extension match method[*.extension]

For the faces servlet configuration, we are able to define and pattern either by using director match method or by using extension match method *Possible url patterns fore faces servlet are /faces/*,*,JSF,*Faces,*, only- ext[user defied]   Screenshot_9   Faces servlet In general in MVC based web appl? The mole of the controller is Pick of the request from client Identify model component Perform model component loading instantiation data population Access application logic Identify target view page Forward request to target view page JSF is MVC based web application f/w, it has provided javax.feices webapp faces serve let as. Controller JSF frame work has provided faces servlet as an implementation class to javax. Servlet. Servlet inter face In JSF, FACE SERVLET has provided the implementation for the following methods from servlet interface public void init (servlet config config)throws servlet exception public void service(servlet request , servlet response ) throws servlet exception, ID exception public servlet config get servlet config get() public String Get Servlet Into() public void destroy()   When we start the server the main job of the container is to recognize each and every web application and it will prepare a separate servlet context object for each and every web app. While recognizing web app container will identify web.xml file then container perform web.xml file loading. Parsing and reading the content. If any application level data is identified then container will sore that data in servlet context objects while reading web.xml file content, it container identify “load on startup” configuration under any servlet then containers will perform that servlet loading instantiation and initialization at the time at servlet start up only In JSF based web application, containers will identify startup configuration under faces servlet configuration with this will search for javax faces web app. Faces servlet class file under classes folder if it is not available then container will search for it  all this jar files provided in “lib” folder In the above context container will identify faces servlet under JSF- ape.jar file then container perform face servlet loading instantiation and initialization at the time of server stat up In faces servlet initialization faces servlet will recognize faces config xml file under WEB-INF folder then faces servlet will perform faces – config .xml file loading parsing After faces- config.xml file parsing .faces servlet will prepare “lifecycle” object After the server startup we have to access a page at client browser, we have to provide data in respective fields and we have to submit request from client browser. To server or any hyperlink or by selecting check boxes or radio buttons. Etc When request is submitted to server container will identify the requested resource is faces servlet through web.xml file and container will execute service method of faces servlet When container bypass request to faces servlet, faces servlet will remark a separate object with the present request called as “faces context” object to store the data which is associated with current request. After preparing ”faces context” object container will invoke life cycle object where lifecycle object will process the request by following ”JSF Request processing lifecycle”

Restore view

Apply request values

Process validates

Update model

Invoke application

Render response

  Restore view This phase will check whether the current request is fore any resource or action. If the current request is for any resource then faces servlet will identify the requested resource and by pass the flow of execution directly or render response phase If the present request is for any particular action then faces servlet will perform the following actions Faces servlet will check any ”component free” is existed the present request or not, if any component free is existed then faces servlet will retrieve it It  no component tree is existed w.r.to the present request then Faces servlet has to create new component tree. While preparing component tree Faces servlet will perform the following action Faces servlet will create a root node for <f:view> tag in component tree and child nodes for each and every JSF component Which are available in the respective page Faces servlet will store view id value in “UIVIEW ROOT” Object which is compiling along with request. Faces servlet will store all the  incoming ”locate” provided by client browser in “UIVIEW ROOT” node. if any Validator are configured for UI Components in page then faces servlet will register all the Validator to the components in component tree. If any converters are configured for UI Component in page then faces servlet will register all the converters to the components in component tree. It any listeners are configured fore UI Components in page then faces servlet will register all the listeners to component in component tree. Faces servlet will identify respective model component or Bean component w.r.to the present request through Faces- config.xml file and perform bean loading and instantiation at the time of bean instantiation default values or any initial values will be stored in bean properties these initial values will be set to the respective components in component tree. After preparing component tree, Faces servlet will store other component tree in faces context object and Http Session Object   Apply Request values In this phase faces servlet will take request parameter values from request object and it will set request parameter values to the corresponding components in component tree To set request parameter values to component tree the following method from UI view root:- Public void process Decode()   Process Validator In this phase, faces servlet will perform the following actions: Faces servlet will search fore registered Validator at each and every component in component tree If any Validator is identified at component then faces servlet will execute all the Validator It any validation error messages are available then it will add validation error messages to “Faces context” object. faces servlet will search for the registered convectors at each and every component in component tree It any convertor is identified then faces servlet will execute that convertors If any conversion errors are identified then faces servlet will check any error message is available in faces context object, It any error message is identified then faces servlet will bypass request to “Render Response” phase directly It no error message is identified in faces context object then faces servlet will bypass request to the next phase id “update model” To perform process Validator phase, faces servlet will execute the following method from UI view root class Public void process Validator()   Update model In apply request values phase, all the request parameter values we were set to the components which are available in component tree In this process Validator phase, we have checked weather the data is valid or not After the data validations and conversions the data which is available in component tree is true data, which we have to use in application logic. To use this data in application logic, first we have to set this data to the properties of model component Update model phase will set trustable data from component tree to the model properties. To perform this phase, faces servlet will execute the following method. Public void process updates()   Invoke Application In this phase, faces servlet will perform the following actions. a) Faces servlet will get the registered method name from the components like button, hyperlink, check box, radio button,…from component tree. b) Faces servlet will execute the respective method in the respective managed bean component. c) If no return value is identified from managed bean component method then faces servlet will bypass request to “Render Response” phase to maintain the same page at client browser. d) It any return value is identified from the respective managed Bean component method then faces servlet will identify the target view page from faces – config. Xml file and by pass request to “Render Response” phase to generate response To perform this phase, faces servlet will execute the following method Public void process application()   Render Response phase  In this phase, Faces servlet will convert the respective component tree into the equivalent html or xml or wml and etc formations in order to send response to client browser in html or xml or WML,…formats It the request is coming from “Restore View” phase then render response phase will send the requested resource as response to client. If the request is coming from “Process Validator” phase then render response phase will get all the validation and conversion error messages from “Faces context” Object and send the same page as response to client by embedding error messages. It the request is coming from invoke application phase then render response phase will send the result page as response to client. To perform this phase, faces servlet will execute   Model Component In MVC based web applications, the main purpose at model component is, To manage application B. logic To manage users from data in order to perform data validations and conversions, To manage users from data in order to perform the database operations like insert, update, retrieve,…. To transferee the data from controller layer to view later. In JSF web applications, Managed bean component is acting as model To prepare managed bean component in JSF based web applications, we have to use the following guide lines In JSF, Managed bean component should be a POJO class, it should not be extended or implemented from any predefined library. In JSF based applications, Managed bean class must be public, non abstract and no final All the properties in managed bean class must match the properties name which we defined along with UI Components in user forms We must provide set..()methods and getter..() methods for each and every bean property We have to declare business methods which are registered with the GUI Components like buttons, hyperlinks,… If we want to provide any construction in managed Bean class then that construct fore must be “Public” and “O-argument”. Define application logic by declaring business methods   Example Public class Login bean { Private string u name; Private string u pwd; Public void set u name(string u name) { This. U name = u name; } Public void set upwd(String upwd) { This. Upwd= upwd; } Public string get u name() { Return u name; } Public string get u pwd() { Return u pwd; } Public string log in() { --- logx--- Return “Success”; } }   Faces configuration File The main purpose of  faces configuration file is to provide the following con fig  details request red by faces servlet. Bean component configuration Navigation Rules Validators Convertors Listeners Local configuration. The default name and location of faces configuration file is “Faces config.xml” file under WEB.INF folder In JSF based web application, it is possible to change the name and location of faces configuration file but we have to configure that name and location under web. In the form of a context parameter Here context parameter name is fixed is Javax. faces. CONFIG –FILE <Web - app> <Context. parm> <Param.name>Javax. faces. CONFIG.FILE</paran-bane> <Param. Value>/WEB- INF/classes /my config.xml <Context param ---- <\web.app> In basic JSF application, we need only bean configuration and navigation configurations in faces configuration file 2faces-config> ----- <managed- bean> <managed- bean-name> bean logical name</managed-bean- name> <managed- bean-class> bean fully qualified name  </managed-bean- class > <managed- bean-score>session/application </managed-bean- scope > </managed-bean > <navigation mull> <from-view-id>/form page</from-view-id> <navigation – case> <from- out come> return value from bean method</from – out come> <to- view- id>/ to page </to-view. id> <\navigation-case> --- <\navigation-mule> --- </faces-config>   Faces- config.xml ------------------------ <faces-config> <managed-bean> <managed-bean-name>log in bean</managed-bean-name> <managed-bean-class>com. dss. Log in bean</managed-bean- class > <managed-bean-scope>session</managed-bean- scope > <managed-bean> <Navigation. mule> <from-view-id>/login from.jsp</ from-view-id > <Navigation-case> <from-outcome>success</ from- outcome > <to-view-id>/success.jsp</ to-view-id > </navigation-case> <navigation-case> <from-outcome>failure</ from- outcome > <to-view-id>/failure.jsp</ to-view-id > </navigation-case> </navigation-mule> </faces- config>

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox