OSChina架构中,依照“管理重于配置”的思路使用Servlet设计的MVC

  • 来源:本站
  • 发布时间:2019-08-13
  • 198已阅读
您现在的位置:首页 >> 西方诗歌 >> 文章
简介 ;*;*;;*;*;*;;;;;/***业务处理方法入口,URI的映射逻辑:*/action/xxxxxx/(req,res)*pre林花谢了春红,太匆匆,无奈朝来寒雨晚来风。 胭脂泪,

	OSChina架构中,依照“管理重于配置”的思路使用Servlet设计的MVC

;*;*;;*;*;*;;;;;/***业务处理方法入口,URI的映射逻辑:*/action/xxxxxx/(req,res)*pre林花谢了春红,太匆匆,无奈朝来寒雨晚来风。 胭脂泪,相留醉,几时重,自是人生长恨水长东。 */pre*authorWinterLau(http:///javayou)br*/publicfinalclassActionServletextendsHttpServlet{privatefinalstaticStringERROR_PAGE="error_page";privatefinalstaticStringGOTO_PAGE="goto_page";privatefinalstaticStringTHIS_PAGE="this_page";privatefinalstaticStringERROR_MSG="error_msg";privatefinalstaticStringUTF_8="utf-8";privateListStringaction_packages=null;privatefinalstaticThreadLocalBooleang_json_enabled=newThreadLocalBooleanOverridepublicvoidinit()throwsServletException{Stringtmp=getInitParameter("packages");action_packages=((tmp,,));Stringinitial_actions=getInitParameter("initial_actions");for(Stringaction:(initial_actions,,))try{_LoadAction(action);}catch(Exceptione){log("Failedtoinitialaction:"+action,e);}}Overridepublicvoiddestroy(){for(Objectaction:()){try{Methoddm=().getMethod("destroy");if(dm!=null){(action);log("!!!!!!!!!"+().getSimpleName()+"destroy!!!!!!!!!");}}catch(NoSuchMethodExceptione){}catch(Exceptione){log("Unabledtodestroyaction:"+().getSimpleName(),e);}}();}OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{process((),false);}OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{process((),true);}/***执行Action方法并进行返回处理、异常处理*paramreq*paramresp*paramis_post*throwsServletException*throwsIOException*/protectedvoidprocess(RequestContextreq,booleanis_post)throwsServletException,IOException{try{().setContentType("text/html;charset=utf-8");if(_process(req,is_post)){Stringgp=(GOTO_PAGE);if((gp))(gp);}}catch(InvocationTargetExceptione){Throwablet=();if(tinstanceofActionException)handleActionException(req,(ActionException)t);elseif(tinstanceofDBException)handleDBException(req,(DBException)t);elsethrownewServletException(t);}catch(ActionExceptiont){handleActionException(req,t);}catch(IOExceptione){throwe;}catch(DBExceptione){handleDBException(req,e);}catch(Exceptione){log("Exceptioninactionprocess.",e);thrownewServletException(e);}finally{g_json_();}}/***Action业务异常*paramreq*paramresp*paramt*throwsServletException*throwsIOException*/protectedvoidhandleActionException(RequestContextreq,ActionExceptiont)throwsServletException,IOException{handleException(req,());}protectedvoidhandleDBException(RequestContextreq,DBExceptione)throwsServletException,IOException{log("DBExceptioninactionprocess.",e);handleException(req,("error","database_exception",().getMessage()));}/***URL解码**paramurl*paramcharset*return*/privatestaticString_DecodeURL(Stringurl,Stringcharset){if((url))return"";try{(url,charset);}catch(Exceptione){}returnurl;}protectedvoidhandleException(RequestContextreq,Stringmsg)throwsServletException,IOException{Stringep=(ERROR_PAGE);if((ep)){if((0)==%)ep=_DecodeURL(ep,UTF_8);ep=();if((0)!=/){(()+"/");}else{().setAttribute(ERROR_MSG,msg);(());}}else{if(g_json_())_json("msg",msg);(msg);}}/***业务逻辑处理*paramreq*paramresp*paramis_post_method*throwsIllegalAccessException*throwsInstantiationException*throwsIOException*throwsServletException*throwsIOException*throwsInvocationTargetException*throwsIllegalArgumentException*/privateboolean_process(RequestContextreq,booleanis_post)throwsInstantiationException,IllegalAccessException,IOException,IllegalArgumentException,InvocationTargetException{StringrequestURI=();String[]parts=(requestURI,/);if(){_found();returnfalse;}//加载Action类Objectaction=this._LoadAction(parts[1]);if(action==null){_found();returnfalse;}Stringaction_method_name=()parts[2]:"index";Methodm_action=this._GetActionMethod(action,action_method_name);if(m_action==null){_found();returnfalse;}//判断action方法是否只支持POSTif(!is_postm_()){_found();returnfalse;}g_json_(m_());if(m_()){IUserloginUser=();if(loginUser==null){Stringthis_page=(THIS_PAGE,"");("user_not_login",this_page);}if(())("user_blocked");=()m_();if(()())("user_role_deny");}//调用Action方法之准备参数intarg_c=m_().length;switch(arg_c){case0://login()m_(action);break;case1:m_(action,req);break;case2://login(HttpServletRequestreq,HttpServletResponseres)m_(action,(),());break;case3://login(HttpServletRequestreq,HttpServletResponseres,String[]extParams)StringBuilderargs=newStringBuilder();for(inti=3;;i++){if((parts[i]))continue;if(()0)(/);(parts[i]);}booleanisLong=m_()[2].equals();m_(action,(),(),((),-1L):());break;default:_found();returnfalse;}returntrue;}/***加载Action类*paramact_name*return*throwsInstantiationException*throwsIllegalAccessException*throwsClassNotFoundException*/protectedObject_LoadAction(Stringact_name)throwsInstantiationException,IllegalAccessException{Objectaction=(act_name);if(action==null){for(Stringpkg:action_packages){Stringcls=pkg+.+(act_name)+"Action";action=_LoadActionOfFullname(act_name,cls);if(action!=null)break;}}returnaction;}privateObject_LoadActionOfFullname(Stringact_name,Stringcls)throwsIllegalAccessException,InstantiationException{Objectaction=null;try{action=(cls).newInstance();try{Methodaction_init_method=().getMethod("init",);action_init_(action,getServletContext());}catch(NoSuchMethodExceptione){}catch(InvocationTargetExceptionexcp){();}if(!(act_name)){synchronized(actions){(act_name,action);}}}catch(ClassNotFoundExceptionexcp){}returnaction;}/***获取名为{method}的方法*paramaction*parammethod*return*/privateMethod_GetActionMethod(Objectaction,Stringmethod){Stringkey=().getSimpleName()+.+method;Methodm=(key);if(m!=null)returnm;for(Methodm1:().getMethods()){if(()==().equals(method)){synchronized(methods){(key,m1);}returnm1;}}returnnull;}privatefinalstaticHashMapString,Objectactions=newHashMapString,ObjectprivatefinalstaticHashMapString,Methodmethods=newHashMapString,Method}。