JavaServer Faces technology makes web applications easier

zhaozj2021-02-16  53

JavaServer Faces technology makes web applications easier

JavaServer Faces technology is a framework for establishing a web application user interface. It is developed by JAVA Community Process, which is developed by JSR-127 expert group, using JavaServer Faces technology, and different levels of developers can quickly And easy to develop web applications, they only need to assemble reusable UI components on a page, link these components to the application data source, and bound the event processors generated by the client to the server side.

The huge power of JavaServer Faces technology allows web applications to process intricate user interface management on server-side, allowing application developers to focus only on application code.

JavaServer Faces is mainly composed of below:

• A set of APIs: Display UI components and manage their status, process events, and input checks, define pages navigation, support international and access.

· A JSP (JavaServer PagesTM) custom label library to represent the UI component on the JSP page.

This clear programming model and the UI component label library greatly reduces the burden of establishing and maintaining web applications, you can

• Bind the events generated by the Client side to the Server-side application code;

· Map page UI components to Server-side data;

· Build reusable and scalable UI components;

• Save and restore the UI status on the Server request life.

Benefits of JavaServer Faces Technology

· Simple and easy to use: JavaServer Faces is easy to use from its own system. On the one hand, JavaServer Faces technology provides clean separation between behavior and performance, most users, from web designers to component developers, can use this advantage of JavaServer Faces technology, making better divisions and shorter Development cycle. On the other hand, the user interface created by JavaServer Faces technology has handled all intricate interface management, including input check, component status management, page navigation, and event processing.

· Standardization: JavaServer Faces technology is developed under JSR-127 through JCP (Java Community Process), and several famous tool developers are members of the SR-127 expert group, they will be committed to supporting JavaServer Faces in their tools. technology.

· Equipment independence: JavaServer Faces technology is very flexible, in the scalable UI component class only defines the functionality of the component, the JavaServer Faces system allows component developers to extend these component classes, which produce themselves for specific clients. Component tag library.

What is JavaServer Faces app

To a large extent, JavaServer Faces applications are like other Java web applications, they are executed in the Java Serveta container, typical they include:

· Java Bean Components (Being a model object in JavaServer Faces technology) contains features and data for specific applications;

· Event listener;

· Pages, such as JSP pages;

· Server-side assisted class, such as database access bean.

In addition to these, JavaServer Faces applications also contain:

· A custom label library is used to display UI components on the page; • A custom tag library is used to represent event processors, checkors, and other actions;

· UI components, is an object with a state of the Server;

· Checklers, event processors and navigation processors.

Each JavaServer Faces application must include two custom tag libraries, one of which defines the label representing the UI component, and another custom label defines other core operations, such as event processors or verifiers. These two tag libraries are provided by JavaServer Faces.

The component label library eliminates the need for hard coding the UI component or other tag language in HTML, resulting in a complete reusable component, and the core label library makes it easy to register events, checkers and other operations on the component.

The component tag library can be an HTML_BASIC tag library included in the JavaServer Faces technology reference implementation, or you can also define your own label library to deliver custom components or transfer output, not HTML.

Another advantage of the JavaServer Faces application is that the UI components on the page are expressed as state-based objects on the server side, which allows the application to handle the status of the component and bundled the client to the Server end.

Finally, JavaServer Faces technology allows you to translate and check data on separate components, and report errors before Server data is updated.

Role in JavaServer Faces Technology Development

Because JavaServer Faces technology makes each part of the work can be separated, the development and maintenance of JavaServer Faces applications can be quickly and easy to do. A typical development group is listed below. In some groups, some people may act as a variety of characters in the following roles, however, JavaServer Faces technology is still useful for JavaServer Faces technology based on the main responsibilities.

· Page designers use tag language, like html, for web application design pages, when using JavaServer Faces technology frame, page designers are likely to use label libraries only.

· Application developers have written model objects, event processors, checkors, and page navigation controls, and application developers are also some other auxiliary classes.

• Components Developers have user interface programming experience, like to create custom components in a language that can be used directly from the component library, and they can also extend the standard components provided by JavaServer Faces technology.

• Tool providers provide tools that integrate JavaServer Faces technology, making it easy to establish a Server-side user interface.

The main users of JavaServer Faces technology are page designers and application developers, the next part will explain the work of page designers and application developers through a simple application.

A simple JavaServer Faces application

This section describes the process of developing a simple JavaServer Faces application that you will find out which performance of a typical JavaServer Faces application, and which part is responsible for each role during the development process.

Development Process

Develop a simple JavaServer Faces application requires implementation:

· Use the UI component label design page;

· Develop model objects, these model objects will hold data;

· Develop event processors.

These work can be performed simultaneously, or in any order, however, those who perform these work should communicate with each other during development, for example, to access model objects from the page, page developers need to know the name of these model objects. In this example, ask you to guess a number between 1 to 10, the second page tells you whether the guess is correct, this example also checks the legitimacy of the input. Create a page

Creating a page is the responsibility of the page designer. This work includes placing UI components on a page, mapping components to model object data, and adding check tags to components.

This is a greeting.jsp page with verification label:

Hello </ title> </ head></p> <p><% @ Taglib Uri = "http://java.sun.com/jsf/html" prefix = "h"%></p> <p><% @ Taglib URI = "http://java.sun.com/jsf/core" prefix = "f"%></p> <p><body bgcolor = "white"></p> <p><h: graphic_image id = "Wave_img" URL = "/ Wave.Med.gif" /></p> <p><h2> hi. My name is duke.</p> <p>I'm thinking of a number from 0 to 10.</p> <p>Can you guess it? </ H2></p> <p><JSP: usebean id = "usernumberbean"</p> <p>Class = "Guessnumber.Usernumberbean" scope = "session" /></p> <p><f: USE_Faces></p> <p><h: form id = "helloform" formname = "Helloform"></p> <p><h: infut_number id = "userno" NumberStyle = "Number"</p> <p>ModelReference = "UserNumberbean.userNumber"></p> <p><f: validate_longrange minimum = "0" maximum = "10" /></p> <p></ h: input_number></p> <p><h: command_button id = "submit" label = "submit"</p> <p>CommandName = "Submit" /> <p></p> <p><h: Output_errors ID = "ErrorS1" ClientId = "Userno" /></p> <p></ h: form></p> <p></ f: USE_FACES></p> <p></ Html></p> <p>This page shows the performance you will use in most JavaServer Faces applications:</p> <p>· Usebean tag</p> <p>If you know the JSP technology, the JSP: Usebean tab You should be familiar, this label instantiate a Server-side Javabean component, here, the name of the bean is userNumberbean, this bean must be included in your application, instantiate Bean You can access its properties within this page. · Form label</p> <p>The Form tab represents an input form, allowing users to enter some data and submit it to the Server side, usually by clicking a button. The label of those components in the form is nested in the Form tag, which is H: Input_Text and H: command_button.</p> <p>· INPUT_NUMBER tag</p> <p>The input_number tab represents a text domain component, and the user can enter a number. This label is by two attributes: ID and modelreference, ID attributes correspond to the ID of the component object represented by this tab, if you don't include this property, JavaServer Faces will be generated for you. ModelReference points to model object properties.</p> <p>· Validate_longrange label</p> <p>The Input_Text tag also contains a validate_longrange tag, which is one of the JavaServer Faces, which is one of the validation labels. This value must be converted to a long point in an area. The Validate_longrange tag is used by two properties, a specified maximum, a specified minimum, where this tag is used to ensure that the text domain enters must be a number of 0 to 10.</p> <p>COMMAND_BUTTON label</p> <p>The Command_Button label represents a button to submit data input to the text domain.</p> <p>· OUTPUT_ERRORS label</p> <p>Output_errors tags will display an error message. If the data input to the text domain does not meet the rules defined by the verifier, the Output_errors tab is placed on the page will display, the clientID property points to the component that the value check failed.</p> <p>The next part discusses the model object used in this example.</p> <p>Development model object</p> <p>Development model objects are the responsibility of the application developer. Page designers and application developers may need to work together to ensure that the component tag points to the correct object properties, the object properties have the correct properties, and pay attention to some other details.</p> <p>This is the UserNumberbean class, which holds data on the text domain on the greeting.jsp page.</p> <p>Package guessnumber;</p> <p>Import java.util.random;</p> <p>Public class usernumberbean {</p> <p>Integer usernumber = NULL;</p> <p>Integer Randomint = NULL;</p> <p>String response = NULL;</p> <p>Public usernumberbean () {</p> <p>Random randomgr = new random ();</p> <p>Randomint = new integer (randomgr.nextint (10));</p> <p>System.out.println ("Duke's Number:" randomint);</p> <p>}</p> <p>Public void setUsernumber (Integer user_number) {</p> <p>Usernumber = user_number;</p> <p>System.out.println ("SET UserNumber" UserNumber;</p> <p>}</p> <p>Public Integer GetUserNumber () {System.out.Println ("Get UserNumber" UserNumber;</p> <p>Return UserNumber;</p> <p>}</p> <p>Public string getResponse () {</p> <p>IF (usernumber.com == 0)</p> <p>Return "Yay! You Got IT!";</p> <p>Else</p> <p>Return "Sorry," UserNumber "IS INCORRECT."</p> <p>}</p> <p>}</p> <p>As you can see, this bean and any JavaBean components have no separate: It has a set of access methods and each property corresponds to a private data domain, which means you can reference you from your JavaServer Faces page. A specific model object property can be referenced by various component, model object properties can be any basic and reference types, including: Number, String, Int, Double, and Float. JavaServer Faces technology automatically converts data to the type specified by the model object property, and you can apply a converter to the component to convert the value of the component to a type it does not support.</p> <p>In UserNumberbean, the userNumber property is an Integer. When you use the Input_Number Tag Update Model Object Properties, JavaServer Faces implements the ability to convert a string request parameter containing this value into an integer.</p> <p>Handle event</p> <p>Application Developer Another duty is to write event processors for component events and application events. If you select a Checkbox, click on a Button, the application event is submitted to a form (Form).</p> <p>In the simplest multi-page application, you need to define on a page when a form is submitted or accessed, which is to be accessed, the application developer defines the application for the application by defining the ApplicationHandler class.</p> <p>This is BasicApplicationHandler in the Guesnumber example:</p> <p>...</p> <p>Public Class BasicApplicationHandler Implements</p> <p>ApplicationHandler {</p> <p>...</p> <p>FaceSevent InstanceOf formevent {</p> <p>ForMevent ForMevent = (ForMevent) Facesevent;</p> <p>IF (ForMevent.getCommandName (). Equals ("Submit")) {</p> <p>Treeid = "/Response.jsp";</p> <p>}</p> <p>...</p> <p>}</p> <p>}</p> <p>This small code checks if the ForMevent is generated by the submit button of the Greeting.jsp page. If so, the ID of the component tree will be set to the component tree associated with the response.jsp page.</p> <p>The next section will explain how the JavaServer Faces page is processed.</p> <p>The life cycle of the JavaServer Faces page</p> <p>The lifecycle of the JavaServer Faces page is similar, the Client side generates an HTTP request, and the Server end responds in an HTML page, but because JavaServer Faces provide additional performance, throughout the lifecycle, through the implementation of these additional performance, some will provide some Additional services.</p> <p>In the lifecycle, that one is executed depends on whether the JavaServer Faces application has generated requests and whether the transmission phase response in the JavaServer Faces life cycle has been generated. This part first explains the different lifecycle scenes, and then explains every stage in the life cycle with the guessnumber example. Request to handle life cycle scenarios</p> <p>JavaServer Faces applications support two different responses and two different requests:</p> <p>· JavaServer Faces Response: Requests the servlet response created when the response phase during the life cycle is processed.</p> <p>· Non-JavaServer Faces Response: The servlet response created when the response phase is executed, one example is the JSP page without JavaServer Faces components.</p> <p>· JavaServer Faces Request: Servlet request sent by the previously generated JavaServer Faces responded. An example is submitting a form from the JavaServer Faces user interface component, requested URI identifies the JavaServer Faces component tree that processes the request.</p> <p>· Non-JavaServer Faces request: Send to the application's servlet request, such as a servlet or JSP page, not to the JavaServer Faces component tree.</p> <p>These different requests and responses have three possible lifecycle scenarios in the JavaServer Faces application:</p> <p>· Scene 1: Non-JavaServer Faces request to produce a JavaServer Faces response</p> <p>An example of this scene is to click the chain on the HTML page to open a page containing the JavaServer Faces component. From a non-JavaServer Faces request to pass a JavaServer Faces response, the application must map the Facesservlet in the URL to the page containing the JavaServer Faces component, and the FaceSservlet accepts requests and delivers them to the execution of life cycle.</p> <p>· Scene 2: JavaServer Faces requests to generate non-JavaServer Faces responses</p> <p>Sometimes the JavaServer Faces application may need to redirect to different web applications or generate a response that does not contain JavaServer Faces components. In this case, developers must call the FacesContext.ResponseComplete method from the application process to ignore the transfer phase. Render Response. FacesContext contains all information associated with a specific JavaServer Faces request, which can be called at the application request value, the processing check, or update model value phase.</p> <p>· Scene 3: JavaServer Faces request to generate a JavaServer Faces response</p> <p>This is the most common scenario of the JavaServer Faces application. This scenario includes JavaServer Faces components to submit to the JavaServer Faces application using FacesServlet, as requested by the JavaServer Faces implementation, so applications do not require any additional steps, all event listeners, verifiers, and application processors The appropriate phase of the standard life cycle is automatically called. These will be described in the next section.</p> <p>Standard request processing life cycle</p> <p>The standard request processing life cycle is the scene 3 described in the previous section. Most of the JavaServer Faces technology users do not need to care for the request to process life cycles.</p> <p>Figure 2 illustrates the JavaServer Faces request - response lifecycle.</p> <p>Figure 2: JavaServer Faces Request - Response Life Cycle</p> <p>Reconstruction request tree</p> <p>When a JavaServer Faces page request is generated, for example, click on the link or button, JavaServer Faces implements the start to enter the reconstruction component. At this stage, JavaServer Faces implements component tree, bundle event processor, and verifier for establishing a JavaServer Faces page, saving component trees to FacesContext. The component tree in the GRETINUMBER example looks like this in the concept: Figure 3: Guessnumber Component Tree</p> <p>Application request value</p> <p>Once the component tree has been established, each component on the component tree will extract a new value from the request parameter from the request parameter, and this new value will be saved, if this value is converted, an associated to this component The error message will generate and queue on FaceSContext, which will be displayed with the checkpoint of the calibration error information together with the checkpoint.</p> <p>At this stage, if there is an event arrangement, JavaServer Faces implements the listener that will broadcast the broadcast event to an interested.</p> <p>The value of the greeting.jsp page UserNumber component is any value entered in the area, because the model object attribute of the binding component is a shaping type, and the JavaServer Faces implements converts a string to shaping.</p> <p>At this time, the components are set up, and the messages and events have been arranged.</p> <p>Handle verification</p> <p>At this stage, JavaServer Faces implements the verification of all registered to the tree component. It checks the properties of the specified verification rule, compares these rules and saved the local value on the component, if the local value is invalid, JavaServer Faces Adding an error message to FaceSContext. And the lifecycle is directly to the delivery response phase, and the page with error message is once again passed, and if there is a conversion error when applying the request value, these error messages will also be displayed.</p> <p>At this stage, if there is an event arrangement, JavaServer Faces implements the listener that will broadcast the broadcast event to an interested.</p> <p>In the Greeting.JSP page, the JavaServer Faces implements the verifier that handles on the UserNumber Input_text tag, which checks the user input in the text domain is an integer of 0 to 10, if the data is invalid or occurs during the application request value. Conversion errors, processing will jump to the pass response phase, display the check and conversion error message Greeting.jsp page is transferred again.</p> <p>Update model value</p> <p>If the JavaServer Faces implementation determines that the data is valid, it will traverse the component tree and set the local value of the component to the corresponding model object, and only the input component of the ModelReference expression will be updated. If the local data cannot be converted to the type specified by the model object property, the lifecycle will jump directly to the pass response phase, and the page with error message is again passed, similar to the check error.</p> <p>At this stage, if there is an event arrangement, JavaServer Faces implements the listener that will broadcast the broadcast event to an interested.</p> <p>At this stage, the UserNumber property of the userNumberBean is set to the local value of the UserNumber component.</p> <p>Call the application</p> <p>This phase JavaServer Faces implements processing any application-level events, such as submitting forms or links to another page.</p> <p>In the GuessNumber example, the Greeting.jsp page has an application-level event that is associated with the Command component. When processing this event, JavaServer Faces implements which page needs to be displayed according to BasicApplicationHandler, and then generates the response component tree of this new page, and finally, JavaServer Faces implements delivery control to the transfer response phase. Transmission response</p> <p>In the transfer response phase, JavaServer Faces implements the encoding feature that calls the component and passes the component tree that is saved in the Component tree of FacesContext.</p> <p>If an error occurs during the application request phase, processing the checkpoint or update model value phase, the initial page will be passed at this stage. If the page contains an Output_ERRORS tab, any arranging error message will be displayed on the page. If the application contains a customized transmitter, this customizer definition how to pass a component, then new components can be added to the component tree. After the content of the tree is passed, the tree will be saved so that the rear request can access it, and it can be utilized in the reconstruction component.</p> <p>in conclusion</p> <p>JavaServer Faces technology provides a simple programming model that makes it easier to develop web applications, truly embodying the MVC mode, using JavaServer Faces technology, and different levels of developers can quickly and easily develop Web application.</p> <p>Reference</p> <p>JavaServer Faces Technology Website http://java.sun.com/j2ee/javaserverfaces</p> <p>About author</p> <p>Wang Gujun is engaged in software development, and it is very interested in Java technology and is currently engaged in the development of Workflow systems. You can contact him directly through E-mail: Wangguojun@vip.sina.com!</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-25296.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="25296" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.041</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'NpVzGSYLWaQ4usUIYOGjc7btIrAsUOc_2FRdcgDVzBc0DxQab9AhJVV66sIFSJJPAlgXHj3cHoNCKLJJoq'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>