model|访问|数据|数据库
下面就正式来讲一下开发步骤:
首先,在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简单例子下载