|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
先说优点,首先和C,C++这些语言比起来,java很简单,去掉指针的java,非常好理解,自动垃圾回收机制也很好,自从JDK1.5推出以后,性能上又有了很大提高。model|会见|数据|数据库上面就正式来说一下开辟步骤:
起首,在Tomcat5.X下建一个jdbc/edainfo的数据源,数据库能够是oracle、sqlserver、mysql。表的布局以下:
CREATETABLEexample(
idvarchar(13)NOTNULL,
namevarchar(50)NULL,
addressvarchar(50)NULL
)ON[PRIMARY]
个中,id为主键。
datasource.xml内容以下:
<?xmlversion="1.0"encoding="gb2312"?>
<database>
<modelname="exampleModel">
<tablename>example</tablename>
<columns>
<columntype="0"name="id"tabColumn="id"/>
<columntype="0"name="name"tabColumn="name"/>
<columntype="0"name="address"tabColumn="address"/>
<columntype="2"name="pageNum"tabColumn="pageNum"/>
<columntype="2"name="operation"tabColumn="operation"/>
</columns>
<relations>
</relations>
<pktabColumn="id"/>
<pages>
<pagename="fore"size="20"viewPage="5"/>
</pages>
</model>
</database>
init-config.xml后面已先容过,这里就不具体先容了。
将以上两个文件都安排到WEB-INF目次下。
在web.xml中,创建一个net.edainfo.filter.SetCharacterEncodingFilter的过滤器。
创建一个ExampleModel.java,以下所示:
packagenet.edainfo.example;
importjava.util.Map;
importnet.edainfo.db.DBModel;
importnet.edainfo.db.ModelException;
importnet.edainfo.util.format.Encode;
importnet.edainfo.util.format.StringProcessor;
publicclassExampleModelextendsDBModel{
publicExampleModel(MapdataBase)throwsModelException{
super("exampleModel",dataBase);
}
publicvoidsetId(Stringid)throwsModelException{
set("id",id);
}
publicStringgetId()throwsModelException{
returngetString("id");
}
publicvoidsetName(Stringname)throwsModelException{
set("name",name);
}
publicStringgetName()throwsModelException{
returngetString("name");
}
publicvoidsetAddress(Stringaddress)throwsModelException{
set("address",address);
}
publicStringgetAddress()throwsModelException{
returngetString("address");
}
}
创建一个非常类ExampleException.java:
packagenet.edainfo.example;
importnet.edainfo.exception.BaseException;
publicclassExampleExceptionextendsBaseException{
staticpublicStringcreateFailed="exception.createFailed";
staticpublicStringretrieveFailed="exception.retrieveFailed";
staticpublicStringupdateFailed="exception.updateFailed";
staticpublicStringdeleteFailed="exception.deleteFailed";
staticpublicString[]exampleArgs={"edainfo.example"};
}
再创建一个action,ExampleAction.java:
packagenet.edainfo.example;
importjava.util.ArrayList;
importjava.util.Map;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importnet.edainfo.db.DAO;
importnet.edainfo.util.ActionBase;
importnet.edainfo.util.EdaGlobals;
importnet.edainfo.util.FormParameter;
importnet.edainfo.util.page.PageList;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.validator.DynaValidatorForm;
/**
*@authorslf
*
*TODOTochangethetemplateforthisgeneratedtypecommentgotoWindow-
*Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassExampleActionextendsActionBase{
publicActionForwardexecuteAction(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse,Mapparams)
throwsException{
DynaValidatorFormtheForm=(DynaValidatorForm)form;
Stringnext="";
conn=this.getConnection();
Stringoperation=(String)theForm.get("operation");
DAOdao=newDAO(conn,params.get(EdaGlobals.DATABASE_TYPE_KEY)
+"");
if(operation==null||operation.equals(""))
operation="list";
if(operation.equals("list")){
ExampleModelmodel=newExampleModel((Map)params
.get(EdaGlobals.DATA_BASE_KEY));
StringpageNum=(String)theForm.get("pageNum");
if(pageNum==null||pageNum.equals("")){
pageNum="1";
}
theForm.set("pageNum",pageNum);
intpageSize=model.getPageSize("fore");
intviewPage=model.getViewPage("fore");
PageListlists=dao.select(model,"",newArrayList(),"",Integer
.parseInt(pageNum),pageSize,viewPage,dao.DISTINCT_OFF);
Mapparms=FormParameter.setListPage(theForm,lists,"example",
request);
request.setAttribute("parms",parms);
next="list";
}elseif(operation.equals("add")){
ExampleModelmodel=newExampleModel((Map)params
.get(EdaGlobals.DATA_BASE_KEY));
model=(ExampleModel)FormParameter.getParameter(model,theForm);
model.setId(dao.generateId());
dao.create(model);
next="add";
}elseif(operation.equals("addform")){
next="addform";
theForm.set("operation","add");
}
returnmapping.findForward(next);
}
}
然后是创建jsp页面,这里有两个页面,一个是扫瞄页,一个是表单页;
扫瞄页,listExample.jsp:
<%@pagecontentType="text/html;charset=UTF-8"%>
<%@tagliburi="/WEB-INF/struts-bean.tld"prefix="bean"%>
<%@tagliburi="/WEB-INF/struts-html.tld"prefix="html"%>
<%@tagliburi="/WEB-INF/struts-logic.tld"prefix="logic"%>
<%
StringlinkPage=(String)request.getAttribute("linkPage");
%>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<title><bean:messagekey="edainfo.example"/></title>
</head>
<body>
<tablewidth="75%"border="0"align="center"cellpadding="3"cellspacing="1">
<tr>
<tdcolspan="2"><html:linkaction="example?operation=addform"><bean:messagekey="example.add"/></html:link></td>
</tr>
<tr>
<tdwidth="47%"><bean:messagekey="example.name"/></td>
<tdwidth="53%"><bean:messagekey="example.address"/></td>
</tr>
<logic:iteratename="lists"id="list">
<tr>
<td><bean:writename="list"property="name"/></td>
<td><bean:writename="list"property="address"/></td>
</tr>
</logic:iterate>
<tralign="center">
<tdcolspan="2">
<%@includefile="/turnPage.jsp"%>
</td>
</tr>
</table>
</body>
</html>
表单页,exampleForm.jsp:
<%@pagecontentType="text/html;charset=UTF-8"%>
<%@tagliburi="/WEB-INF/struts-bean.tld"prefix="bean"%>
<%@tagliburi="/WEB-INF/struts-html.tld"prefix="html"%>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<title><bean:messagekey="edainfo.example"/></title>
</head>
<body>
<logic:messagesPresent>
<ul>
<html:messagesid="error">
<li><bean:writename="error"/></li>
</html:messages>
</ul>
</logic:messagesPresent>
<html:formaction="example">
<tablewidth="75%"border="0"align="center"cellpadding="3"cellspacing="1">
<tr>
<tdwidth="26%"><bean:messagekey="example.name"/></td>
<tdwidth="74%"><html:textproperty="name"size="20"/></td>
</tr>
<tr>
<td><bean:messagekey="example.address"/></td>
<td><html:textproperty="address"size="20"/></td>
</tr>
<tralign="center">
<tdcolspan="2"><html:submitproperty="submit"styleClass="buttonface">
<bean:messagekey="button.save"/>
</html:submit></td>
</tr>
</table>
<html:hiddenproperty="operation"/>
</html:form>
</body>
</html>
以上两个页面放到页面根目次下,请记得将请记得将turnPage.jsp也拷贝过去。
然后,我们在struts-config.xml中设置我们的action:
<?xmlversion="1.0"encoding="ISO-8859-1"?>
<!DOCTYPEstruts-configPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!--==========FormBeanDefinitions===================================-->
<form-beans>
<form-beanname="exampleForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-propertyname="name"type="java.lang.String"/>
<form-propertyname="address"type="java.lang.String"/>
<form-propertyname="operation"type="java.lang.String"/>
<form-propertyname="pageNum"type="java.lang.String"/>
</form-bean>
</form-beans>
<!--globalexceptiondefinitions-->
<global-exceptions>
<exceptionhandler="net.edainfo.exception.BaseExceptionHandler"
key="exception.unawareError"
path="/error.jsp"
scope="request"
type="java.lang.Exception"/>
</global-exceptions>
<!--==========GlobalForwardDefinitions==============================-->
<global-forwardstype="org.apache.struts.action.ActionForward">
</global-forwards>
<!--==========ActionMappingDefinitions==============================-->
<action-mappings>
<actionpath="/example"
type="net.edainfo.example.ExampleAction"
name="exampleForm"
validate="false"
scope="request">
<forwardname="list"path="/listExample.jsp"/>
<forwardname="add"path="/example.do"redirect="true"/>
<forwardname="addform"path="/exampleForm.jsp"/>
</action>
</action-mappings>
<!--==========MessageResourcesDefinitions===========================-->
<message-resources
null="false"
parameter="ApplicationResources"/>
<!--==========PlugInsConfiguration==================================-->
<!--
Addmultiplevalidatorresourcefilesbysettingthepathnamesproperty
withacommadelimittedlistofresourcefilestoload.
-->
<plug-inclassName="org.apache.struts.validator.ValidatorPlugIn">
<set-propertyproperty="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复杂例子下载
如果你学习的是市场营销,是销售,也许参加大课堂的学习会更合适,因为你的工作能力中有个基础就是搭建自己的人脉, |
|