欢迎光临
我们一直在努力

数据库访问简单实现—edainfo-model(三)——简单例子-JSP教程,数据库相关

建站超值云服务器,限时71元/月

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

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 数据库访问简单实现—edainfo-model(三)——简单例子-JSP教程,数据库相关
分享到: 更多 (0)

相关推荐

  • 暂无文章