XXX System Development Summary (Ssh+jquery Easyui)

Source: Internet
Author: User

I. General introduction of the project

In the previous period of work, I spent about two or three months to develop a Web management information system, using the framework set for Struts2.3.1+spring3.0+hibernate3+jquery EasyUI1.3.5, the system business logic is not complex, complete data collection (in the form of questionnaires), calculation processing and the formation of reports published. Easyui is an excellent JS UI framework that is easy to use and effective, and is a compromise between UI effects and bandwidth speed. The system also has a press release module, using a rich text editor, after comparing a lot of plug-ins, chose Kindeditor, for the simple reason, because it has on-line and easyui combination of examples. In addition, the Jfreechart plugin was used to produce the chart. The system development running environment is windows7 under MYECLIPSE8.5+JDK1.6+TOMCAT6.0+SQLSERVER2008R2.

Second, about Easyui

Easyui is very convenient to use, generally only need to import some files in a page, the other pages are embedded in the current page to buy. Easyui provides a number of components, such as layout, Panel, tabs, DataGrid, and so on, that only need to initialize these components and provide the appropriate data. Each component is declared or created in two ways, using the form of a markup element and the use of JS. Refer to the project source code or the API provided by Easyui website.

Easyui Official website: http://www.jeasyui.com/index.php

Easyui official api:http://www.jeasyui.com/documentation/index.php

Third, about the system architecture

The system applies the regular "three layer architecture", namely the view presentation layer, the business logic layer and the data processing layer. The action of EASYUI+STRUTS2 is responsible for page display, data crawl and push, view process Control, service layer is responsible for the implementation of business logic, DAO layer and hibernate, responsible for data processing, dealing with the database. And spring plays a housekeeper role, its function is IOC container, dependency injection, integration of other plug-ins, etc., responsible for the integration of the entire system, reasonable scheduling.

The following is a brief summary of the design of action, Service, and DAO. Its basic process is that action calls Service,service to invoke DAO, and each layer uses spring's dependency injection to instantiate each layer of properties.

1. Action Layer

The action takes care of receiving the request submitted by the foreground, obtains the request data, and then invokes the service to complete the business process, then forwards the request to the next process (which may be returned to a page or another action) and forwards the corresponding data along with it.

This project first wrote a baseaction, inherited the Struts2 actionsupport. Because the data format received by Easyui is in JSON format, a JSON plug-in is used here Fastjson and a Writejson (Object O) method is provided to write the data to the foreground page for the Easyui component to read when needed.

The other action simply inherits the action. The project takes a model-driven approach to the request parameter, that is, the action implements the Modeldriven interface, which accepts a generic T, where T is typically a data model class, so that the foreground can directly write the properties of a class, and the backend can fetch them and automatically encapsulate them into an object.

Exception handling in the project is done in the action layer, and the service and DAO are only responsible for capturing and throwing up to the top, which is handled at the action. This is not absolute, see the specific requirements.

2. Service

The service layer completes the specific business logic. Call different DAO, etc. according to different conditions.

3. Dao

DAO combines hibernate with a specific database, that is, the persistence of data, or the loading of persisted data objects. Here DAO's design uses the combination of generics and interfaces, first wrote a common interface basedaoi<t> with generics, and the interface defines some common, common methods that either accept or return types are generic. The interface is then implemented, and its generic information remains. In this way, the specific DAO that is associated with a table simply inherits the generic interface when writing the interface, replaces the generic with the entity type, and then writes the implementation to implement just the interface, and inherits the implementation of the common interface. The advantage is that the common functionality is defined in the common interface, and the generics are used to perform a good type of over-and-over and checking without having to cast with an object.

For example, take a user entity-related operation as an example.

(1), public interface

Import Java.io.serializable;import java.util.list;import java.util.map;/** * @author Administrator * * @param <T> * /public interface Basedaoi<t> {/** * Save entity * * @param o * @throws Exception */void Save (T o) throws exception;/** * Delete entity * * @param o * @throws Exception */void Delete (T o) throws exception;/** * Delete data as SQL * @param SQL Delete SQL * @throws E  Xception */void Delete (String sql) throws exception;/** * Update entity * * @param o * @throws Exception */void update (T o) throws  exception;/** * Save or UPDATE * * @param o * @throws Exception */void saveorupdate (T o) throws exception;/** * @param c * @param ID * @return * @throws Exception */t get (class<t> c,serializable ID) throws exception;/** * HQL query without parameters * * @param h QL * Complete HQL statement * @return Result set * @throws Exception */list<t> get (String hql) throws exception;/** * HQL with parameters  Query, which uses a question-mark placeholder and passes parameters in order (not recommended) * * @param hql * HQL statement with parameters * @param params * Well-ordered argument list * @return result set * @throws Exception */lIst<t> Get (String hql, object[] params) throws exception;/** query with parameters, using the ": Parameter name" placeholder and using map to pass parameters (recommended) * @param HQL * HQL statement with parameters * @param params * Map parameter list * @return result set * @throws Exception */list<t> Get (St Ring hql, map<string, object> params) throws exception;/** * Paged query statement with parameters, can query the record of the specified number of bars * * @param hql * Query HQ  L * @param params * parameter list * @param starrow * Query start line (number of hibernate lines starting from 0) * @param size * Query quantity * @return Result set * @throws Exception */list<t> get (String hql, map<string, object> params, int starrow, int size) Throws exception;/** * Paging query statement without parameters, can query the record of the specified number of bars * * @param hql * Query HQL * @param starrow * Query the starting line (Hiber Nate number of rows starting from 0) * @param size * Query quantity * @return result set * @throws Exception */list<t> get (String hql, int starrow , int size) throws exception;/** * Total number of records without parameter query * @param hql query hql (for example, "SELECT COUNT (*) from XXXX") * @return Total number of records * @throws Exception */long GettotaLamount (String hql) throws exception;/** * Total number of records with parameter query * @param hql query hql (for example "SELECT COUNT (*) from XXXX where ...") * @param  Params parameter list * @return Total records * @throws Exception */long gettotalamount (String hql, map<string, object> params) throws exception;/** * Executes a specific HQL statement * @param hql * @return * @throws Exception */int executehql (String hql) throws Exception;}

(2), the implementation of the common interface

Import Java.io.serializable;import java.util.list;import Java.util.map;import Org.hibernate.query;import Org.hibernate.sqlquery;import Org.hibernate.session;import Org.springframework.orm.hibernate3.support.hibernatedaosupport;import Com.pmi.dao.basedaoi;public Class Basedaoimpl<t> extends Hibernatedaosupport implements basedaoi<t> {public void save (T o) throws Exception {ge Thibernatetemplate (). Save (o);} Public list<t> get (String hql) throws Exception {list<t> list = Gethibernatetemplate (). Find (HQL); return list ;} Public list<t> Get (String hql, object[] params) throws Exception {Session session = Gethibernatetemplate (). Getsessi Onfactory (). Getcurrentsession (); Query q = session.createquery (HQL), if (params! = null && params.length > 0) {for (int i = 0; i < Params.leng Th i++) {Q.setparameter (I, params[i]);}} list<t> list = Q.list (); return list;} Public list<t> Get (String hql, map<string, object> params) throws Exception {Session session = Gethibernatetemplate (). Getsessionfactory (). Getcurrentsession (); Query q = session.createquery (HQL), for (String Key:params.keySet ()) {Q.setparameter (key, Params.get (key));} list<t> list = Q.list (); return list;} public void Delete (T o) throws Exception {gethibernatetemplate (). Delete (o); public void Saveorupdate (T o) throws Exception {gethibernatetemplate (). Saveorupdate (o); public void update (T o) throws Exception {gethibernatetemplate (). Update (o); Public list<t> Get (String hql, map<string, object> params, int starrow,int size) throws Exception {Session ses sion = Gethibernatetemplate (). Getsessionfactory (). Getcurrentsession (); Query q = session.createquery (HQL), for (String Key:params.keySet ()) {Q.setparameter (key, Params.get (key));} Q.setfirstresult (Starrow); q.setmaxresults (size); list<t> list = Q.list (); return list;} Public list<t> Get (String hql, int starrow, int size) throws Exception {Session session = Gethibernatetemplate (). Get Sessionfactory (). GetcurrEntsession (); Query q = session.createquery (HQL); Q.setfirstresult (Starrow); q.setmaxresults (size); list<t> list = Q.list (); return list;} Public Long Gettotalamount (String hql) throws Exception {Session session = Gethibernatetemplate (). Getsessionfactory (). Getcurrentsession (); Query q = session.createquery (HQL); Long Count = (long) q.uniqueresult (); return count;} Public Long Gettotalamount (String hql, map<string, object> params) throws Exception {Session session = Gethibernatet Emplate (). Getsessionfactory (). Getcurrentsession (); Query q = session.createquery (HQL), for (String Key:params.keySet ()) {Q.setparameter (key, Params.get (key));} Long Count = (long) q.uniqueresult (); return count;} public void Delete (String sql) throws Exception {Session session = Gethibernatetemplate (). Getsessionfactory (). Getcurrentsession (); SQLQuery q = session.createsqlquery (sql); Q.executeupdate ();} Public T get (class<t> C, Serializable id) throws Exception {return gethibernatetemplate (). Get (c, id);} Public INT EXECUTEHQL (String hql) throws Exception {Session session = Gethibernatetemplate (). Getsessionfactory (). Getcurrentsession (); Query q = session.createquery (HQL); int i = Q.executeupdate (); return i;}}

(3), User entity interface

Import Com.pmi.model.database.tbyhxx;public Interface Userdaoi extends basedaoi<tbyhxx> {}

(4), User entity interface implementation

</pre><pre name= "code" class= "java" >import com.pmi.dao.userdaoi;import com.pmi.model.database.TbYhxx; public class Userdaoimpl extends basedaoimpl<tbyhxx> implements Userdaoi {... (Unique method)}

4. Permission control

Design idea: The project adopts role-based permission control, that is, the user's permissions are differentiated by role. User rights include menu resource permissions, add and check operation permissions. When the user logs in, the system will load the user's menu resources and operation permissions according to the user's role, and the matching operation can be completed.

Implementation of Menu Resource permissions: This logic is relatively simple, the menu for the role assignment is stored in the data table, when the user logs on, the menu item is loaded according to the role.

Operation Permission Implementation: Operation permission refers to the specific addition and deletion of the operation, such as viewing news, add news, delete users and other operations. The Struts2 interceptor is used to control the operation Rights and filter the action layer. After intercepting the requested action and method, it is first determined that a permission check is not required, because not all operations require permission control, such as viewing news. By default all Aciton requests need to be checked, if there is an exception, through the Ignoreactions configuration in Struts.xml. Check the process for, first check whether the login, if not logged in, block the request, and give a prompt, if logged, check the current request operation permissions and the user has a match, the match is released, otherwise block the request, give a hint.

5. Entity model

Generally in the SSH application, a data table corresponding to a solid model, the model not only to complete the related work of persistence, but also to the view layer of the data carrier, this is relatively simple, but also bring some drawbacks, such as in the foreground page, in addition to the model corresponding to the data table of the field, Additional parameters (paging parameters, custom other parameters) are required as attributes, and, for example, when using Hibernate mappings, the general foreign key is mapped to the object that holds the foreign key table corresponding to the model, while on the foreground page, you may not need all the information for this object but only some specific property values. At this point, if all are written in a model, when the completion of the persistence is not clear, for the development of maintenance personnel may cause logic confusion, the use of a two-model mechanism, that is, a table corresponding to two model entities, one is a complete data table field as a property, The other is the corresponding model replicator that is associated with the view layer, which may have more information to be needed by the view layer and complete the two model data replication at the service level. Take advantage of the static copyproperties () method provided by spring's Beanutils class. Take the User information table as an example, the User Information table has the following fields

Model1:hibernate Reverse mapping Auto-generated

public class Tbyhxx implements java.io.Serializable {//fieldsprivate Integer yhid;private tbdyxx tbdyxx;private tbybxx TB Ybxx;private tbjsxx tbjsxx;private String yhname;private string yhpwd;private string yhxm;private string Phone;private St Ring email;private integer yhstate;private integer extend1;private string extend2;private string extend3;private Set tbkz Wjtbs = new HashSet (0);p rivate set Tbwjtbs = new HashSet (0);p rivate set tbggxxes = new HashSet (0);p rivate set tbfxbgs = NE W HashSet (0);p rivate set Tbybkzs = new HashSet (0);p rivate set tbtsxxes = new HashSet (0);p rivate set tbtjxxes = new HashSet (0);p rivate set tbzhzses = new HashSet (0);p rivate Set Tbxwggs = new HashSet (0);p rivate set tbtjjgs = new HashSet (0); Setters and Getters}

Model2: Associated with the view layer, the foreground page

public class User extends Basemodel {//fields//DataGrid required field name private integer yhid;private integer dyid;private String dy Name;private integer qyid;private string qyname;private integer jsid;private string jsname;private string yhname;private String yhpwd;//The user modifies the password information by using the private string Newpwd;private string Yhxm;private string Phone;private string email;private Integer yhstate;private integer extend1;private string extend2;private string extend3;........setters and getters}

As can be seen from the example, the two model still has a certain difference, and it is of practical significance.

6. Log records

Log information can detect the daily operation of the system information, such as user operations and anomalies or error scenarios, a well-designed management information system should have a logging function. This system uses the common log4j plug-in as the log tool. Log4j use is relatively simple, after importing the jar package, add a configuration file. The configuration of this project is shown below.

Log4j.properties:

Log4j.rootcategory=info, stdout, rlog4j.appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=[PMI]%p [%d]%C.%M (%L) | %m%nlog4j.appender.r=org.apache.log4j.dailyrollingfileappenderlog4j.appender.r.file=d\:/pmi_log/pmi_ DAILY.LOGLOG4J.APPENDER.R.LAYOUT=ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.R.LAYOUT.CONVERSIONPATTERN=[PMI] %p \:%m-%c | %t | %d%n



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.