没有在网上公开发部,
与大家一同学习,分享,交流经验
旧版本的代码可以参考我以前的文章, 设计思路上差不多
新版本中加入了 preHandler,postHandler,exceptionHandler等方法, 可自定义实现,这个来自于spring mvc, 其实以前有,只不过不是叫这些名字, 改了之后显得更专业些
加入了 自动事务处理功能 调用start()即可,其后的数据库操作加入事务容器,自动提交,回滚 当然 "事务容器"只是个概念,用把template和proxy即可轻松实现的, 当然也不支持分布式事务拉,不过应付一般项目足以
与webwork类似,action来一个new一个,没有线程安全问题 action里可放心大胆使用成员变量
配置文件在见 ajf.xml
关于跳转路径
有!前缀的使用redirect, 没有的则forward
ajf (agile java framework)
// power magic action ,from struts,webwork and spring mvc
//组合使用 command,template,proxy,decorator,filter,chain等设计模式 //实现了类似aop功能,轻松实现日志,权限,连接,事务等问题 //拥有一个超强魔力的action基类
//还可以覆盖实现preHandler,postHandler,exceptionHandler
//做各种各样的前置后置异常处理等动作
//在这里你可以看到struts,webwork,spring mvc等框架的影子
//BaseAction extends Action //XXXAction extends BaseAction
//一个action多个操作
//根据method参数 ,利用反射调用相应的execute方法,如execute_query
//可采用传统mvc框架配置也可零配置,在jsp页面里直接调用action
一个用户增删改查的例子,所有操作都写在一个action里
下面是BaseAction和UserAction的代码及注释
//------------------ //---------------------------
BaseAction.java
package com.zjuee.action; import java.util.*; import com.zjuee.mvc.*; import com.zjuee.*;
public class BaseAction extends Action{
String action = null; String actionClassName = null; long start = 0; long end = 0; Map ajf_system_request_map = null; long diff=0;
//看到这些方法的名字就晓得是怎么回事了, //分别是前置,后置,异常处理器
//与webwork类似,action来一个new一个,没有线程安全问题 //放心大胆使用成员变量
public String preHandler()throws Exception{ /* LogUtil.info("tiger",StringUtil.getNowTime()); action = AjfUtil.getAction(request); actionClassName = this.getClass().getName(); LogUtil.info("action="+action+",method="+method); LogUtil.info(actionClassName+",pre handler,"+StringUtil.getNowTime()); */ ajf_system_request_map = JspUtil.getRequestModel(request); start=StringUtil.getNowMs(); return null; }
public String postHandler()throws Exception{ /*
LogUtil.info(actionClassName+",post handler,"+StringUtil.getNowTime()); LogUtil.debug(actionClassName+",post handler,"+StringUtil.getNowTime()); LogUtil.warn(actionClassName+",post handler,"+StringUtil.getNowTime()); */ end=StringUtil.getNowMs(); diff=end-start; LogUtil.info(diff+","+JspUtil.getUrl(request)+","+ajf_system_request_map); return null; }
public void exceptionHandler(Exception e)throws Exception{
LogUtil.error(actionClassName+",exception handler,"+e+","+StringUtil.getNowTime()); return; }
}
//------------------------ //----------------------------UserAction.java
package com.zjuee.action;
import com.zjuee.mvc.*; import com.zjuee.*; import java.util.*;
// power magic action ,from struts,webwork and spring mvc
//组合使用 command,template,proxy,decorator,filter,chain等设计模式 //实现了类似aop功能,轻松实现日志,权限,连接,事务等问题 //拥有一个超强魔力的action基类
//还可以覆盖实现preHandler,postHandler,exceptionHandler
//做各种各样的前置后置异常处理等动作
//在这里你可以看到struts,webwork,spring mvc等框架的影子
//BaseAction extends Action //XXXAction extends BaseAction
//一个action多个操作
//根据method参数 ,利用反射调用相应的execute方法,如execute_query
//可采用传统mvc框架配置也可零配置,在jsp页面里直接调用action
public class UserAction extends BaseAction{
public String execute()throws Exception{ return execute_query(); }
public String execute_query()throws Exception{
createTable();
String sql = "select * from t_user"; conn=DBUtil.getConn(); //just get connection //close,commit,rollback, //exception handle in parent class //all is auto //连接的释放,事务处理,异常处理,日志,权限 在父类进行 //手动开启事务,调用start()方法即可, //其后的所有数据库操作将纳入事务管理容器, //自动提交,回滚,关闭Connection //当然不支持分布式事务,不过应付一般的项目足以 //简单的crud操作,在action里直接调用jdbc封装类 //如果业务复杂,则可用service包裹,注入connection即可 //关于分层设计,该分的时候分,该合的时候合 Map map = PagedUtil.queryString(conn,sql,1,1,request); //分页查询,两个标志位分别表示主键列数,是否显示chechbox,供选择记录进行查看,编辑,删除操作 //返回数据表格和工具条,key值分别为data 和 page_bar //如果是大数据量查询,可设置db_query_max_row 参数 add(map); return "q"; //如果在jsp页面里直接调用则返回null即可, //Action action = new UserAction(); //action.run(request,response,"query"); //run(request,respnse,"method_name"); // user$query.do // user$method.do // 根据method参数 ,利用反射调用相应的execute方法,如execute_query //execute 命名方法 }
public String execute_input()throws Exception{
return "input"; }
public String execute_insert()throws Exception{
String table = "t_user"; String cols = "id,name,hh,ww,birth_day"; int autoPK = 1; conn=DBUtil.getConn(); DBUtil.insert(conn,table,cols,autoPK,model); return "!query"; }
public String execute_edit()throws Exception{
Map user = null; String objectid = (String)model.get("objectid"); String sql = "select * from t_user where id='"+objectid+"'"; conn = DBUtil.getConn(); user = DBUtil.queryOne(conn,sql,null); //sql = "select * from t_user where id=?" //user = DBUtil.queryOne(conn,sql,new Object[]{objectid}); if(user==null){ return "no_object"; } model.put("user",user); return "edit"; }
public String execute_view()throws Exception{
Map user = null; String objectid = (String)model.get("objectid"); String sql = "select * from t_user where id='"+objectid+"'"; conn = DBUtil.getConn(); user = DBUtil.queryOne(conn,sql,null); //sql = "select * from t_user where id=?" //user = DBUtil.queryOne(conn,sql,new Object[]{objectid}); if(user==null){ return "no_object"; } model.put("user",user); return "view"; }
public String execute_update()throws Exception{
String table = "t_user"; String cols = "id,name,hh,ww,birth_day"; int autoPK = 1;//主键是否自增 ,max(id)+1 conn = DBUtil.getConn(); DBUtil.updateRow(conn,table,cols,1,model); return "!query"; }
public String execute_delete()throws Exception{
String sql = "delete from t_user where id "; DBUtil.batchDelById(sql,request); return "!query"; }
private void createTable(){ String sql = null; sql = "create table t_user(id int primary key,name varchar(50),hh numeric(8,3),ww numeric(8,3),birth_day datetime)"; try{ DBUtil.update(sql,null,request); }catch(Exception e){} }
}
//--------------------配置文件ajf.xml
<ajf>
<view name="error">/pages/commons/error.jsp</view> <view name="home">/pages/commons/ajf_home.jsp</view> <view name="no_object">/pages/commons/no_object.jsp</view>
<action name="/pages/user/user" class="com.zjuee.action.UserAction"> <view name="query">user.do</view> <view name="q">user_query.jsp</view> <view name="input">user_input.jsp</view> <view name="edit">user_edit.jsp</view> <view name="view">user_view.jsp</view> </action> <action name="/pages/user/x" forward="/pages/user/user.do"/> <action name="/xx" forward="!/pages/user/user.do"/> </ajf>
enjoy! giscat 20061121
附件:ajf1.2_20061121.rar(9K)
|
|