JSF (Java Server Face) with the application of rapid development in Web UI

xiaoxiao2021-03-06  60

JSF (Java Server Face) with the application of rapid development in Web UI

Copyright Notice: 9CBS is this BLOG managed service provider. If this paper involves copyright issues, 9CBS does not assume relevant responsibilities, please contact the copyright owner directly with the article Author.

Technical Analysis Report: 2004-09-17

This topic: JSF (Java Server Face) and Application of Rapid Development in Web UI

Author: genstone chye

Java Server Face Introduction

Java Server Face is a framework and solution for rapid development of web applications. He changed the process-response mechanism based on Java web application, using an event-driven processing mechanism. This provides possible possible to quickly develop web applications on the Java platform. The JSF framework simplifies the Web Form validity check, the request parameter resolution, status management, multi-threaded support. Developers simply implement specific event processors and transaction logic (called Handlers and Components in JSF, respectively.

When developing with a JSF framework, no inheritance of any API of the JSF is required, the relationship between the components is implemented through an XML (Faces-Config.xml) configuration file. His implementation is similar to the Spring framework introduced in the past.

Comparison of Java Server Face and MVC Architecture

Different from the current MVC architecture now, JSF does not separate views and logical transactions in design structures, but depends on the specific implementation of programming staff. For example, Handlers and Components can be implemented in a class in the actual development, so that the view layer and the logic processing layer are closely coupled. It is recommended that the program designers should be strictly required to separate Handlers and Components when performing JSF projects.

The following is a comparison of MVC (with Struts as an example) and JSF framework in structure design.

As can be seen from the above figure, the development structure of MVC and JSF is very different. MVC mainly focuses on the development of loose coupling of each layer; JSF is focused on incident processing. From the application, the division of the MVC attached to the structure and JSF is committed to providing a base solution for rapid development. The author recommends that the two architectures cannot be compared, and the actual development and application choice should be combined.

Technology implementation of Java Server Face

The implementation of the JSF framework depends on the following technologies: Java Tag Lib, IOC TYPE2 (SETTER AND GETTER), XML

The representation of the JSF uses TAG LIB technology to process the web object

Associate Form Object and specific Handler and Component using IOC Type2 and XML

Sample of Java Server Face

Example Application Demand Instructions: Implement a user management function, requires the view layer, the logic layer, and the data access layer to reduce the couplings of each layer.

The following figure is an illustration of an example, wherein the data layer uses DAO Hibernate technology.

Userform code:

Package jsfdemo.forms;

/ **

* Created Date: 2004-8-19

* @Author genstone

*

* /

Public Class Userform

{

PRIVATE STRING UserName;

PRIVATE Long UserId;

PRIVATE STRING Password;

/ **

* @Return Returns the username.

* /

Public string getUsername ()

{

Return UserName;

}

/ **

* @Param Username The UserName To Set.

* /

Public void setusername (String username) {

THIS.USERNAME = UserName;

}

/ **

* @Return Returns the password.

* /

Public string getpassword ()

{

Return Password;

}

/ **

* @Param Password The Password to Set.

* /

Public void setPassword (String Password)

{

this.password = password;

}

/ **

* @Return Returns the userid.

* /

Public long getUserid ()

{

Return UserId;

}

/ **

* @Param Userid the userid to set.

* /

Public void setUserid (long userid)

{

THIS.USERID = UserId;

}

}

UserHandler code:

Package jsfdemo.handlers;

Import jsfdemo.components.basecomponent;

/ **

* Created Date: 2004-9-20

* @Author genstone

*

* /

Public class userHandler

{

Private basecomponent component;

/ **

* @Return Returns the Component.

* /

Public BaseComponent getcomponent ()

{

Return Component;

}

/ **

* @Param Component The Component To Set.

* /

Public void setcomponent (BaseComponent Component)

{

this.Component = Component

}

Public void save ()

{

Component.save ();

}

Public void delete ()

{

Component.delete ();

}

Public void modify ()

{

Component.modify ();

}

}

UserComponent code:

Package jsfdemo.components;

Import net.sf.hibernate.hibernateException;

Import jsfdemo.beans.userinfo;

Import jsfdemo.daos.basedao;

Import jsfdemo.forms.userform;

Import jsfdemo.util.valueobejctutil;

IMPORT Util.logger;

/ **

* Created Date: 2004-9-20

* @Author genstone

*

* /

Public Class UserComponent Implements Basecomponent

{

Private userform userform = null;

PRIVATEDAO DAO = NULL;

/ **

* @Return Returns the userform.

* /

Public userform getuserform ()

{

Return Userform;

}

/ **

* @Param Userform The Userform to set.

* /

Public void setUserform (userform userform) {

THIS.USERFORM = Userform;

}

Public boolean save ()

{

UserInfo Info = Valueobejctutil.getPersistObject ((userform) Userform;

Try

{

// Insert Into Database

Info.userid = (long) DAO.SAVE (INFO);

}

Catch (HibernateException E)

{

// Todo Auto-Generated Catch Block

Logger.getLogger (this.getclass ()). Error (E.GetMessage ());

}

// Return Forward Page

Return True;

}

Public Boolean Delete ()

{

UserInfo Info = Valueobejctutil.getPersistObject ((userform) Userform;

Try

{

// Insert Into Database

DAO.DELETE (INFO);

}

Catch (HibernateException E)

{

// Todo Auto-Generated Catch Block

Logger.getLogger (this.getclass ()). Error (E.GetMessage ());

}

// Return Forward Page

Return True;

}

Public boolean modify ()

{

UserInfo Info = Valueobejctutil.getPersistObject ((userform) Userform;

Try

{

// Insert Into Database

Dao.update (Info);

}

Catch (HibernateException E)

{

// Todo Auto-Generated Catch Block

Logger.getLogger (this.getclass ()). Error (E.GetMessage ());

}

// Return Forward Page

Return True;

}

/ **

* @Return Returns the Dao.

* /

Public Basedao getdao ()

{

Return DAO;

}

/ **

* @Param Dao the Dao to set.

* /

Public void setdao (Basedao DAO)

{

THIS.DAO = DAO;

}

}

Userdao code:

Package jsfdemo.daos;

IMPORT Util.logger;

Import net.sf.hibernate.hibernateException;

/ **

* This class has been automaticly generated by Hibernate Synchronizer.

* For more information or documentation, Visit the Hibernate Synchronizer Page

* at http://www.binamics.com/hibernatesync or Contact Joe Hudson at joe@binamics.com.

*

* This is the object class thing relates to the TBL_USER TABLE.

* Any Customizations Belong Here.

* /

Public class userinfodao extends baseuserinfodao {

PRIVATE STRING HibernateConfig = NULL;

Public userinfodao ()

{

}

/ **

* @Return Returns The HibernateConfig.

* /

Public string gethibernateConfig ()

{

Return HibernateConfig;

}

/ **

* @Param HibernateConfig The HibernateConfig to set.

* /

Public void sethibernateConfig (String HibernateConfig)

{

THIS.HIBERNATECONFIG = HibernateConfig;

Try

{

this.initialize (this.hibernateConfig);

}

Catch (HibernateException E)

{

// TODO automatically generates a Catch block

Logger.getLogger (this.getclass ()). Error (E.GetMessage ());

E.PrintStackTrace ();

}

}

}

Profile: /Web-inf/faces-config.xml

user

jsfdemo.forms.userform

session

userdao

jsfdemo.daos.userinfodao

session

HibernateConfig

/Hibernate-config.xml

usercomponent

jsfdemo.components.usercomponent

session

Userform

# {user}

DAO

# {userdao}

userhandler

jsfdemo.handlers.userhandler

session

Component

# {usercomponent}

Profile: /Web-inf/web.xml

PUBLIC "- // Sun microsystems, Inc.//dtd Web Application 2.2 // en"

"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

JavaServerFace Example Application

javax.faces.state_saving_method

Client

Facesservlet

javax.faces.Webapp.Facesservlet

Facesservlet

*. jsf

User_add.jsp code:

<% @ Page ContentType = "text / html; charset = GB2312"%>

<% @ Taglib Uri = "http://java.sun.com/jsf/html" prefix = "h"%>

<% @ Taglib Uri = "http://java.sun.com/jsf/core" prefix = "f"%>

jsf demo </ title></p> <p><meta http-equiv = "content-type" content = "text / html; charSet = GB2312"></p> <p><Style></p> <p>.input {height: 18px; border: solid 1px # 3979bd; Background-Color: # e7f3ff; color: # 000000}</p> <p>.button {</p> <p>FONT-SIZE: 12PX;</p> <p>Border-width: 1px;</p> <p>Border-color: # d4d0c8;</p> <p>CURSOR: POINTER;</p> <p>Background-color: # dee0e0;</p> <p>}</p> <p></ Style></p> <p></ HEAD></p> <p><Body marginwidth = "0" marginheight = "0"></p> <p><f: view local = "z"></p> <p><h: form></p> <p><table width = "645" border = "0" cellpadding = 0 Cellspacing = 0></p> <p><tr Valign = "TOP"></p> <p><TD Width = "50"> </ td></p> <p><TD></p> <p><Table Border = "0"></p> <p><tr></p> <p><TD align = "center" colspan = "2"> <b></p> <p><font size = "4"> Add user </ font> </ b></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TD Width = "92" Valign = "TOP"> User Name </ TD></p> <p><TD> <h: INPUTTEXT VALUE = "# {user.username}" Required = "true" styleclass = "infut" /> </ td></p> <p></ TR></p> <p><tr></p> <p><TD width = "92" Valign = "TOP"> Password </ td></p> <p><TD> <H: INPUTSECRET VALUE = "# {user.password}" Required = "true" styleclass = "infut" /> </ td></p> <p></ TR></p> <p><tr></p> <p><TD colspan = "2" align = "center"></p> <p><h: commandbutton value = "save" action = "# {userhandler.save}" styleClass = "button" /></p> <p></ td></p> <p></ TR></p> <p></ table></p> <p></ td></p> <p></ TR></p> <p></ table></p> <p></ h: form></p> <p></ f: view></p> <p></ Body> </ html></p> <p>note</p> <p>When calling the user_add.jsp file in the browser, pay attention to the configuration call in the JSF's servlet-mapping in the web.xml file, that is, as follows:</p> <p>http://192.168.1.10:8080/jsfdemo/user_add.jsf</p> <p>If servlet-mapping configuration is as follows</p> <p><servlet-mapping></p> <p><servlet-name> Facesservlet </ servlet-name></p> <p><url-pattern> *. Faces </ url-pattern></p> <p></ servlet-maping> Call the JSP page in the browser http://192.168.1.10:8080/jsfdemo/user_add.face</p> <p>The JSF framework will automatically map to the corresponding JSP file</p> <p>The above example uses Eclipse3.0 development, Tomcat4.1.29 J2SDK1.4.1 environmental test runs normally</p> <p>The principle of JSF is described above. In actual development, depending on the IDE used, the process of rapid development of the JSF framework is also different. Today, the tools for providing JSF development are mainly some commercial development tools, such as WebShpere5.12, JBuilder et al. Eclipse's third-party plug-in EXADEL JSF Studio supports JSF development. For Eclipse's Exadel JSF, please refer to http://www.exadel.com/downloads/jsf/userguide/index.html.</p> <p>For WebSphere's JSF development, please refer to the following article: http://www-106.ibm.com/developerWorks/websphere/techjournal/0401_barcia/barcia.html</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-112742.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="112742" 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.036</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 = 'QZJJ2tJEKt6tvqM8Ehw6pMvXmqZeK3XDa_2B8Y3PNBMd09TjoAnzGSJMTOvt8FQVKSM1XOgAo4LlnsUPgx0IprTw_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>