Develop web applications with JavaServer Faces (4)

zhaozj2021-02-16  48

6. Event processing.

The next step is to write an event processor program to respond to component events (such as, select a multi-selection option or click a button to submit a form, etc.). For simple applications, you also need to specify which page will be accessed when a form is submitted or accessed. You can implement this interface for ApplicationHandler to achieve this. The following code segment shows an example. In this example, I have to see if the formit button is triggered in index.jsp. If so, the component tree ID is set to the ID of the component tree associated with the Index.jsp page.

Code 5: BasicApplicationHandler.java

Import java.util.sortedMap;

Import javax.faces.facesexception;

Import javax.faces.component.uicomponent;

Import javax.faces.context.facescontext;

Import javax.faces.tree.tree;

Import javax.faces.tree.treefactory;

Import javax.faces.factoryfinder;

Import javax.faces.lifecycle.ApplicationHandler;

Import javax.faces.event.formevent;

Import javax.faces.event.facesevent;

Import javax.faces.event.commandevent;

Import com.sun.faces.riconstants;

Public Class BasicApplicationHandler Implements ApplicationHandler {

Public Boolean ProcesseVent (FacesContext Context, FaceSevent Facesevent) {

IF (! (! (! (! (! "&& formvent &&

! (Facesevent InstanceOf Commandevent)) {

Return True;

}

Boolean ReturnValue = FALSE;

String TreeId = NULL;

FaceSevent InstanceOf formevent {

ForMevent ForMevent = (ForMevent) Facesevent;

IF (ForMevent.getCommandName (). Equals ("Submit")) {

TreeID = "/Hello.jsp";

}

ReturnValue = True;

} else if (FaceSevent InstanceOf CommandEvent) {

CommandEvent CommandEvent = (CommandEvent) Facesevent;

UIComponent C = CommandEvent.getComponent ();

IF (C.GetaTRibute ("Target")! = null) {

TreeId = (string) C.GetaTribute ("Target");

ReturnValue = True;

}

}

IF (null! = treid) {

TreeFactory TreeFactory = (TreeFactory)

FactoryFinder.getFactory (FactoryFinder.tree_Factory);

Context.Settree (TreeFactory.gettree (Context, TreeID);

Return ReturnValue;

}

}

7. Develop the context monitoring program.

If you read the deployment description file web.xml, you will notice that I declare a servlet context monitor program (BasicServletContextListener). When the application is started, the servlet container creates an instance of servlet context listener and calls its conteitialized method; when the app is closed, its ContexTdStroyed method is called. Here is an example of a servlet context listener.

Code 6: BasicServletContextListener.java

Import Javax.Servlet.ServletContextListener;

Import javax.servlet.servletContextevent;

Import javax.faces.factoryfinder;

Import javax.faces.lifecycle.lifecyclefactory;

Import javax.faces.lifecycle.lifecycle;

Import javax.faces.lifecycle.ApplicationHandler;

Public Class BasicServletContextListener IMPLEments ServletContextListener {

Public BasicServletContextListener () {

}

Public void contextinitialized (servletContextevent E) {

ApplicationHandler Handler = New BasicApplicationHandler ();

LifecycleFactory Factory = (LifecycleFactory)

FactoryFinder.GetFactory (FactoryFinder.lifecycle_Factory);

Lifecycle Lifecycle =

Factory.getlifecycle (lifecyclefactory.default_lifecycle);

Lifecycle.SetApplicationHandler (HANDLER);

}

Public void contextdestroyed (servletContext E) {

}

}

8, complete the response page.

When the form in Index.jsp is submitted, the application processor program is started and the user will be forwarded to the response page Hello.jsp. code show as below:

Code Sample 7: Hello.jsp

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><f: USE_Faces></p> <p><h: form id = "responseform" formname = "responseform"></p> <p><H2> Hello, <h: Output_text ID = "Userlabel" Modelreference = "UserNameBean.usename" /> </ h2></p> <p><p></p> <p></ h: form></p> <p></ f: USE_FACES></p> <p></ Html></p> <p><Understanding></p> <p>For the rest: http://www.9cbs.net/develop/read_article.asp?id=18705 Develop web applications with JavaServer Faces (1)</p> <p>Http://www.9cbs.net/develop/read_article.asp?id=18707 Develop web applications with JavaServer Faces (2)</p> <p>Http://www.9cbs.net/develop/read_article.asp?id=18709 Develop web applications with JavaServer Faces (3) http://www.9cbs.net/develop/read_article.asp?id=18712 with JavaServer Faces Development web application (5)</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-26942.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="26942" 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.050</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 = 'aRgC1euszWKT4F8h5RwjTgOSCMcO4iTOhmoXz8yFZ_2FYtYJ5klJ_2B6h0Pw3HGgLKQViNpnF7ajESgU_2FlbXmHkZPQ_3D_3D'; 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>