Interpreting extremecomponents code Structure--Reprint

Source: Internet
Author: User

Original address: http://blog.csdn.net/lark3/article/details/1937466

Roughly sorted out what was written last year, listed below:


EC is a series of open source JSP custom tags that provide advanced display, currently contains extremetable for displaying data in table Form. The EC version is now 1.0.1, developed by Jeff Johnston, at the website: http://www.extremecomponents.org.
It should be said that Extremecomponents has implemented some of the more complete functions, including sorting, filtering, and now also support AJAX features.
The user can easily implement a list of data by setting the properties of the label (such as Table,row,column, etc.) (most of the attributes are the same as those in the table, TR,TD, and so on in HTML, plus some properties for control). The power of the EC is also due to its good scalability, as users can easily develop it two times to meet a number of special needs.
Since this article is primarily an analysis of the EC code design rather than the use of instructions, how to use the EC can refer to the relevant guidelines and reference documentation. The following is just an example of a release package test.jsp: (Most of the attributes are obvious and are not explained here)

The following is the program code:

<ec:table
items= "Pres"
action= "${pagecontext.request.contextpath}/test.jsp"
Imagepath= "${pagecontext.request.contextpath}/images/table/*.gif"
title= "Test"
Width= "60%"
Rowsdisplayed= "5"
>
<ec:exportxls filename= "Resourcelist.xls" tooltip= "Export Excel"/>
<ec:exportpdf filename= "resourcelist.pdf" tooltip= "Export pdf"/>
<ec:row highlightrow= "true" >
<ec:column property= "ROWCOUNT" cell= "ROWCOUNT" onclick= "alert (' ${pres[0]} ');"
Title= "No." sortable= "false" filterable= "false" Width= "5%"
style= "Text-align:center"/>
<ec:column property= "name" title= "name" href= "#" filtercell= "Droplist"/>
<ec:column property= "nickname" title= "nickname" filterable= "true" sortable= "false"/>
<ec:column property= "term"/>
</ec:row>
</ec:table>

One of the highlights of the EC is the limit interface and its implementation class.
The EC is also very good in terms of the entire software design architecture. The EC is fully object-oriented and fully utilizes the design pattern, and the entire refactoring code is simple and efficient.

1. Code structure

1. 14 First Level packages:

Package org.extremecomponents.table.*; (list)

Package org.extremecomponents.test; (for testing)

Package org.extremecomponents.tree; (tree type, still in development)

Package org.extremecomponents.util; (Tool Class)

Among them, the Htmlbuilder class under the Util package encapsulates the various operations of view output (such as HTML formatting), such as the function table () for outputting an HTML tag <table, which contains a private variable of StringWriter.

The Extremeutils class encapsulates a number of commonly used functions, such as functions FormatDate and FormatNumber.

1. The main content under the 2table package:

(1) package org.extremecomponents.table.bean;

Description: A simple bean class, similar to Vo.

The attributes abstract class contains a private variable for the HashMap, which is used to correlate properties and their values. Class column,export,row,table all inherit attributes and add various attribute variables, such as the style variable in column is the style property of TD in the corresponding HTML. The border variable in table corresponds to the Border property of table in HTML, and so on. Of course, there are some attributes in these beans that are used for control, and the cell variables in column are used to indicate which cell class (in fact, the format used to control the output) is used for the output of the cells, and if cell= "date", Then the output of the cell takes the output of the Datecell class, the date format, and so on.

and Columndefaults,exportdefault,rowdefault,tabledefault. These final classes are used to set some default property values for the corresponding column,export,row,table initialization. These default values are set by the Extremetable.properties file under the core package and are read by the class tableproperties when TableModel is initialized.

(2) package org.extremecomponents.table.calc;

Description: A value that is computed from the values of multiple columns in the properties for the column (calc, and Calctitle). Such as: total value, average, etc.

Calc interface, only defines a function getcalcresult (model,column);

Classes Averagecalc and Totalcalc implement the interface, calculating the average and total values, respectively.

(3) package org.extremecomponents.table.callback;

Description: Used to retrieve, filter, and sort rowset data, called by the Execute method in TableModel.

Three interfaces: Retrieverowscallback, Filterrowscallback, sortrowscallback respectively defined function retrieverows (model), filterrows (model), Sortrows (model) for retrieving, filtering, and sorting data. The default implementation of Filterrowscallback is to get the collection of beans or maps and then filter by implementing the Jakarta predicate interface.

The Processrowscallback class implements these three interfaces and is the default class for retrieving, filtering, and sorting data in the EC. However, the correct implementation of this function is based on the fact that the data obtained by the EC must be any raw data that is unprocessed (i.e. filtered or sorted) in the database, otherwise the results of processing such as filtering or sorting are not correct. Also, the EC can handle data paging, but in reality our data volume is often very large (thousands of), it is impossible to unprocessed all the data to read all the EC to deal with it! Obviously, the process of paging, filtering, sorting, etc. is done in the process of interacting with the database, and the EC simply accepts the processed data and then displays it. The Limitcallback class implements this approach, which also implements the three interfaces above, but only returns data directly. The implementation of this "limit" is discussed again in the limit package.

(4) package Org.extremecomponents.table.cell;

Description: The formatted output for the cell.

The cell interface defines two methods: Getexportdisplay and Gethtmldisplay.

Abstractcell abstract class implements the cell, in fact, it also defines the output framework of the cell, and its inheriting class simply implements the Getcellvalue method. The Gethtmldisplay method is implemented as follows:

Public String Gethtmldisplay (TableModel model, column column) {

Columnbuilder Columnbuilder = new columnbuilder (column);

Columnbuilder.tdstart ();

Columnbuilder.tdbody (Getcellvalue (model, column));

Columnbuilder.tdend ();

return columnbuilder.tostring ();

}

Displaycell inherits the Abstractcell, which is the default cell in EC.

Datecell inherits the Abstractcell, which is used to format cells for the output date.

The Filtercell implements the cell, which outputs an input box for filtering on the head.

The Filterdroplistcell implements the cell, which outputs a drop-down list for filtering on the head.

The Headercell implements the cell, which is used to output the contents of headers in the header.

Numbercell inherits the Abstractcell, which is used to output cells formatted with numbers.

Rowcountcell inherits the Abstractcell, which is used to output the sequence number of the data collection.

The Selectallheadercell implements the cell, which is used to generate a selection box on the head to select all the data.

(5) package org.extremecomponents.table.context;

Description: The encapsulation of the context class used in the EC.

The context interface, which is used to obtain variables for contexts such as application,page,session,request.

The Httpservletrequestcontext implements the context interface.

The Servletrequestcontext implements the context interface.

The Jsppagecontext implements the context interface. The class was used when TableModel was initialized (in Tabletag):

Model = new Tablemodelimpl (new Jsppagecontext (PageContext), tagutils.evaluateexpressionasstring ("locale", This.locale, this, PageContext));

(6) package org.extremecomponents.table.core;

Description: The core package of the EC list, including tabular model, configuration file, properties file, parameter encapsulation, etc.

Registry interface that handles all parameters (), including user-defined.

Abstractregistry abstract class implements the registry, saving some EC internal parameters and user parameters.

Tableregistry inherits the Abstractregistry class, which is called by the addtable function in TableModel.

The messages interface, which supports internationalized display, obtains the correct resource file from local (such as ZH_CN). Implemented by the Tableresourcebundle class in the resource package. Initialize in Tablemodelimp:

Messages Messages = Tablemodelutils.getmessages (this);

Messages.init (context, This.locale);

This.messages = messages;


The preferences interface, which is used to get the setting value in the configuration file.

Tableproperties implements Preferences, initializes the system default configuration file, and then loads the files that are configured by the user, as follows:

public void init (context context, String preferenceslocation) {

try {

Properties.load (This.getclass (). getResourceAsStream (extremetable_properties));

if (Stringutils.isnotblank (preferenceslocation)) {

InputStream input = This.getclass (). getResourceAsStream (preferenceslocation);

if (input! = null) {

Properties.load (input);

}

}

} catch (IOException e) {

if (logger.iserrorenabled ()) {

Logger.error ("Could not load the extremetable preferences.", e);

}

}

}

It is called in Tablemodelimpl:

Preferences Preferences = new Tableproperties ();

Preferences.init (Context, tablemodelutils.getpreferenceslocation (context));

This.preferences = preferences;

In order to set the properties file, you should declare a context-param in the/web-inf/web.xml file as shown in the following example, and specify the path to your properties file:

<context-param>
<param-name>extremecomponentsPreferencesLocation</param-name> <param-value>/org/extremesite/ Resource/extremecomponents.properties</param-value>
</context-param>

The Tablecache class is used to obtain some cached objects, including Cell,state,callback,interceptor, so these classes are singleton and are no longer thread-safe.

The TableModel interface is the core interface of the system, including its implementation class Tablemodelimpl, because they link all the variables in the system.

Tablemodelimpl implements the TableModel, obtains context,preferences and messages instances when initializing, and obtains addtable and registry instances through the Limitfactory,limit function.

The variables defined are: Context,preferences,messages,registry, Tablehandler,rowhandler,columnhandler,viewhandler,exporthandler, Limit,locale and so on.

The variable Currentrowbean saves the currently processed bean and sets the value of the Var variable (the var attribute in the table) in the context to the bean, so that the current bean object can be applied by using the var variable in the row and column tags. Get some meaningful value. As follows:

public void Setcurrentrowbean (Object bean) {

int rowcount = Rowhandler.increaserowcount ();

This.currentrowbean = Bean;

Context.setpageattribute (Tableconstants.rowcount, string.valueof (ROWCOUNT));

Context.setpageattribute (Tablehandler.gettable (). GetVar (), Bean);

}

Collectionofbeans, Collectionoffilteredbeans, Collectionofpagebeans, respectively, save all beans, filtered beans, and the current page's beans.

The Execute function in Tablemodelimpl is called at the first iteration of the label, filtered, sorted, and then Viewhandler.setview () to set the output view.


(7) package org.extremecomponents.table.filter;

Description: Filter, for export when the filter, the realization of javax.servlet.Filter.

(8) package org.extremecomponents.table.handler;

Description: Various processing handles help TableModel to handle the corresponding bean, the associated model and the bean.

Classes are: Columnhandler, Exporthandler, Rowhandler, Tablehandle, Viewhandler.

(9) package org.extremecomponents.table.interceptor;

Description: An interceptor that is used to add and modify the properties of the corresponding bean at run time.

Interfaces are: Tableinterceptor, Rowinterceptor, Columninterceptor, Exportinterceptor.

Users can implement their own interceptor, and then use the Interceptor property in the corresponding tag to set and use it. All interceptor interfaces define an Add method, which is used to handle the properties of the model bean when it was first created. The row and column interceptors also have a modify method that operates on property values when the rows and classes are processed.

(ten) package org.extremecomponents.table.limit;

Description: Encapsulates sorting, filtering, and paging information that is used to pass a limit object to a background program.

Limitfactory interface, limit's factory interface.

The Abstractlimitfactory abstract class implements Limitfactory, which is used to get the export, the current number of pages, the sort fields and values, and the filter set.

Tablelimitfactory inherited the Abstractlimitfactory.

Modellimitfactory also inherited the abstractlimitfactory.

Filter final class, value object, three private variables of type string: Alias, property, value.

The Filterset class contains a filter array.

The limit interface defines some functions for obtaining the limit information, such as sorted values, filter fields and values, and so on.

Tablelimit the final class, the limit is realized. The parameter of its constructor is limitfactory, that is, the value of limit is obtained by the factory class.

Sort final class, value object, three private variables of type string: Alias,property,value.


(one) package org.extremecomponents.table.resource;

Description: Resource files and classes for manipulating resources.

Tableresourcebundle implements the messages interface, which loads specific resource files and user-defined resource files when initialized, and is obtained by defining extremecomponentsmessageslocation values in Web. Xml.

public void init (context context, locale locale) {

This.locale = locale;

Defaultresourcebundle = Findresourcebundle (extremetable_resource_bundle, locale);

String messageslocation = tablemodelutils.getmessageslocation (context);

if (Stringutils.isnotblank (messageslocation)) {

Customresourcebundle = Findresourcebundle (messageslocation, locale);

}

}

(a) package org.extremecomponents.table.state;

Description: Handles the status of the table.

The state interface defines the saveparameters and getparameters two functions.

Abstractstate abstract class, implements the State interface, defines the Saveparameters function.

The Defaultstate class implements the State interface, and the default two functions are empty.


(+) package org.extremecomponents.table.tag;

Description: The Label class is where the EC begins.

Columnstag inherits TagSupport, which is used to generate automatically generated classes.


Columntag inherits the Bodytagsupport and implements the Columninterceptor interceptor.

The first iteration does not generate the view code, but instead:

Column column = new column (model);

Set some properties ...

Addcolumnattributes (model, column);

Model.getcolumnhandler (). AddColumn (column);

After the 2nd iteration begins, the actual view output is executed:

if (column! = null) {//NULL if view not allowed

Object Bean = Tagutils.getmodel (this). Getcurrentrowbean ();

Object propertyvalue = Tablemodelutils.getcolumnpropertyvalue (Bean, property);

Column.setvalue (GetColumnValue (PropertyValue));

Column.setpropertyvalue (PropertyValue);

Modifycolumnattributes (model, column);

Model.getcolumnhandler (). modifycolumnattributes (column);

Model.getviewhandler (). Addcolumnvaluetoview (column);

}

The function code of the last statement is as follows:

public void Addcolumnvaluetoview (column column) {

Cell cell = Tablemodelutils.getcell (column);

Boolean isexported = Model.getlimit (). isexported ();

if (!isexported) {

Column.setcelldisplay (Cell.gethtmldisplay (model, column));

} else {

Column.setcelldisplay (Cell.getexportdisplay (model, column));

}

GetView (). Body (model, column);

}

The output of the view is completed by calling the GetView (). Body () function.

RowTag inherited the TagSupport and implemented the Rowinterceptor.

Similar to Columntag, the first iteration is simply a new row object, and then the property is set and added to the model. However, the RowTag does not produce the output of the view, but rather the first or last column when the Columntag view is output, and if so, the row's view data is output. The following (abstract class Abstracthtmlview:)

public void body (TableModel model, column column) {

if (Column.isfirstcolumn ()) {

Rowbuilder.rowstart ();

}

Html.append (Column.getcelldisplay ());

if (Column.islastcolumn ()) {

Rowbuilder.rowend ();

}

}

Tabletag inherits the TagSupport and implements the trycatchfinally and Tableinterceptor interfaces.

In the doStartTag () function:

Initializes an instance of TableModel as a Tablemodelimpl class, instantiates a table class and sets properties, and finally adds the table to the model by model.addtable (table). The initialization of Tableregistry and Tablelimit is done in the addtable function.

In the Doafterbody () function:

In the Doendtag () function:

Pagecontext.getout (). println (Model.getviewdata ());

The above statement completes the output of the view, and the code for the Model.getviewdata () function is as follows:

Public Object Getviewdata () throws Exception {

Object viewData = Viewhandler.getview (). Afterbody (this);

if (limit.isexported ()) {

Context.setrequestattribute (Tableconstants.view_data, viewData);

Context.setrequestattribute (Tableconstants.view_resolver, Exporthandler.getcurrentexport (). GetViewResolver ());

Context.setrequestattribute (Tableconstants.export_file_name, Exporthandler.getcurrentexport (). GetFileName ());

Return "";

}

return viewData;

}

There are several tag:exportcsvtag, Exportpdftag, Exporttag, Exportxlstag,parametertag and so on.

And the Tagutils class encapsulates several processing functions, such as the use of the Expressionevaluatormanager class to complete the setting of properties?


(+) package org.extremecomponents.table.view;

Description: View section, including html,toolbar,pdf,xsl, etc.

View interface, defines three functions: Beforebody, Body, afterbody.

Abstracthtmlview Abstract class realizes the view, actually also defines the table output frame, inherits the class only then implements the Beforebodyinternal and afterbodyinternal two functions, respectively for the output table header data and the footer data , and its body function is called when the Columntag tag is processed. In the Beforebody function, the abstract class instantiates Htmlbuilder, Formbuilder, Tablebuilder, Rowbuilder, and other classes used to build the corresponding view. such as Formbuilder completion of the HTML form form forms and other parameters such as settings.

Htmlview inherits the Abstracthtmlview class, which is the default view in EC.

(a) package org.extremecomponents.table.view.html;

Description: A class that is used to help view build output, such as columnbuilder,formbuilder,rowbuilder,tablebuilder, such as the Columnbuilder.tdend () function, generates code that is "</td> ”。

The Tableactions class encapsulates some JS action codes, which are mainly used for form actions.


(+) package org.extremecomponents.table.html.toobar;

Description: Toolbar, type: Buttons, characters, graphics, etc.

ToolbarItem interface,

Abstractitem abstract class.

Buttonitem,imageitem,textitem inherited Abstractitem implements the ToolbarItem interface.

Interpreting extremecomponents code Structure--Reprint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.