Database Access Simple Implementation - EDAINFO-Model (3) - Simple Example

xiaoxiao2021-03-06  27

Let's officially explain the steps:

First, build a JDBC / EDAINFO data source in Tomcat5.x, the database can be Oracle, SQL Server, MySQL. The structure of the table is as follows:

CREATE TABLE EXAMPLE

ID VARCHAR (13) Not NULL,

Name varchar (50) NULL,

Address Varchar (50) NULL

) On [primary]

Where ID is the primary key.

DataSource.xml content is as follows:

Example

Init-config.xml has been introduced, here is not described in detail here.

Place the above two files into the web-inflicity.

In Web.xml, establish a net.edainfo.filter.setCharacterencodingFilter filter.

Create an ExampleModel.java as follows:

Package Net.edainfo.example;

Import java.util.map;

Import net.edainfo.db.dbmodel;

Import net.edainfo.db.modelexception;

Import net.edainfo.util.format.Encode;

Import net.edainfo.util.format.stringprocessor;

Public Class ExampleModel Extends DBModel {

Public ExampleModel (Map Database) throws modelexception {

Super ("EXAMPLEMODEL", DATABASE);

}

Public void setid (String ID) throws modelexception {set ("id", id);

}

Public string getId () throws modelexception {

Return GetString ("ID");

}

Public void setname (String name) throws modelexception {

Set ("Name", Name);

}

Public string getname () throws modelexception {

Return GetString ("name");

}

Public void setaddress (string address) throws modelexception {

Set ("Address", Address;

}

Public string getaddress () throws modelexception {

Return GetString ("Address");

}

}

Establish an exception class ExampleException.java:

Package Net.edainfo.example;

Import net.edainfo.exception.baseexception;

Public class exampleException extends baseexception {

Static public string createfailed = "exception.createfailed";

Static Public String RetrieveFailed = "Exception.Retrievefailed";

Static public string updatefailed = "exception.Updatefailed";

Static public string deletefailed = "exception.deletefailed";

Static public string [] esampleargs = {"eDAINFO.EXAMPLE"}

}

Built an action, ExampleAction.java:

Package Net.edainfo.example;

Import java.util.arraylist;

Import java.util.map;

Import javax.servlet.http.httpservletRequest;

Import javax.servlet.http.httpservletResponse;

Import net.edainfo.db.dao;

Import net.edainfo.util.Actionbase;

Import net.edainfo.util.edaglobal;

Import net.edainfo.util.FormParameter;

Import net.edainfo.util.page.pagelist;

Import org.apache.struts.Action.actionform;

Import org.apache.struts.Action.actionForward;

Import org.apache.struts.action.actionmapping;

Import org.apache.struts.validator.dynavalidatorform;

/ **

* @Author SLF

*

* Todo to change the template for this generated Type Comment Go to WINDOW - * Preferences - Java - Code Style - Code Templates

* /

Public class exampleAction extends anctionbase {

Public ActionForward Executection (ActionMapping Mapping, Actionform Form,

HTTPSERVLETREQUEST Request, HttpServletResponse Response, Map Params

Throws exception {

DynavalidatorForm Theform = (Dynavalidatorform) Form;

String next = "";

CONN = this.getConnection ();

String Operation = (string) Theform.get ("Operation");

Dao Dao = New Dao (conn, params.get (edaglobals.database_type_key)

"" "

IF (Operation == Null || Operation.equals ("))

Operation = "List";

IF (Operation.equals ("List")) {

EXAMPLEMODEL MODEL = New ExampleModel ((MAP) Params

.get (edaglobals.data_base_key));

String Pagenum = (String) Theform.get ("Pagenum");

IF (Pagenum == Null || Pagenum.equals (")) {

Pagenum = "1";

}

Theform.Set ("Pagenum", Pagenum;

INT PageSize = Model.getPagesize ("Fore");

INT ViewPage = Model.getViewPage ("Fore");

Pagelist Lists = DAO.SELECT (Model, ", New ArrayList ()," ", Integer

.PARSEINT (PAGENUM), PAGESIZE, VIEWPAGE, DAO.DISTINCT_OFF;

Map Parms = FormParameter.SetListPage (Theform, Lists, "EXAMPLE",

REQUEST;

Request.setttribute ("PARMS", PARMS;

Next = "List";

} else if (Operation.equals ("add")) {

EXAMPLEMODEL MODEL = New ExampleModel ((MAP) Params

.get (edaglobals.data_base_key));

Model = (ExampleModel) FormParameter.GetParameter (Model, Theform);

Model.setid (DAO.generateId ());

Dao.create (Model);

Next = "add";} else if (Operation.equals ("addform")) {

Next = "addform";

Theform.Set ("Operation", "Add");

}

Return mapping.findforward (Next);

}

}

Then there is a JSP page, here there are two pages, one is a browsing page, one is a table page;

Browse page, Listexample.jsp:

<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%>

<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>

<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>

<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>

<%

String linkpage = (string) Request.getaTribute ("linkpage");

%>

<bean: message key = "eDAINFO.EXAMPLE" /> </ title></p> <p></ hEAD></p> <p><body></p> <p><table width = "75%" border = "0" align = "center" cellpadding = "3" Cellspacing = "1"></p> <p><tr></p> <p><TD COLSPAN = "2"> <html: link action = "eXample? Operation = addform"> <bean: message key = "example.add" /> </ html: link> </ td></p> <p></ TR></p> <p><tr></p> <p><TD width = "47%"> <bean: message key = "example.name" /> </ td></p> <p><td width = "53%"> <bean: message key = "example.address" /> </ td></p> <p></ TR></p> <p><logic: Itereate Name = "Lists" ID = "List"></p> <p><tr></p> <p><TD> <bean: Write Name = "List" property = "name" /> </ td></p> <p><TD> <bean: Write Name = "List" property = "address" /> </ td> </ tr></p> <p></ logic: Iterate></p> <p><tril = "center"></p> <p><TD COLSPAN = "2"></p> <p><% @ include file = "/ turnpage.jsp"%></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html></p> <p>Table page, ExampleForm.jsp:</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%></p> <p><html></p> <p><HEAD></p> <p><meta http-equiv = "content-type" content = "text / html; charSet = GB2312"></p> <p><title> <bean: message key = "eDAINFO.EXAMPLE" /> </ title></p> <p></ hEAD></p> <p><body></p> <p><logic: MessagesPresent></p> <p><ul></p> <p><html: messages id = "error"></p> <p><li> <bean: Write name = "error" /> </ li></p> <p></ html: Messages></p> <p></ ul></p> <p></ logic: MessagesPresent></p> <p><html: form action = "eXample"></p> <p><table width = "75%" border = "0" align = "center" cellpadding = "3" Cellspacing = "1"></p> <p><tr></p> <p><td width = "26%"> <bean: message key = "example.name" /> </ td></p> <p><td width = "74%"> <html: text protety = "name" size = "20" /> </ td></p> <p></ TR></p> <p><tr></p> <p><TD> <bean: Message Key = "Example.Address" /> </ td></p> <p><TD> <HTML: Text Property = "Address" Size = "20" /> </ td></p> <p></ TR></p> <p><tril = "center"> <TD colspan = "2"> <html: Submit property = "submit" styleclass = "buttonface"></p> <p><bean: message key = "button.save" /></p> <p></ html: Submit> </ td></p> <p></ TR></p> <p></ TABLE></p> <p><html: hidden protety = "Operation" /></p> <p></ html: form></p> <p></ body></p> <p></ html></p> <p>The above two pages are placed in the root directory of the page, please remember to remember to copy the turnpage.jsp.</p> <p>Then we configure our Action in Struts-Config.xml:</p> <p><? Xml Version = "1.0" encoding = "ISO-8859-1"?></p> <p><! DocType struts-config public</p> <p>"- // Apache Software Foundation // DTD Struts Configuration 1.1 // En"</p> <p>"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"></p> <p><struts-config></p> <p><! - ========================================================= == -></p> <p><form-beneans></p> <p><form-bean name = "exampleform"</p> <p>Type = "org.apache.struts.validator.dynavalidatorform"></p> <p><form-protety name = "name" type = "java.lang.string" /></p> <p><form-property name = "address" type = "java.lang.string" /></p> <p><form-protety name = "Operation" type = "java.lang.string" /></p> <p><form-property name = "pagenum" type = "java.lang.string" /></p> <p></ form-bean></p> <p></ form-beans></p> <p><! - Global Exception Definitions -></p> <p><global-exceptions></p> <p><exception handler = "Net.edaInfo.exception.baseexceptionhandler"</p> <p>Key = "exception.unawareerror"</p> <p>Path = "/ error.jsp" scope = "request"</p> <p>TYPE = "java.lang.exception" /></p> <p></ global-exceptions></p> <p><! - ================================================= -></p> <p><global-forwards type = "org.apache.struts.Action.actionforward"></p> <p></ global-forwards></p> <p><! - ================================================= -></p> <p><action-mappings></p> <p><action path = "/ esample"</p> <p>TYPE = "Net.edaInfo.example.example"</p> <p>Name = "exampleform"</p> <p>VALIDATE = "false"</p> <p>Scope = "Request"></p> <p><forward name = "list" path = "/ listexample.jsp" /></p> <p><forward name = "add" path = "/ example.do" redirect = "true" /></p> <p><forward name = "addform" path = "/ exampleForm.jsp" /></p> <p></ action></p> <p></ action-mappings></p> <p><! - ==========Message resources definitions ============================ -></p> <p><message-resources</p> <p>null = "false"</p> <p>Parameter = "ApplicationResources" /></p> <p><! - ========================================= = -> <! -</p> <p>Add Multiple Validator Resource Files By Setting The Pathnames Property</p> <p>WITH A COMMA DELIMITTED LIST OF RESOURCE FILES to LOAD.</p> <p>-></p> <p><plug-in classname = "org.apache.struts.validator.validatorPlugin"></p> <p><set-protety property = "pathnames" value = "/ web-inf / validator-rules.xml,</p> <p>/Web-inf/Validation.xml "/></p> <p></ plug-in></p> <p></ Struts-Config></p> <p>In web-inf / lib, remember to be asked as follows: struts1.1 and its related package, JFreechart and its related packages, edabase-model.jar, log4j-1.2.8.jar, jdom.jar, Xercesimpl.jar, XmlParserapis.jar.</p> <p>Now, start Tomcat, knock in http: //xxxxx/example.do in the browser, you can see the effect.</p> <p>Here, mainly demonstrates the database operation, paging, and Actionform's database operation, paging, and Actionform. If you have already interested it, please continue to pay attention to this article related articles, and more exciting content is waiting for you. .</p> <p>Here is the full source code of this example:</p> <p>EDAINFO-MODEL simple example download</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-81511.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="81511" 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.039</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 = 'qv87XDDNJzZs4VHrWP_2BUelkDR6N9UNwUjEonjFzZwXBXMWDcYR8gsbfqeJrZ7XcVTBcKx9cDDyYO_2FVhgeji2Yw_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>