Simple implementation of database access---edainfo-model (c)--simple example

Source: Internet
Author: User
model| Access | data | Database The following is a formal development step:
First, build a jdbc/edainfo data source under tomcat5.x, and the database can be Oracle, SQL Server, MySQL. The structure of the table is as follows:
CREATE TABLE Example (
ID varchar (not NULL),
Name varchar (NULL),
Address varchar (NULL)
) on [PRIMARY]
Where the ID is the primary key.
Datasource.xml contents are as follows:
<?xml version= "1.0" encoding= "gb2312"?>
<database>
<model name= "Examplemodel" >
<tablename>example</tablename>
<columns>
<column type= "0" name= "id" tabcolumn= "id"/>
<column type= "0" name= "name" tabcolumn= "name"/>
<column type= "0" name= "Address" tabcolumn= "Address"/>
<column type= "2" name= "Pagenum" tabcolumn= "Pagenum"/>
<column type= "2" name= "Operation" tabcolumn= "Operation"/>
</columns>
<relations>
</relations>
&LT;PK tabcolumn= "id"/>
<pages>
<page name= "Fore" size= "viewpage=" "5"/>
</pages>
</model>
</database>
Init-config.xml has been introduced before, here is not detailed.
Place the above two files in the Web-inf directory.
In Web.xml, a net.edainfo.filter.SetCharacterEncodingFilter filter is established.
Create a 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");
}
}
Create 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[] Exampleargs = {"Edainfo.example"};
}
Create a 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.EdaGlobals;
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 of this generated type comment go to Window-
* Preferences-java-code Style-code Templates
*/
public class Exampleaction extends Actionbase {
Public Actionforward executeAction (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.setattribute ("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 create JSP page, there are two pages, one is the browsing page, one is the form 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.getattribute ("Linkpage");
%>


<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title><bean:message key= "Edainfo.example"/></title>

<body>
<table width= "75%" border= "0" align= "center" cellpadding= "3" cellspacing= "1" >
<tr>
&LT;TD colspan= "2" ></tr>
<tr>
&LT;TD width= "47%" ><bean:message key= "Example.name"/></td>
&LT;TD width= "53%" ><bean:message key= "example.address"/></td>
</tr>
<logic:iterate name= "lists" id= "list" >
<tr>
<td><bean:write name= "list" property= "name"/></td>
<td><bean:write name= "List" property= "Address"/></td>
</tr>
</logic:iterate>
&LT;TR align= "center" >
&LT;TD colspan= "2" >
<%@ include file= "/turnpage.jsp"%>
</td>
</tr>
</table>
</body>

Form page, exampleform.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"%>


<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title><bean:message key= "Edainfo.example"/></title>

<body>
<logic:messagesPresent>
<ul>
<li><bean:write name= "Error"/></li>

</ul>
</logic:messagesPresent>
<table width= "75%" border= "0" align= "center" cellpadding= "3" cellspacing= "1" >
<tr>
&LT;TD width= "26%" ><bean:message key= "Example.name"/></td>
&LT;TD width= "74%" >
</tr>
<tr>
<td><bean:message key= "Example.address"/></td>
<td>
</tr>
&LT;TR align= "center" >
&LT;TD colspan= "2" ><bean:message key= "Button.save"/>
</tr>
</table>

</body>

The above two pages are placed in the root of the page, please remember to copy turnpage.jsp as well.
We then configure our action in the Struts-config.xml:
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE Struts-config Public
"-//apache Software foundation//dtd Struts Configuration 1.1//en"
"Http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" >
<struts-config>
<!--========== Form Bean definitions ===================================-->
<form-beans>
<form-bean name= "Exampleform"
Type= "Org.apache.struts.validator.DynaValidatorForm" >
<form-property name= "name" type= "java.lang.String"/>
<form-property name= "Address" type= "java.lang.String"/>
<form-property name= "Operation" type= "java.lang.String"/>
<form-property name= "Pagenum" type= "java.lang.String"/>
</form-bean>
</form-beans>
<!--global exception definitions-->
<global-exceptions>
<exception handler= "Net.edainfo.exception.BaseExceptionHandler"
key= "Exception.unawareerror"
Path= "/error.jsp"
Scope= "Request"
Type= "Java.lang.Exception"/>
</global-exceptions>
<!--========== Global Forward definitions ==============================-->
<global-forwards type= "Org.apache.struts.action.ActionForward" >
</global-forwards>
<!--========== Action Mapping definitions ==============================-->
<action-mappings>
<action path= "/example"
Type= "Net.edainfo.example.ExampleAction"
Name= "Exampleform"
Validate= "false"
Scope= "Request" >
<forward name= "list" path= "/listexample.jsp"/>
<forward name= "Add" path= "/example.do" redirect= "true"/>
<forward name= "AddForm" path= "/exampleform.jsp"/>
</action>
</action-mappings>
<!--========== message definitions ===========================-->
<message-resources
Null= "false"
parameter= "Applicationresources"/>
<!--========== Plug Ins Configuration ==================================-->

<!--
Add multiple validator resource files by setting the Pathnames property
With a comma delimitted list of resource files to load.
-->
<plug-in classname= "Org.apache.struts.validator.ValidatorPlugIn" >
<set-property property= "pathnames" value= "/web-inf/validator-rules.xml,
/web-inf/validation.xml "/>
</plug-in>

</struts-config>
Under Web-inf/lib Remember to put the following package: struts1.1 and its related packages, Jfreechart and its related packages, Edabase-model.jar, Log4j-1.2.8.jar, Jdom.jar, Xercesimpl.jar , Xmlparserapis.jar.
Now it's done, start Tomcat, knock like Http://xxxxx/example.do in the browser, and you'll see the effect.
Here, the main demonstration of Edainfo-model database operations, paging and actionform in batches to obtain data, if you have been interested in it, please continue to pay attention to the relevant articles of this site, there are more wonderful content waiting for you.
The following is the complete source download for this example:
Edainfo-model Simple Example Download



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.