The simplest implementation of winter framework source code SSH

Source: Internet
Author: User

I have been writing this framework the other day and now I am releasing the source code!

Main functions:

1. Automatically encapsulate the form into an object (similar to struts2)

2. automatically generate addition, deletion, modification, and query SQL statements based on the object (similar to hibernate)

3. Support spring dynamic injection. You can hand over custom actions to spring for management.

4. Custom tab Library

5. Support for pseudo-static functions

Pseudo-static implementation. You can use a regular expression !~

This framework is almost the most streamlined implementation of SSH.

The configuration is flexible and simple. It is much easier than the three frameworks, and there is only one package, which is very small!

Packages and classes:

Org. Pan. Code this is the package of the core class

Main classes:

Filter-forwarding controller-Action Manager-field type converter-spring support-JDBC support-SQL Creation

The main configuration file is request. xml.


Baidu Network Disk: http://pan.baidu.com/share/link? Consumer id = 467157 & UK = 470382596

Q: 599194993

If you are interested, contact me and complete it together !~

Some source code is attached:

The core code is Action Manager:

Package Org. pan. code; import Java. lang. reflect. field; import Java. lang. reflect. invocationtargetexception; import Java. lang. reflect. method; import Java. util. hashmap; import Java. util. iterator; import Java. util. map; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. pan. convetor. fieldconvertor; import Org. pan. exception. initializeactionexception; import Org. pa N. support. servletactioncontent; import Org. pan. util. methodsuitl; import Org. pan. util. stringuitl;/*** request data calls the corresponding class for processing * @ author Pan **/public class actionmanage {private httpservletrequest request; // The request object private httpservletresponse response; // response object private Map <string, Object> map = new hashmap <string, Object> (); // map object private string result; // return type private string classpath; // class path private string me Thodname; // method to be operated private object bean; // the instance of the object can be the public void setmethodname (string methodname) obtained from spring {This. methodname = methodname;} Public String getmethodname () {return methodname;} Public String getresult () {return result;} private Map <string, Object> getmap () {return map ;} public actionmanage (Object bean, httpservletrequest request, httpservletresponse response, string classpath, string methodnm E) {This. request = request; this. response = response; this. classpath = classpath; this. methodname = methodname; this. bean = bean; invokemap (); // put the request value in maptry {Init (); // initialization} catch (exception e) {// todo auto-generated catch blocke. printstacktrace () ;}}/*** put the request value into map */private void invokemap () {iterator it = request. getparametermap (). entryset (). iterator (); While (it. hasnext () {map. entry entry = (map. entry) it. next (); Str Ing key = entry. getkey (). tostring (); string value = stringuitl. getstringarray (string []) entry. getvalue (); map. put (Key, value );}} /*** set related objects to the user action * @ throws initializeactionexception * @ throws nosuchmethodexception * @ throws securityexception * @ throws invocationtargetexception * @ throws illegalaccessexception * @ throws limit */private void Init () throws exception {// get Class Object Class1 = NULL; object = NULL; try {// If (this. Bean! = NULL) {object = bean; class1 = bean. getclass ();} else {class1 = Class. forname (classpath); // initialization object = class1.newinstance (); system. out. println ("initialization object") ;}} catch (exception e) {system. err. println (E); throw new initializeactionexception () ;}// set the field parameter initfield (class1, object) for the object; // call this. result = invokemethod (class1, object);}/*** initialization field */private void initfield (class class1, object) throws exception {// get a set of fields Field [] fields = class1.getdeclaredfields (); // obtain the method set [] Methods = class1.getdeclaredmethods (); For (field: fields) {string name = (string) map. get (field. getname (); // assign a value to the specified string methodname = "set" + stringuitl. capitalize (field. getname (); If (methodsuitl. exist (methods, methodname) {field. setaccessible (true); // field. set (object, map. get (field. getname (); object value = map. get (field. getname (); If (value! = NULL) {fieldconvertor. convertor (object, field, value. tostring () ;}}}/*** call Method */private string invokemethod (class class1, object) throws exception {// create the servletactioncontent Instance Object servletactioncontent servlet = new servletactioncontent (); servlet. setrequest (request); servlet. setresponse (response); servlet. setsession (request. getsession (); // create the parameter type class parameter [] = new class [] {servletactioncontent. class}; Method method = class1.getmethod ("setservletactioncontent", parameter); // parameter value: Object OBJ [] = new object [] {servlet}; method. invoke (object, OBJ); // operation method // call the execute method // method execute = class1.getmethod ("execute", new class [0]); method execute = class1.getmethod (this. methodname, new class [0]); object type = execute. invoke (object, new class [0]); return type. tostring (); // set the return type }}

Request Manager:

Package Org. pan. controller; import Java. util. list; import javax. servlet. filterchain; import javax. servlet. servletrequest; import javax. servlet. servletresponse; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. pan. bean. request; import Org. pan. bean. result; import Org. pan. bean. springbean; import Org. pan. code. actionmanage; import Org. pan. code. configuration; Im Port Org. pan. spring. springbeanfactory; import Org. pan. support. actionsupport; import Org. pan. util. resultuitl;/*** request controller ** @ author Pan **/public class requestcontroller {private httpservletrequest request; private httpservletresponse response; private filterchain chain; Public requestcontroller (servletrequest request, servletresponse response, filterchain chain) {This. request = (httpservletrequest) request; t His. response = (httpservletresponse) response; this. chain = chain;}/*** process request */Public void dofilter () throws exception {string filepath = request. getservletpath (). substring (1); // The Name Of The current file // get the jump object through the configuration file and some operation methods configuration = new configuration (request); system. out. println ("filepath:" + filepath); // pseudo static request RT = configuration. findbylike (filepath); If (RT = NULL) {system. out. println (RT); system. out. print Ln ("error:" + configuration. find (filepath); RT = configuration. find (filepath);} // skip if rt is null or not. If (RT = NULL) {chain. dofilter (request, response); return;} // if no class path is configured, it is directly forwarded to the result page as a forwarder if (RT. getclasspath () = NULL | RT. getclasspath () = "") {result rs = resultuitl. findresult (RT. getresults (), actionsupport. success); If (rs = NULL) {chain. dofilter (request, response);} else {request. getrequestdispatcher (RS. getpath ()). f Orward (request, response);} return;} // spring supportspringbeanfactory factory = new springbeanfactory (request); object = NULL; If (RT. GETID ()! = NULL | Rt. GETID ()! = "") {// Enable if (springbean. getsupport () {object = factory. getbean (RT. GETID () ;}}// user Action Manager actionmanage = new actionmanage (object, request, response, Rt. getclasspath (), Rt. getmethod (); string result = actionmanage. getresult (); // search for the page list corresponding to the returned value <result> Results = RT. getresults (); string Path = ""; for (result result2: Results) {If (result2.getname (). equals (result) {// obtain the corresponding Path = result2.getpath () ;}} // Forward to the corresponding page if (! Path. equals ("") {request. getrequestdispatcher (PATH ). forward (request, response);} // The above is not processed, that is, the configuration does not win or the page does not exist else {chain. dofilter (request, response );}}}

Configuration Manager and XML reading:

Package Org. pan. code; import Java. util. list; import javax. servlet. HTTP. httpservletrequest; import Org. pan. bean. request; import Org. pan. bean. result; import Org. pan. exception. readrequestxmlexception;/*** Configuration Manager ** @ author Pan **/public class configuration {private list <request> requests; Public configuration (httpservletrequest request) {xmlread = new xmlread (request); try {requests = xmlread. read ();} catch (readrequestxmlexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}/*** search for the request Node object in the configuration file * @ Param name * @ return */public request find (string name) {for (request: requests) {system. out. println ("name:" + name + "-" + request. getpage (); If (request. getpage (). equals (name) {return request;} return NULL;}/*** fuzzy search * @ Param name * @ return */public request findbylike (string name) {// use a regular expression to verify for (request: requests) {system. out. println ("findbylike:" + request. getpage () + "-" + name); string Reg = request. getpage (); string page = Name; If (page. matches (REG) {return request;} return NULL;}/*** searches for objects by ID, it is mainly used for URL pseudo static * @ Param ID * @ return */public request findbyid (string ID) {for (request: requests) {system. out. println (request. getpage () + "-" + id); If (request. GETID (). equals (ID) {return request ;}} return NULL ;}}

XML reading:

Package Org. pan. code; import Java. io. file; import Java. io. fileinputstream; import Java. util. arraylist; import Java. util. list; import javax. servlet. HTTP. httpservletrequest; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import Org. pan. bean. jdbcconnectconfig; import Org. pan. bean. request; import Org. pan. bean. result; import Org. pan. bean. springbean; import Org. pan. ti On. readrequestxmlexception; import Org. pan. util. serverurl; import Org. w3C. dom. document; import Org. w3C. dom. element; import Org. w3C. dom. node; import Org. w3C. dom. nodelist;/*** read XML * @ author Pan **/public class xmlread {private httpservletrequest request; // The request object private list read from XML <request> requests = new arraylist <request> (); Public xmlread (httpservletrequest request) {This. request = request;} public list <r Equest> Read () throws readrequestxmlexception {// determine whether the file exists string strpath = serverurl. getdiskpath (request) + "WEB-INF/request. XML "; file = new file (strpath); If (! File. exists () {Throw new readrequestxmlexception ();} documentbuilderfactory DBF = documentbuilderfactory. newinstance (); try {documentbuilder DB = DBF. newdocumentbuilder (); document DOC = dB. parse (New fileinputstream (strpath); element root = Doc. getdocumentelement (); // whether action is managed by spring string spring = "false"; try {spring = Doc. getelementsbytagname ("Spring "). item (0 ). gettextcontent (); If (Spring = NULL) {Spring = "false" ;}} catch (exception e) {spring = "false" ;}finally {springbean. setsupport (Boolean. parseboolean (spring);} // JDBC configuration string url = ""; string driverclass = ""; string username = ""; string Password = ""; nodelist serverslist = Doc. getelementsbytagname ("request"); For (INT I = 0; I <serverslist. getlength (); I ++) {node = serverslist. item (I); system. out. println (serverslist. getlength (); string P Age = NULL; try {page = node. getattributes (). getnameditem ("page "). getnodevalue () ;}catch (exception e) {page = "" ;}// ID string id = ""; try {id = node. getattributes (). getnameditem ("ID "). getnodevalue (); Id = id = NULL? "": ID;} catch (exception e) {id = "";} string classpath = NULL; try {classpath = node. getattributes (). getnameditem ("class "). getnodevalue (); If (classpath = NULL) {classpath = "" ;}} catch (exception e) {classpath = "" ;}string method = NULL; try {method = node. getattributes (). getnameditem ("method "). getnodevalue (); If (Method = NULL | method = "null") {method = "execute" ;}} catch (exception E) {method = "execute";} string pretend = NULL; try {pretend = node. getattributes (). getnameditem ("Pretend "). getnodevalue () ;}catch (exception e) {pretend = "false" ;}request request = new request (); Request. setid (ID); Request. setpage (PAGE); Request. setclasspath (classpath); Request. setmethod (method); // method name request. setpretend (Boolean. parseboolean (pretend); // pseudo static for (Int J = 0; j <node. getchildnodes (). getlength (); j ++) {node book1 = node. getchildnodes (). item (j); If (book1.getnodetype () = node. element_node) {result = new result (); string name = book1.getattributes (). getnameditem ("name "). getnodevalue (); string Path = book1.gettextcontent (); result. setname (name); result. setpath (PATH); Request. add (result) ;}} requests. add (request) ;}} catch (exception e) {system. err. println (E); // throw new readrequestxmlexception ();} return this. requests ;}}

Spring support module:

Package Org. pan. spring; import javax. servlet. HTTP. httpservletrequest; import Org. pan. util. serverurl; import Org. springframework. context. support. abstractapplicationcontext; import Org. springframework. context. support. applicationobjectsupport; import Org. springframework. context. support. classpathxmlapplicationcontext; import Org. springframework. context. support. filesystemxmlapplicationcontext;/*** Spring framework support class ** @ author Pan **/public class springsupport extends applicationobjectsupport {private abstractapplicationcontext AAC; Public springsupport (httpservletrequest request) {// applicationcontent can be set. XML file in SRC or WEB-INF try {AAC = new classpathxmlapplicationcontext ("applicationcontext. XML ");} catch (exception e) {}if (AAC = NULL) {AAC = new filesystemxmlapplicationcontext (serverurl. getdiskpath (request) + "/WEB-INF/applicationcontext. XML ") ;}} public object getbean (string bean) {try {return AAC. getbean (bean);} catch (exception e) {system. err. println (E); return NULL ;}}}

Spring Bean Factory:

package org.pan.spring;import javax.servlet.http.HttpServletRequest;/** * Spring Bean Factory *  * @author Pan *  */public class SpringBeanFactory extends SpringSupport {public SpringBeanFactory(HttpServletRequest request) {super(request);// TODO Auto-generated constructor stub}public Object findBean(String id) {Object object = null;try {object = getBean(id);} catch (Exception e) {//Do not display error}return object;}}

SQL statement creation:

Package Org. pan. SQL. create; import Java. lang. reflect. field; import Java. lang. reflect. method; import Org. pan. exception. fieldnullexception; import Org. pan. util. stringuitl;/*** this the create SQL code util * @ author Pan **/public class sqlcodecreate {private object; Public sqlcodecreate (Object object) {This. object = object;}/*** create update SQL code ** @ return * @ throws exception */Public Str Ing updatesql () throws exception {class Cl = object. getclass (); string table = Cl. getsimplename (); // class name = table name string SQL = "Update [" + Table + "] Set "; // obtain the field name // generate the field [] fields = Cl. getdeclaredfields (); field ID = Cl. getdeclaredfield ("ID"); Method idmethod = Cl. getmethod ("GETID", new class [0]); object ivalue = idmethod. invoke (object, new class [0]); If (ivalue = NULL) {Throw new fieldnullexception (" Field: ID, not null! ") ;}For (field: fields) {If (field. getname (). equals ("ID") continue; string M = "get" + stringuitl. capitalize (field. getname (); Method method = Cl. getmethod (M, new class [0]); object o = method. invoke (object, new class [0]); If (o! = NULL) SQL + = "[" + field. getname () + "] = '" + O + "',";} SQL = stringuitl. removeendchar (SQL); Method method = Cl. getmethod ("GETID", new class [0]); SQL + = "where id = '" + method. invoke (object, new class [0]) + "'"; return SQL ;} /*** create an SQL statement ** @ return * @ throws nosuchmethodexception * @ throws securityexception */Public String insertsql (Boolean sign) throws exception {class Cl = object. getclass (); Str Ing table = Cl. getsimplename (); // class name = table name string SQL = "insert into [" + Table + "]"; // obtain the field name string strfileds = "("; // field string strvalues = ") values ("; // value // generate field [] fields = Cl. getdeclaredfields (); // output value for (field: fields) {string M = "get" + stringuitl. capitalize (field. getname (); Method method = Cl. getmethod (M, new class [0]); object value = method. invoke (object, new class [0]); // insert Idi F (field. getname (). equals ("ID") continue; If (sign) {If (value = NULL) {continue;} strfileds + = "[" + field. getname () + "],"; strvalues + = "'" + value + "',";} strfileds = stringuitl. removeendchar (strfileds); strvalues = stringuitl. removeendchar (strvalues) + ");"; SQL + = strfileds + strvalues; return SQL ;} /*** create a query statement ** @ return * @ throws exception */Public String selectsql () throws exception {Class C L = object. getclass (); string table = Cl. getsimplename (); string SQL = "select * from [" + Table + "]"; return SQL;}/*** query fields by condition, query all non-null fields and pass in the bool parameter to specify whether to use like to query ** @ return * @ throws nosuchmethodexception * @ throws securityexception */Public String seletefieldsql (Boolean like) throws exception {class Cl = object. getclass (); string table = Cl. getsimplename (); string SQL = "select * from [" + Table + "] Where 1 = 1"; field [] fields = Cl. getdeclaredfields (); For (field: fields) {string fname = field. getname (); Method method = Cl. getmethod ("get" + stringuitl. capitalize (fname), new class [0]); object o = method. invoke (object, new class [0]); If (o! = NULL) {If (! Like) {SQL + = "and [" + fname + "] = '" + O + "'";} else {SQL + = "and [" + fname + "] Like '%" + O + "%'" ;}} return SQL ;} /*** create and delete statement * @ return * @ throws exception */Public String deletesql () throws exception {class Cl = object. getclass (); string table = Cl. getsimplename (); Method method = Cl. getmethod ("GETID", new class [0]); object Ob = method. invoke (object, new class [0]); If (Ob = NULL) {Throw new fieldnullexception ("field is null from: ID ");} string SQL = "Delete [" + Table + "] Where id = '" + OB + "'"; return SQL ;}}

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.