繁体中文
设为首页
加入收藏
当前位置:JSP技术首页 >> 资料/其它 >> 数据库访问简单实现---edainfo-model(三)——简单例子

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

2004-12-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:下面就正式来讲一下开发步骤: 首先,在Tomcat5.X下建一个jdbc/edainfo的数据源,数据库可以是oracle、sql server、mysql。表的结构如下: CREATE TABLE example ( id varchar(13) NOT NULL , name varchar(50) ...

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

首先,在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内容如下:

example

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

%>

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

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

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

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

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

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

"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"

"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

type="org.apache.struts.validator.DynaValidatorForm">

key="exception.unawareError"

path="/error.jsp"

scope="request"

type="java.lang.Exception"/>

type="net.edainfo.example.ExampleAction"

name="exampleForm"

validate="false"

scope="request">

null="false"

parameter="ApplicationResources"/>

在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简单例子下载

责任编辑:admin
相关文章