论坛首页 Java企业应用论坛

基于infoq介绍的国产框架开发新增,删除,修改,分页,导出体验

浏览 4434 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2010-12-12   最后修改:2010-12-13

infoq报道国内开源框架,本人从零开始体验simple,包括simple基础文件配置步骤。愿意与大家分享下开发过程,请各位高手给予指导,不要讨论除技术之外的人身攻击,多谢。


经过最近对simple的学习,力求从零基础介绍如何基于simple开发应用,经过实践,个人觉得基于simple开发应用,在展现层可以使用页面handleClass,不需要破坏html页面,后台业务处理可以调用组件的确可以加快开发速度(这是开发体会,不是广告,在开发过程中也很郁闷,看着自己憋了几个小时份上,请提出技术意见)。关于开发技巧还需要要继续探索,并愿意与志同道合者多多交流。

 

文件一,web.xml,拷贝到WEB-INF目录下(已经有simple工程跳过该步骤)。

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>simpleframework</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
<servlet>
<servlet-name>startup</servlet-name>
<servlet-class>net.simpleframework.web.SimpleWebApplication</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<filter>
<filter-name>actionFilter</filter-name>
<filter-class>net.simpleframework.web.page.PageFilter</filter-class>
<init-param>
<param-name>initializerHandle</param-name>
<param-value>net.simpleframework.example.MyPageInitializer</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>actionFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
 
<listener>
<listener-class>net.simpleframework.web.page.PageEventAdapter</listener-class>
</listener>
 
<error-page>
<error-code>404</error-code>
<location>/template/404.jsp</location>
</error-page>
</web-app>

文件二,project.xml,拷贝到WEB-INF目录下 (已经有simple工程跳过该步骤)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="defaultDatasource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/simple3?useServerPrepStmts=false&amp;useUnicode=true&amp;characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="14400000" />
</bean>
   <bean id="idOrgInitializer" class="net.simpleframework.organization.OrgInitializer">
<property name="sync" value="true" />
</bean>
<bean id="idContentInitializer" class="net.simpleframework.content.ContentInitializer">
<property name="sync" value="true" />
<property name="bbsApplicationHandle" value="net.simpleframework.example.MyBbsHandle" />
</bean>
<bean id="idMyInitializer" class="net.simpleframework.my.MyInitializer">
<property name="sync" value="true" />
<property name="homeApplicationHandle" value="net.simpleframework.example.MyHomeApplicationHandle"></property>
</bean><bean id="idNotificationInitializer" class="net.simpleframework.notification.NotificationInitializer">
</bean>
<bean id="idSysManagerInitializer" class="net.simpleframework.sysmgr.SysManagerInitializer">
<property name="sync" value="true" />
</bean>
<bean id="idApplicationConfig" class="net.simpleframework.web.WebApplicationConfig">
<property name="initializerList">
<list>
<ref local="idSysManagerInitializer" />
<ref local="idOrgInitializer" />
<ref local="idContentInitializer" />
<ref local="idMyInitializer" />
<ref local="idNotificationInitializer" />
</list>
</property>
 
<property name="serverUrl" value="http://127.0.0.1:8080" />
<property name="datePattern" value="yyyy-MM-dd HH:mm" />
<property name="tableEntityManagerClass">
<value>net.simpleframework.ado.db.cache.MapTableEntityManager</value>
</property>
<!-- cache.Map -->
</bean>
</beans>
 

第三步,编写演示例子数据库脚本(这才是开发的第一步)。

CREATE TABLE `simple_framework_test` (
 `id` int(32) default NULL,
 `version` varchar(128) default NULL,
 `name` varchar(128) default NULL,
 `description` varchar(128) default NULL
) ;

第四步,编写TestHandle.java(该类实现了新增,修改,删除,该类继承了AbstractAjaxRequestHandle)。

package net.simpleframework.test;
 
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
 
import net.simpleframework.ado.DataObjectManagerFactory;
import net.simpleframework.ado.db.ITableEntityManager;
import net.simpleframework.ado.db.schema.ExpressionValue;
import net.simpleframework.ado.db.schema.Table;
import net.simpleframework.ado.id.ID;
import net.simpleframework.web.IWebApplication;
import net.simpleframework.web.page.IForward;
import net.simpleframework.web.page.UrlForward;
import net.simpleframework.web.page.component.ComponentParameter;
import net.simpleframework.web.page.component.base.ajaxrequest.AbstractAjaxRequestHandle;
 
public class TestHandle extends AbstractAjaxRequestHandle {
 
// 新增 本例只是dmeo,并没有做到公用方法抽象与优化
public IForward doSave(final ComponentParameter compParameter)
throws Exception {
final DataSource dataSource = IWebApplication.Instance.getWebApplication().getDataSource();
Table table = new Table("simple_framework_test");
 
ITableEntityManager entityManager = DataObjectManagerFactory.getTableEntityManager(dataSource, table);
Map<String, Comparable> insertData = new HashMap<String, Comparable>();
final HttpServletRequest request = compParameter.request;
// request.getParameter
String id = (String) request.getParameter("id2");// 获得修改的id
if (id != null) {
insertData.put("id", id);
insertData.put("version",compParameter.getRequestParameter("version2"));
insertData.put("name", compParameter.getRequestParameter("name2"));
insertData.put("description",compParameter.getRequestParameter("description2"));
entityManager.update(insertData);
} else { // 新增
insertData.put("id", (Comparable) ID.Utils.newID(entityManager.nextIncValue("id")));
insertData.put("version",compParameter.getRequestParameter("version"));
insertData.put("name", compParameter.getRequestParameter("name"));
insertData.put("description",compParameter.getRequestParameter("description"));
entityManager.insert(insertData);
}
return jsonForward(new JsonCallback() {
@Override
public void doAction(Map<String, Object> json) throws Exception {
json.put("ok", true);
}
});
}
 
// 删除
public IForward doDeleteTest(final ComponentParameter compParameter)
throws Exception {
final DataSource dataSource = IWebApplication.Instance.getWebApplication().getDataSource();
Table table = new Table("simple_framework_test");
ITableEntityManager entityManager = DataObjectManagerFactory.getTableEntityManager(dataSource, table);
 
String id = compParameter.getRequestParameter("testId");
entityManager.delete(new ExpressionValue("id=?", new Object[] { id }));
 
return jsonForward(new JsonCallback() {
@Override
public void doAction(Map<String, Object> json) throws Exception {
json.put("ok", true);
}
});
}
}
 

第五步,编写TestPagerHandle.java (该类实现了表格列表,导出,分页功能)。

package net.simpleframework.test;

 
import java.util.HashMap;
import java.util.Map;
 
import javax.sql.DataSource;
 
import net.simpleframework.ado.DataObjectManagerFactory;
import net.simpleframework.ado.IDataObjectQuery;
import net.simpleframework.ado.db.IQueryEntityManager;
import net.simpleframework.ado.db.schema.SQLValue;
import net.simpleframework.web.IWebApplication;
import net.simpleframework.web.page.component.ComponentParameter;
import net.simpleframework.web.page.component.ui.pager.AbstractTablePagerData;
import net.simpleframework.web.page.component.ui.pager.AbstractTablePagerHandle;
import net.simpleframework.web.page.component.ui.pager.TablePagerBean;
import net.simpleframework.web.page.component.ui.pager.TablePagerColumn;
 
public class TestPagerHandle extends AbstractTablePagerHandle {
 
@Override
public Object getBeanProperty(final ComponentParameter compParameter,
final String beanProperty) throws Exception {
if ("title".equals(beanProperty)) {
final StringBuilder sb = new StringBuilder();
sb.append("<a style=\"margin-left: 10px;\" onclick=\"$Actions['")
.append(compParameter.componentBean.getName())
.append("'].exportFile(null, false);\">导出</a>");
return sb.toString();
}
return super.getBeanProperty(compParameter, beanProperty);
}
 
@Override
public IDataObjectQuery<?> createDataObjectQuery(
final ComponentParameter compParameter) throws Exception {
 
final DataSource dataSource = IWebApplication.Instance.getWebApplication().getDataSource();
final IQueryEntityManager queryManager = DataObjectManagerFactory.getQueryEntityManager(dataSource);
final IDataObjectQuery<Map<String, Object>> data = queryManager.query(new SQLValue("SELECT * FROM simple_framework_test s"));
return data;
}
// 这个方法对业务数据做相关处理相当的重要
@Override
public AbstractTablePagerData createTablePagerData(final ComponentParameter compParameter)
throws Exception {
return new AbstractTablePagerData(compParameter) {
@Override
protected Map<String, TablePagerColumn> getTablePagerColumns() {
return ((TablePagerBean) compParameter.componentBean).getColumns();
}
 
@SuppressWarnings("unchecked")
@Override
protected Map<Object, Object> getRowData(final Object dataObject) throws Exception {
final Map<Object, Object> dataObjectMap = new HashMap<Object, Object>();
 
final Map map = (Map) dataObject;
 
// 拼装查看url
final StringBuffer name = new StringBuffer(200); 
name.append("<a ");
name.append("onclick=\"");
name.append("$Actions['viewTestWindow']('view=true&testId="+ map.get("id") + "')");
name.append("\"");
name.append(">");
name.append(map.get("name").toString());
name.append("</a>");
map.put("name",name);
       
dataObjectMap.putAll((Map<? extends Object, ? extends Object>) map);
 
dataObjectMap.put("action","<a onclick=\"$Actions['DeleteTest']('testId="+ map.get("id") + "');$Actions['TestListPager'].refresh();\">删除</a>&nbsp;<a onclick=\"$Actions['addOrUpdateTestWindow']('testId="+ map.get("id") + "');\">修改</a>");
//dataObjectMap.put("action","<a class=\"TestPagerRowStyle down_menu_image\"></a>");  // 这是数据菜单绑定相关操作按钮
return dataObjectMap;
}
 
@Override
protected Map<Object, Object> getRowAttributes(final Object dataObject) throws Exception {
final Map<Object, Object> attributes = super.getRowAttributes(dataObject);
 
attributes.put(ROW_ID,((Map) dataObject).get("id") ); // 如果不封装bean,需要特殊处理
//attributes.put("name",((Map) dataObject).get("name") );
final Map<Object, Object> attributes2 = getTableRowAttributes(dataObject);
if (attributes2 != null) {
attributes.putAll(attributes2);
}
return attributes;
}
}; 
}
}

第六步,编写TestModify.java (修改实现)。

package net.simpleframework.test;

 
import java.util.List;
import java.util.Map;
 
import javax.sql.DataSource;
 
import net.simpleframework.ado.DataObjectManagerFactory;
import net.simpleframework.ado.db.ITableEntityManager;
import net.simpleframework.ado.db.schema.Table;
import net.simpleframework.ado.db.schema.UniqueValue;
import net.simpleframework.web.IWebApplication;
import net.simpleframework.web.page.DefaultPageHandle;
import net.simpleframework.web.page.PageParameter;
 
public class TestModify extends DefaultPageHandle {
 
@Override
public void pageLoad(final PageParameter pageParameter, final Map<String, Object> dataBinding,
final List<String> visibleToggleSelector, final List<String> readonlySelector,
final List<String> disabledSelector) throws Exception {
 
String id = pageParameter.request.getParameter("testId"); // 获得需要修改
String view = pageParameter.request.getParameter("view"); // 页面是否只读
        
final DataSource dataSource = IWebApplication.Instance.getWebApplication().getDataSource();
Table table = new Table("simple_framework_test");
  ITableEntityManager entityManager = DataObjectManagerFactory.getTableEntityManager(dataSource,table);  
      // 根据id 查询需要修改的对象
  Map date = entityManager.queryForMap(new  UniqueValue(id));
// simple 提供的数据绑定,在jsp页面自动填充字段
dataBinding.put("id2", id);
dataBinding.put("name2", date.get("name"));
dataBinding.put("description2", date.get("description"));
dataBinding.put("version2", date.get("version"));
 
   if(view!=null) {
readonlySelector.add("#ajaxTestParams input"); // simple 控制 input 页面显示只读
}
}
}
 

   发表时间:2010-12-12  

第六步,编写TestModify.java (修改实现)。

package net.simpleframework.test;

 
import java.util.List;
import java.util.Map;
 
import javax.sql.DataSource;
 
import net.simpleframework.ado.DataObjectManagerFactory;
import net.simpleframework.ado.db.ITableEntityManager;
import net.simpleframework.ado.db.schema.Table;
import net.simpleframework.ado.db.schema.UniqueValue;
import net.simpleframework.web.IWebApplication;
import net.simpleframework.web.page.DefaultPageHandle;
import net.simpleframework.web.page.PageParameter;
 
public class TestModify extends DefaultPageHandle {
 
@Override
public void pageLoad(final PageParameter pageParameter, final Map<String, Object> dataBinding,
final List<String> visibleToggleSelector, final List<String> readonlySelector,
final List<String> disabledSelector) throws Exception {
 
String id = pageParameter.request.getParameter("testId"); // 获得需要修改
String view = pageParameter.request.getParameter("view"); // 页面是否只读
        
final DataSource dataSource = IWebApplication.Instance.getWebApplication().getDataSource();
Table table = new Table("simple_framework_test");
  ITableEntityManager entityManager = DataObjectManagerFactory.getTableEntityManager(dataSource,table);  
      // 根据id 查询需要修改的对象
  Map date = entityManager.queryForMap(new  UniqueValue(id));
// simple 提供的数据绑定,在jsp页面自动填充字段
dataBinding.put("id2", id);
dataBinding.put("name2", date.get("name"));
dataBinding.put("description2", date.get("description"));
dataBinding.put("version2", date.get("version"));
 
   if(view!=null) {
readonlySelector.add("#ajaxTestParams input"); // simple 控制 input 页面显示只读
}
}
}
 

第七步,编写test.jsp 与 test.xml (大家可以看下jsp 文件,都是标准的html元素,xml文件中定义组件元信息,页面通过CSS选择器和组件绑定,所有simple不需要标签库)。

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

 
<table width="550" cellpadding=6 cellspacing=0 border=1 align="center">     
    <tr>
       <td> <h1 align="center">基于SimpleFramework 开发新增,删除,修改,查询</h1></td>
</tr>
</table>
 
<table width="550" cellpadding=6 cellspacing=0 border=1 align="center" id="ajaxTestParams">
<tr>
  <td colspan="2">产品名称:<input type="text" id="name" name="name" /></td>
</tr>
<tr>
<td colspan="2">产品描述:<input type="text" id="description" name="description" /></td>
</tr>
<tr>
<td colspan="2">产品版本:<input type="text" id="version" name="version" /></td>
</tr>
<tr>
<td><input type="button" name="Add" value="Add" onclick="$Actions['TestSave']();"></td>
 
<td>查询<input type="text" id="version" name="version"  /> </td> 
</tr>
<tr>
  <td id="testTablePager"  colspan="2"></td>
    </tr>
</table>
 
<div id="ajaxTestParams1">
</div>
------------------------------------------------下面是   test.xml--------------------------------------------
 
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/xsd/default/simple.xsd">
<scriptEval>first</scriptEval>
<components>
<!-- 组件组合使用 本例使用 ajaxRequest,tablePager,menu 组件的配合工作 -->    
<ajaxRequest name="TestSave" handleClass="net.simpleframework.test.TestHandle"
handleMethod="doSave" formSelector="#ajaxTestParams">
<jsCompleteCallback>
if (json["ok"]) 
alert("ok");
$Actions['TestListPager'].refresh(); <!-- 刷新表格,看到新增数据  -->  
   </jsCompleteCallback>
</ajaxRequest>
<ajaxRequest name="DeleteTest"
handleClass="net.simpleframework.test.TestHandle"
handleMethod="doDeleteTest">
<jsCompleteCallback>
<![CDATA[
if(json['ok'] == 'true'){
alert('删除成功');
}
]]>
</jsCompleteCallback>
</ajaxRequest>
<tablePager name="TestListPager" containerId="testTablePager"  
handleClass="net.simpleframework.test.TestPagerHandle" showCheckbox="true">
<!-- pagerBarLayout="none"  showCheckbox="true" pagerBarLayout="bottom"-->
<columns>
<column columnName="name" columnText="产品名称" separator="true"
style="text-align: center;width: 120px;" headerStyle="width: 120px;" />
<column columnName="description" columnText="产品描述" separator="true"
style="text-align: center;width: 120px;" headerStyle="width: 120px;" />
<column columnName="version" columnText="产品版本" separator="true"
style="text-align: center;width: 120px;" headerStyle="width: 120px;" />
<!-- 配置操作菜单 -->
<column columnName="action" sort="false" separator="true"  
style="text-align: center; width: 64px;" headerStyle="width: 64px;" 
columnText='操作' />
</columns>
     </tablePager>
 
<!--   urlForward 是简单写法  -->
<ajaxRequest name="addOrUpdateTestAjaxRequest">
<urlForward>/modify.jsp</urlForward>
</ajaxRequest>
 
<window name="addOrUpdateTestWindow"
contentRef="addOrUpdateTestAjaxRequest" title="测试修改" minWidth="550"
minHeight="380" resizable="true" destroyOnClose="true">
</window>
 
<ajaxRequest name="viewTestAjaxRequest">
<urlForward>/view.jsp</urlForward>
</ajaxRequest>
 
<window name="viewTestWindow"
contentRef="viewTestAjaxRequest" title="查看" minWidth="550"
minHeight="380" resizable="true" destroyOnClose="true">
</window>
 
</components>
</page>
 

第八步,编写modify.jsp 与 modify.xml

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<table width="550" cellpadding=6 cellspacing=0 border=1 align="center">     
    <tr>
       <td> <h1 align="center">基于SimpleFramework 开发修改</h1></td>
</tr>
</table>
<table width="550" cellpadding=6 cellspacing=0 border=1 align="center" id="ajaxTestParams">
    <!-- 对应于TestModify类的  pageLoad 方法中 dataBinding数据显示-->
  <input type="hidden" id="id2" name="id2"/> 
<tr>
  <td>产品名称:<input type="text" id="name2" name="name2" /></td>
</tr>
<tr>
<td>产品描述:<input type="text" id="description2" name="description2" /></td>
</tr>
<tr>
<td>产品版本:<input type="text" id="version2" name="version2" /></td>
</tr>
<tr>
<td><input type="button" name="Save" value="Save" onclick="$Actions['TestSave']();$Actions['addOrUpdateTestWindow'].close();"></td>
</tr>
</table>
 
----------下面是 modify.xml--------------
 
 
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/xsd/default/simple.xsd">
<scriptEval>first</scriptEval>
<!-- 定义页面处理类 ,handleClass 页面处理类 -->
<handleClass>
  net.simpleframework.test.TestModify
</handleClass>
</page>

第九步,编写view.jsp 与 view.xml

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<table width="550" cellpadding=6 cellspacing=0 border=1 align="center">     
    <tr>
       <td> <h1 align="center">基于SimpleFramework 开发修改</h1></td>
</tr>
</table>
<table width="550" cellpadding=6 cellspacing=0 border=1 align="center" id="ajaxTestParams">
  
<tr>
  <td>产品名称:<input type="text" id="name2" name="name2" /></td>
</tr>
<tr>
<td>产品描述:<input type="text" id="description2" name="description2" /></td>
</tr>
<tr>
<td>产品版本:<input type="text" id="version2" name="version2" /></td>
</tr>
</table>
 
----------------下面是  view.xml --------------------
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/xsd/default/simple.xsd">
<scriptEval>first</scriptEval>
<!-- 定义页面处理类 -->
<handleClass>
  net.simpleframework.test.TestModify
</handleClass>
</page>

 

下图是 工程的文件结构图:

 

运行效果如下:

 

附件: SimpleFramework Helloword 工程下载

 本例代码下载

注: 需要手工把本例代码拷贝到 simpleFramework Helloword 工程中,如果没有描述清楚的地方,请大家论坛讨论。

 

0 请登录后投票
   发表时间:2010-12-12   最后修改:2010-12-12
帖子终于发完了,但是版式乱了,
0 请登录后投票
   发表时间:2010-12-12  
注释写的不太清楚,下载一个看看。
0 请登录后投票
   发表时间:2010-12-13   最后修改:2010-12-13
前面还好,后面的格式全乱了?怎么回事?
0 请登录后投票
   发表时间:2010-12-13  
??排版怎么回事
0 请登录后投票
   发表时间:2010-12-13  
javaeye的排版这么奇怪?莫非现在在升级?
0 请登录后投票
   发表时间:2010-12-13  
chrome 下的样式,可以用恐怖来形容!
0 请登录后投票
   发表时间:2010-12-13  
我晕,这什么版面 咋看啊
0 请登录后投票
   发表时间:2010-12-13  
晕 我也是chrome  拌面全乱掉了 没法看
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics