Ext4 + Java build classic web application background Interface

Source: Internet
Author: User

An article I wrote earlier: extjs4 + Servlet/struts2 + JSON + accordion layout dynamic Ext. tree. panel menu. tree. panel component and ext. tab. the combination of panel components is confusing. Asynchronous loading of Ext. Tree. Panel is also problematic. I wrote this article to share some of my practices and knowledge about ext development. Let's talk about how to build the ext application framework. Let's take a look at it below:

  • On the left side of the graph is a tree. The structure of the tree is obtained from the database asynchronously. The data format is JSON. What is asynchronous? It is to load the parent node instead of the child node, and then load the child node as needed to save a few resources.
  • The figure above shows a very simple application name. The toolbar below shows the logon person, system time, exit button, and other components.
  • The right side of the figure is a tab, which is mainly the user's operation interface. Here, some data of the tree control is used to generate tabs.
This project was extended from my article and added the database. The database is MySQL, and the database is just a table, and the table name is resource: the field here is relatively simple,
  • ID: primary key ID
  • Component: an ext extension class or called page. To put it bluntly, it is the content you want users to see.
  • Descriptio: description, which is business critical and purely redundant.
  • Iconcls: the icon displayed on the tree node
  • Text: name displayed on the tree node
  • Sort: used for sorting
  • Type: resource type, component/url, in the form of a component or URL
  • Parent_id: parent node
  • Leaf: whether it is a root node
This is the data table structure. Now let's look at the JS implementation code:
Ext.Loader.setConfig({enabled : true});Ext.Loader.setPath({'Ext.ux' : 'ext4/ux','Ext.app' : 'ext4/app'});Ext.require(['Ext.app.Portlet', 'Ext.app.PortalColumn', 'Ext.app.PortalPanel','Ext.app.PortalDropZone', 'Ext.ux.TabReorderer','Ext.ux.TabCloseMenu']);

Here we configure the ext Dynamic Loading Function and introduce some required ext classes. If you do not know much about ext dynamic loading, you can refer to the article by Mr. Huang dengqiao: use ext loader in the application. Here we will describe the dynamic loading of ext in detail. I will not describe it here. Next, let's take a look at the overall layout of the application. The entire framework has three components: title, tree on the left, and tab on the right. Let's take a look at the implementation of these components, first read title:

VaR Title = ext. create ("Ext. panel. panel ", {Height: 80, HTML: 'Basic business Platform', Region: 'north', split: True, bbar: [{iconcls: 'icon-user', text: 'postmaster '},'-', {text: ext. date. format (new date (), 'y, M, dday')}, '->', {text: 'exited ', iconcls: 'icon-logout'}], bodystyle: 'background- color: #99bbe8; line-Height: 50px; padding-left: 20px; font-size: 22px; color: #000000; font-family:; font-weight: bolder; '+ 'background:-WebKit-gradient (linear, Left top, left bottom, color-stop (0%, rgba (153,187,232, 0.4 )), color-stop (50%, rgba (153,187,232, 1), color-stop (0%, rgba (153,187,232, 0.4 )))'});

This is title. This component is very simple. It mainly displays the system name of the "basic business platform" and configures related styles. A bbar is defined to display the user name, current time, exit button, and other information. See the tab below:

VaR tab = ext. create ('ext. tab. panel ', {activetab: 0, enabletabscroll: True, animscroll: True, border: True, autoscroll: True, Region: 'center', split: True, items: [{iconcls: 'icon-activity', Title: 'Platform homepage ', xtype: 'portalpanel', layout: 'column', items: [{xtype: 'portalcolumn ', columnwidth: 0.7, items: [{Title: 'News', height: 150, iconcls: 'icon-News'}, {Title: 'latest notification', height: 150, iconcls: 'icon-notic'}, {Title: 'performance report', height: 150, iconcls: 'icon-chart'}]}, {xtype: 'portalcolumn ', columnwidth: 0.3, items: [{Title: 'function link', height: 150, iconcls: 'icon-link'}, {Title: 'backlog ', height: 150, iconcls: 'icon-note'}, {Title: 'email list', height: 150, iconcls: 'icon-email-list'}], plugins: [Ext. create ('ext. UX. tabreorderer '), ext. create ('ext. UX. tabclosemenu ', {closetabtext: 'close panel', closeotherstabstext: 'close others', closealltabstext: 'close all '})]});

Here we have defined a tabpanel, some common configurations, you can go to the official documentation. This Code contains two key parts:

  • We have defined a portalpanel. This is an extended application of Ext, which allows users to typeset. You can see the effect:
  • There are also two plug-ins: Ext. UX. tabreorderer, which is used for label dragging. Ext. UX. tabclosemenu. This is the right-click menu of the tag to close the tab.
Let's take a look at the implementation of the tree panel on the left. You may be familiar with this Code. The key is the accordion layout, so we will not explain it more:
VaR tree = ext. create ("Ext. panel. panel ", {region: 'west', title:" System Menu ", width: 250, iconcls:" icon-Tree ", autoscroll: false, layout: 'accordion ', collapsible: True, layoutconfig: {Animate: true}, split: true });

The following shows the components in the viewport component:

Ext. create ('ext. container. viewport', {layout: 'border', items: [Title, tab, tree], listeners: {afterrender: function () {Ext. getbody (). mask ('loading System menu .... '); Ajax ({URL: "app", // obtain the Panel address Params: {Action: "list"}, callback: addtree });}}});
Through the implementation of this code, you may need to take a look at the layout of Ext. You have noticed that we have registered an afterender event in this Code. The main function of this event is to obtain the System Menu after the component rendering is complete, the Ajax method is to send a request to the background to obtain the data of the tree panel. Let's look at the server implementation. The server side is written in Java:
Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {This. dopost (request, response);} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {string action = request. getparameter ("action"); initheader (response); If (action. equals ("list") {// obtain the rendertext (this. gettreepanellist (), response);} else if (action. equals ("Node") {rendertext (this. gettreenodelist (request. getparameter ("ID"), response) ;}} Public String gettreepanellist () {string SQL = "select T. ID, T. text, T. component, "+" T. description, T. type, T. iconcls, T. sort "+" from resource t where T. parent_id is null "; return basedao. findbysql (SQL ). tostring ();} Public String gettreenodelist (string ID) {string SQL = "select T. ID, T. text, T. component, "+" T. description, T. type, T. iconcls, T. sort, T. leaf "+" from resource t where T. parent_id = '"+ ID +"' "; return basedao. findbysql (SQL ). tostring ();}

The implementation of this Code is to determine whether to obtain the tree panel or tree node through parameters, basedao. the findbysql method converts the returned result set into a list <jsonobject> object. After obtaining the data, it is sent to the page. There are two methods to obtain the data:

  • Gettreepanellist: get the tree panel. The logical judgment here is that there is no data from the parent node.
  • Gettreenodelist: get the tree node and use the parent node to find the child node.
Register a callback function named addtree In the Ajax method to add the tree panel:
Function addtree (data) {Ext. getbody (). unmask (); For (VAR I = 0; I <data. length; I ++) {tree. add (ext. create ("Ext. tree. panel ", {Title: Data [I]. text, iconcls: Data [I]. iconcls, // usearrows: True, autoscroll: True, rootvisible: false, viewconfig: {loadingtext: "loading... "}, store: createstore (data [I]. ID), listeners: {afterlayout: function () {If (this. getview (). el) {var El = This. getview (). el; var table = El. down ("table. x-grid-table "); If (table) {table. setwidth (El. getwidth () ;}}, itemclick: function (view, node) {If (node. isleaf () {If (node. data. type = 'url') {var Panel = ext. create ('ext. panel. panel ', {Title: node. data. text, closable: True, iconcls: 'icon-activity', HTML: '<IFRAME width = "100%" Height = "100%" frameborder = "0" src = "http://www.baidu.com"> </iframe>'}); tab. add (panel); tab. setactivetab (Panel );} Else if (node. data. type = 'component') {var Panel = ext. create (node. data. component, {Title: node. data. text, closable: True, iconcls: 'icon-activity'}); tab. add (panel); tab. setactivetab (panel) ;}}}}); tree. dolayout () ;}} VAR model = ext. define ("treemodel", {// defines the tree node data model extend: "Ext. data. model ", fields: [{name:" ID ", type:" string "},{ name:" text ", type:" string "},{ Name: "iconcls", type: "string" },{ Name: "leaf", type: "Boolean" },{ name: 'type' },{ name: 'component'}]}); vaR createstore = function (ID) {// create a tree panel data source var me = This; return Ext. create ("Ext. data. treestore ", {defaultrootid: ID, // Default Root Node idmodel: model, proxy: {type:" ajax ", // obtain Method URL:" app? Action = node "// get the address of the Tree node}, clearonload: True, nodeparam:" ID "// set the parameter name passed to the background, the value is the ID attribute of the Tree node });};

This code is explained in my article. I will not repeat it here. Let's take a look at the effects of the generated tree panel:

In this way, the tree space is generated. Note that in the code of the addtree method above, we have registered an itemclick event. itemclick will be triggered when the tree node is clicked:
Itemclick: function (view, node) {If (node. isleaf () {// determine whether it is a leaf node if (node. data. type = 'url') {// determine the resource type var Panel = ext. create ('ext. panel. panel ', {Title: node. data. text, closable: True, iconcls: 'icon-activity', HTML: '<IFRAME width = "100%" Height = "100%" frameborder = "0" src = "http://www.baidu.com"> </iframe>'}); tab. add (panel); tab. setactivetab (panel);} else if (node. data. type = 'component') {var Panel = ext. create (node. data. component, {Title: node. data. text, closable: True, iconcls: 'icon-activity'}); tab. add (panel); tab. setactivetab (panel );}}}

The business logic here is to determine whether the clicked node is a leaf node. If it is a leaf node, then determine the node type. If it is a URL, I have done a simple process here, nest Baidu to tab. If it is component, create the corresponding component and add it to the tab component. :

The grid component has several points to note:
  • After creating and adding a component, you must call the setactivetab method of the tab component to activate the component and display it.
  • Ext. Create ('class') method. If this class is defined, ext will be directly created. If ext is not defined, it will be loaded through the configured dynamic loading rules.
  • If you want to call the attributes in node. Data, you must define them in the model used by store. Otherwise, it is an undefined
At this point, we have finished building this platform. We hope you can understand it !!

Http://download.csdn.net/detail/leecho571/4307693 instance download, which has an app. SQL database file, with MySQL Database Import can see the article comment is a virtue, your comment is my biggest motivation !! We look forward to your comments !!

  • Previous Article: ext4 + servlet + HTML5: Real-Time File Upload progress acquisition, local preview, and drag/Drop File Upload album instances
  • Next article: Ext. Form. Field. ComboBox, combined with Servlet and JSON, implements AutoComplete
  • Top
    19
    Step on
    0

    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.