歡迎您光臨本站 註冊首頁

超級簡單的mvc框架ajf1.2發布

←手機掃碼閱讀     火星人 @ 2014-03-10 , reply:0

沒有在網上公開發部,與大家一同學習,分享,交流經驗,舊版本的代碼可以參考我以前的文章,設計思路上差不多.新版本中加入了preHandler,postHandler,exceptionHandler等方法,可自定義實現,這個來自於spring mvc,其實以前有,只不過不是叫這些名字,改了之後顯得更專業些

加入了 自動事務處理功能
調用start()即可,其後的資料庫操作加入事務容器,自動提交,回滾
當然 "事務容器"只是個概念,用把template和proxy即可輕鬆實現的,當然也不支持分散式事務拉,不過應付一般項目足以與webwork類似,action來一個new一個,沒有線程安全問題action里可放心大膽使用成員變數

配置文件在見jf.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


[火星人 ] 超級簡單的mvc框架ajf1.2發布已經有619次圍觀

http://coctec.com/docs/java/show-post-61913.html