分享
 
 
 

数据库访问简单实现---edainfo-model(三)——简单例子

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

下面就正式来讲一下开发步骤:

首先,在Tomcat5.X下建一个jdbc/edainfo的数据源,数据库可以是oracle、sql server、mysql。表的结构如下:

CREATE TABLE example (

id varchar(13) NOT NULL ,

name varchar(50) NULL ,

address varchar(50) NULL

) ON [PRIMARY]

其中,id为主键。

datasource.xml内容如下:

<?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>

<pk tabColumn="id" />

<pages>

<page name="fore" size="20" viewPage="5"/>

</pages>

</model>

</database>

init-config.xml前面已经介绍过,这里就不详细介绍了。

将以上两个文件都放置到WEB-INF目录下。

在web.xml中,建立一个net.edainfo.filter.SetCharacterEncodingFilter的过滤器。

建立一个ExampleModel.java,如下所示:

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");

}

}

建立一个异常类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"};

}

再建立一个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 for 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);

}

}

然后是建立jsp页面,这里有两个页面,一个是浏览页,一个是表单页;

浏览页,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");

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title><bean:message key="edainfo.example"/></title>

</head>

<body>

<table width="75%" border="0" align="center" cellpadding="3" cellspacing="1">

<tr>

<td colspan="2"><html:link action="example?operation=addform"><bean:message key="example.add"/></html:link></td>

</tr>

<tr>

<td width="47%"><bean:message key="example.name"/></td>

<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>

<tr align="center">

<td colspan="2">

<%@ include file="/turnPage.jsp" %>

</td>

</tr>

</table>

</body>

</html>

表单页,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"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title><bean:message key="edainfo.example"/></title>

</head>

<body>

<logic:messagesPresent>

<ul>

<html:messages id="error">

<li><bean:write name="error"/></li>

</html:messages>

</ul>

</logic:messagesPresent>

<html:form action="example">

<table width="75%" border="0" align="center" cellpadding="3" cellspacing="1">

<tr>

<td width="26%"><bean:message key="example.name"/></td>

<td width="74%"><html:text property="name" size="20"/></td>

</tr>

<tr>

<td><bean:message key="example.address"/></td>

<td><html:text property="address" size="20"/></td>

</tr>

<tr align="center">

<td colspan="2"><html:submit property="submit" styleClass="buttonface">

<bean:message key="button.save"/>

</html:submit></td>

</tr>

</table>

<html:hidden property="operation"/>

</html:form>

</body>

</html>

以上两个页面放到页面根目录下,请记得将请记得将turnPage.jsp也拷贝过来。

然后,我们在struts-config.xml中配置我们的action:

<?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 Resources 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>

在WEB-INF/lib下记得要放如下包:struts1.1及它的相关包、jfreechart及其相关包、edabase-model.jar、log4j-1.2.8.jar、jdom.jar、xercesImpl.jar、xmlParserAPIs.jar。

现在已经完成,启动tomcat,在浏览器中敲如http://xxxxx/example.do,就可以看到效果了。

在这里,主要演示了edainfo-model的数据库操作、分页及actionform中批量的获取数据,如果你已经对它发生兴趣了,请继续关注本站的相关文章,还有更多精彩的内容等着你。

下面是这个例子的完整源码下载:

edainfo-model简单例子下载

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有