论坛首页 Java企业应用论坛

也谈我开发SSH框架

浏览 3640 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-09-02  
下面是本人在业余时间搞的框架的简要介绍,无所谓推广,跟大家一起分享和交流
1.可视化代码生成工具,参考界面
基本原理还是从数据库中生成出来,目前支持mysql和Oracle的数据源,支持外键生成many-to-one关系,可以生成元数据,参考下面元数据介绍;
2.元数据
对象的基本属性,合法性和其他的一些扩展属性的描述,生成代码时可以根据数据库的定义生成,如:数据库字段注释,类型等;
可以用来描述界面,获取关联对象等;
3.RBAC的角色权限控制,并集成ACEGI
4.日志,Lucene等等

详细参考开源http://code.google.com/p/whfframework
   发表时间:2008-09-02  
上述工作完成后,相关的代码简单的表现为:
列表:
<framework:table pkStyle="checkbox_counter" navPosition="both" queryString="<%= queryString %>">
<framework:caption><span class="whf_object_title"><framework:attr source="STRUTS-META" attr="name" /></span></framework:caption>
</framework:table>


编辑:
<framework:table pkStyle="" border="1" cellspacing="0" cellpadding="0" navPosition="none" pageSize="1" displayStyle="form" editable="true" useStrutsForm="true" view="SYSTEM-OBJECT-VIEW">
<framework:caption><span class="whf_object_title"><bean:write name="STRUTS-META" property="name" /></span></framework:caption>
</framework:table>


主从表编辑:
<input type="hidden" name="UPDATING_PROPERTY_NAME" value="properties" />
<html:hidden property="webMethod" />
<framework:table pkStyle="" border="1" cellspacing="0" cellpadding="0" navPosition="none" pageSize="1" displayStyle="form" editable="true" useStrutsForm="true" view="SYSTEM-OBJECT-VIEW">
<framework:caption><span class="whf_object_title"><bean:write name="STRUTS-META" property="name" /></span></framework:caption>
</framework:table>

<framework:table id="ObjectProperty" pkStyle="checkbox_counter" pkSuffix="true" namePrefix="Property" border="1" cellspacing="0" cellpadding="0" boClassName="whf.framework.meta.entity.ObjectProperty"  navPosition="bottom" pageSize="100" editable="true" useStrutsForm="true" dataSourceName=".sortedProperties">
</framework:table>


查看:
<html:form action="<%=  (String)request.getAttribute(Constants.STRUTS_PATH) %>" method="post">
<html:hidden property="id" />
<html:hidden property="webMethod" />
<framework:table pkStyle="" border="1" cellspacing="0" cellpadding="0" navPosition="none" pageSize="1" displayStyle="form" useStrutsForm="true">
<framework:caption><span class="whf_object_title"><framework:attr source="STRUTS-META" attr="name" /></span></framework:caption>
</framework:table>
<input type='button' onclick='window.history.back()' value='<bean:message key="form.return" />' />
</html:form>


对于真正使用过程可以通过定义公共jsp文件,而对于服务器端都作了第一层封装处理,如果没有特殊处理,你不需要写一行代码;
0 请登录后投票
   发表时间:2008-09-02  
不错!楼主可以参考我的实现:http://www.iteye.com/topic/232219
另外,能否介绍一下你的代码生成器的设计思路,供大家参考参考!
0 请登录后投票
   发表时间:2008-09-02  
对于实体,从数据库中的描述即可,如下代码:
package whf.framework.tools.generator;

import whf.framework.entity.AbstractEntity;
import whf.framework.tools.generator.db.Column;
import whf.framework.tools.generator.db.Table;
import whf.framework.util.BeanUtils;

/**
 * @author wanghaifeng
 *
 */
public class EntityGenerator extends AbstractGenerator {
	private String packageName;

	private String className;

	private String[] props;

	private String[] types;
	
	private Table table;

	public EntityGenerator(String packageName, String className, String[] types, String[] props) {
		this.packageName = packageName;
		this.className = className;
		this.types = types;
		this.props = props;
	}

	public EntityGenerator(String packageName, Table table) {
		this.packageName = packageName;
		this.table = table;
	}

	public String[] getProps(){
		return this.props;
	}
	
	protected void gen() {
		super.setPackageName(this.packageName+".entity");
		super.setSuperClassName(AbstractEntity.class.getName());
		super.setImplementsSerializable(true);
//		super.setInterfaces(new String[]{"I"+this.table.getBoClassName()});
		if(table == null){
			super.setClassName(this.className);
			for (int i = 0; props != null && i < props.length; i++) {
				if(!BeanUtils.hasProperty(AbstractEntity.class, props[i])){
					super.addProp(types[i], props[i],true, true, props[i]);
				}
			}
		} else {
			super.setClassName(table.getBoClassName());
			for(Column col: table.getColumns()){
				String pn = col.getPropertyName();
				if(!BeanUtils.hasProperty(AbstractEntity.class, pn)){
					super.addProp(col.getPropertyType(), col.getPropertyName(), true, true, col.getColumnComment());
//					if(!col.isForeign()){
//						super.addProp(col.getPropertyType(), pn,true, true);
//					}else{
//						String refTableName = col.getReferencedTable();
//						String refBo = Utils.getBoClassName(refTableName, table.getTablePre());
//						String dt = this.packageName+".entity."+refBo;
//						String p = pn;
//						if(p.toLowerCase().endsWith("id")){
//							p = p.substring(0, p.length() - 2);
//						}
//						super.addProp(dt, p, true, true);
//					}
				}
			}
		}
	}

	public static void main(String[] args) throws Exception {

	}
}

对于其他使用模板,如下:
<?xml version="1.0" encoding="UTF-8"?>
<root>
	<comment>
		<![CDATA[
		{0}:boClassName
		{1}:bo variable
		{2}:rootPackage
		{3}:tableName
		{4}:subModule
		{5}:now
		{6}:IBOClassName
		]]>
	</comment>
	<java>
		<bo></bo>
		<dao>
		<![CDATA[
package {2}.dao;

import {2}.entity.{0};

/**
 * 数据访问对象接口
 * @author wanghaifeng
 * @email king@126.com
 * @modify {5}
 */
public interface {0}DAO extends whf.framework.dao.DAO<{0}> {

}
		]]>
		</dao>
		<daoImp>
		<![CDATA[
package {2}.dao;

import {2}.entity.{0};

/**
 * 数据访问对象具体实现
 * @author wanghaifeng
 * @email king@126.com
 * @modify {5}
 */
public class {0}DAOImp extends whf.framework.dao.DAOImp<{0}> implements {0}DAO {

}
		]]>
		</daoImp>
		<service>
		<![CDATA[
package {2}.service;

import {2}.entity.{0};

/**
 * 服务对象接口
 * @author wanghaifeng
 * @email king@126.com
 * @modify {5}
 */
public interface {0}Service extends whf.framework.service.Service<{0}> {

}
		]]>
		</service>
		<serviceImp>
		<![CDATA[
package {2}.service;

import whf.framework.exception.ServiceNotFoundException;
import {2}.entity.{0};
import {2}.dao.{0}DAO;
import whf.framework.util.BeanFactory;

/**
 * 服务对象具体实现,委托相应的dao对象实现相关操作
 * @author wanghaifeng
 * @email king@126.com
 * @modify {5}
 */
public class {0}ServiceImp extends whf.framework.service.ServiceImp<{0}> implements {0}Service {
	public void set{0}DAO({0}DAO {1}DAO) {
		this.entityDAO = {1}DAO;
	}
	
	/**
	 * 静态的简单的服务获取方法
	 */
	public static {0}Service get{0}Service() throws ServiceNotFoundException {
		return ({0}Service)BeanFactory.getService({0}ServiceImp.class) ;
	}
}
		]]>
		</serviceImp>
		<webservice>
		<![CDATA[
package {2}.webservice;

import javax.jws.WebService;

import {2}.entity.{6};

import whf.framework.exception.CreateException;
import whf.framework.exception.NotFoundException;
import whf.framework.exception.RemoveException;
import whf.framework.exception.UpdateException;

/**
 * web服务接口,定义远程服务可以使用的方法
 * @author wanghaifeng
 * @email king@126.com
 * @modify {5}
 */
@WebService(targetNamespace="http://whf.framework/{0}Service")
public interface {0}ServiceWS {

	public void create({6} {1}) throws CreateException;

	public void update({6} {1}) throws UpdateException;

	public void remove({6} {1}) throws RemoveException;

	public void remove(long id) throws RemoveException;

	public void remove(long[] ids) throws RemoveException;

	public {6} findByPrimaryKey(long id) throws NotFoundException;

}
		]]>
		</webservice>
		<webserviceImp>
		<![CDATA[
package {2}.webservice;

import javax.jws.WebService;

import {2}.entity.{6};
import {2}.service.{0}Service;

import whf.framework.exception.CreateException;
import whf.framework.exception.NotFoundException;
import whf.framework.exception.RemoveException;
import whf.framework.exception.UpdateException;

/**
 * web服务的实现,委托具体的本地服务类实现相关操作
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2006-10-31
 */
@WebService(serviceName = "{0}Service", endpointInterface = "{2}.service.{0}ServiceWS")
public class {0}ServiceWSImp implements {0}ServiceWS {
	/**
	 * 委托服务
	 */
	private {0}Service service;

	public void setService({0}Service service) {
		this.service = service;
	}

	public void create({6} {1}) throws CreateException {
		this.service.create({1});
	}

	public void update({6} {1}) throws UpdateException {
		this.service.update({1});
	}

	public void remove({6} {1}) throws RemoveException {
		this.service.remove({1});
	}

	public void remove(long id) throws RemoveException {
		this.service.remove(id);
	}

	public void remove(long[] ids) throws RemoveException {
		this.service.remove(ids);
	}

	public {6} findByPrimaryKey(long id) throws NotFoundException {
		return this.service.findByPrimaryKey(id);
	}
} 
		]]>
		</webserviceImp>
		<dispatch-action>
		<![CDATA[
package {2}.web;

import whf.framework.web.struts.WebDispatchAction;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify {5}
 */
public class {0}Action extends WebDispatchAction {

}
		]]>
		</dispatch-action>
	</java>
	<xml>
		<meta>
		<![CDATA[
	<meta package="{2}"> 
		<rowsPerPage>10</rowsPerPage>
		<name>{0}</name>
		<description/>
		<serviceName>{0}Service</serviceName>
		<daoName>{0}DAO</daoName>
		<modelClass>{0}</modelClass>
		<actionClass>{0}Action</actionClass>
		<serviceClass>{0}ServiceImp</serviceClass>
		<daoClass>{0}DAOImp</daoClass>
		<persistenceTable>{3}</persistenceTable>
		<parentModelClass/>
	</meta> 
		]]>
		</meta>
		<spring>
	<![CDATA[
	<bean id="{4}.{0}DAO" class="{2}.dao.{0}DAOImp">
		<property name="daoSupport">
			<ref bean="daoSupport"/>
		</property>
	</bean>
	<bean id="{4}.{0}Service" parent="txProxyTemplate">
		<property name="transactionManager">
			<ref bean="transactionManager"/>
		</property>
		<property name="target">
			<bean id="{4}.{0}ServiceTarget" class="{2}.service.{0}ServiceImp">
				<property name="{1}DAO">
					<ref bean="{4}.{0}DAO"/>
				</property>
			</bean>
		</property>
	</bean>
	]]>
		</spring>
		<spring2>
		<![CDATA[
	<bean id="{0}DAO" class="{2}.dao.{0}DAOImp"> 
		<property name="sessionFactory"><ref bean="sessionFactory"/></property> 
	</bean> 
	<bean id="{0}Service" parent="txProxyTemplate"> 
		<property name="target"> 
			<bean class="{2}.service.{0}ServiceImp"> 
				<property name="{1}DAO"><ref bean="{0}DAO"/></property> 
			</bean> 
		</property>
	</bean> 
		]]>
		</spring2>
		<hibernate></hibernate>
		<struts>
		<![CDATA[
	<form-bean name="{4}{0}Form" 	type="{2}.entity.{0}"/> 

	<action path="/{4}/{0}" type="{2}.web.{0}Action" name="{4}{0}Form" scope="request" parameter="webMethod" validate="true">
		<forward name="list" 	path="/common/global/list_jsp"/>
		<forward name="edit" 	path="/common/global/edit_jsp"/>
		<forward name="view" 	path="/common/global/view_jsp"/>
	</action>
		]]>
		</struts>
		<webservice>
		<![CDATA[
	<bean id="{0}WS" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <property name="serviceFactory">
            <ref bean="xfire.serviceFactory"/>
        </property>
        <property name="xfire">
            <ref bean="xfire"/>
        </property>
        <property name="serviceBean">
            <ref bean="{0}Service"/>
        </property>
        <property name="serviceClass">
            <value>{2}.service.{0}ServiceWSImp</value>
        </property>
    </bean>
		]]>
		</webservice>
	</xml>
	<jsp>
		<edit>
		<![CDATA[
<%@page contentType="text/html;charset=UTF-8"%>
<%@ page import="whf.framework.web.Constants"%>

<%@taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/tlds/framework.tld" prefix="framework"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><bean:write name="STRUTS-META" property="name" /></title>
<jsp:include page="/inc/select.jsp" />
</head>
<body>

<html:form action="<%=  (String)request.getAttribute(Constants.STRUTS_PATH) %>" method="post">
<html:hidden property="webMethod" />
<framework:ext />
<framework:table pkStyle="" navPosition="none" pageSize="1" displayStyle="form" editable="true" useStrutsForm="true">
<framework:caption><span class="whf_object_title"><framework:attr source="STRUTS-META" attr="name" /></span></framework:caption>
</framework:table>
<html:submit><bean:message key="form.submit" /></html:submit>
<html:reset><bean:message key="form.reset" /></html:reset>
</html:form>

</body>
</html> 
		]]>
		</edit>
		<list>
		<![CDATA[
<%@page contentType="text/html;charset=UTF-8"%>
<%@ page import="whf.framework.web.Constants"%>

<%@taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/tlds/framework.tld" prefix="framework"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><framework:attr source="STRUTS-META" attr="name" /></title>
<jsp:include page="/inc/select.jsp" />
</head>

<body>
<html:form action='<%=  (String)request.getAttribute(Constants.STRUTS_PATH) %>' method="post">
<html:hidden property="webMethod" value="list" />
<framework:ext />
<framework:table pkStyle="checkbox" navPosition="both" queryString="">
<framework:caption><span class="whf_object_title"><framework:attr source="STRUTS-META" attr="name" /></span></framework:caption>
</framework:table>
<framework:funcbtn operation="prepareCreate" validationFunction="" valueKey="form.create" />
<framework:funcbtn operation="prepareUpdate" validationFunction="selectionMoreThanOne" valueKey="form.update" />
<framework:funcbtn operation="duplicate" 	validationFunction="selectionMoreThanOne" valueKey="form.duplicate" />
<framework:funcbtn operation="delete" 		validationFunction="selectionMoreThanOne" valueKey="form.delete" />
<html:reset><bean:message key="form.reset" /></html:reset>
</html:form>

</body>
</html> 
		]]>
		</list>
		<view>
		<![CDATA[
<%@page contentType="text/html;charset=UTF-8"%>
<%@ page import="whf.framework.web.Constants"%>

<%@taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/tlds/framework.tld" prefix="framework"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><bean:write name="STRUTS-META" property="name" /></title>
<jsp:include page="/inc/select.jsp" />
</head>
<body>

<html:form action="<%=  (String)request.getAttribute(Constants.STRUTS_PATH) %>" method="post">
<html:hidden property="id" />
<html:hidden property="webMethod" />
<framework:table pkStyle="" navPosition="none" pageSize="1" displayStyle="form">
<framework:caption><span class="whf_object_title"><framework:attr source="STRUTS-META" attr="name" /></span></framework:caption>
</framework:table>
<input type='button' onclick='window.history.back()' value='<bean:message key="form.return" />' />
<html:reset><bean:message key="form.reset" /></html:reset>
</html:form>

</body>
</html> 
		]]>
		</view>
	</jsp>
</root>
0 请登录后投票
论坛首页 Java企业应用版

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