ESK的个人空间

鞋服行业、系统架构、ERP性能分析、Java技术、工作流、商业智能

  博客园 :: :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

      表示层我们选用的是struts1.1,为什么选用它,而不选用spring MVC或者是struts2,没什么特别的原因,主是因为

我们熟,因为struts1.1里面的代码我们都有研究过.

     直接用表示层肯定不能满足业务需求,现在任何一个系统,都可能会用大量的Ajax,所以我们进行了改写,改写重要参

考就是springside2.

    1):我们把ActionForward抽象出来,进行封装,以便可以支持不同类型的数据格式,比如:jsp,json,Excel,pdf,velocity等

       定义一个struts Forward接口

       public interface IStrutsForward { 

         public abstract ActionForward findForward(ActionContext context);
      } 

    定义一个抽象类,实现这个接口,主要的目的是把一些公共的抽出来.

  public abstract class AbstractStrutsForward implements IStrutsForward {
      final public static String CONTENTTYPE_TEXT = "text/plain;charset=UTF-8"; 

      final public static String CONTENTTYPE_XML = "text/xml;charset=UTF-8"; 

      final public static String CONTENTTYPE_HTML = "text/html;charset=UTF-8"; 

      final public static String CONTENTTYPE_JSON = "text/x-json;charset=UTF-8"; 

      final public static String CONTENTTYPE_PDF = "application/pdf"; 

      final public static String CONTENTTYPE_EXCEL = "application/vnd.ms-excel";
      final public static String CONTENTTYPE_download = "application/x-download"; 

      protected Object forward = null;

      ……

    }

 不同的类型继承抽象类,实现接口,比如:

    public class JspForward extends AbstractStrutsForward{
       public JspForward (Object forward){
           super(forward);
       }
       /**
        * 导向struts Forward
        * @param context
        * @return  
        * @see com.esk2.framework.web.action.IStrutsForward#findForward(com.esk2.framework.web.action.ActionContext)
        */    
       public ActionForward findForward(ActionContext context){
           if(forward instanceof String){
               return context.getMapping().findForward((String)forward);
           }else{
               SystemLogger.error("Forward类型应该为String,而不是:"+forward.toString());
               return context.getMapping().findForward(SysFinal.STRUTS_SYSTEM_ERROR);
           }
       }
}

  2):因为Action是直接将页面传过来的method参数,dispatch到Manager里面调用,所以表示层,我们借鉴了struts自带的DispatchAction的原理,再结合spring,就很好的实现了这一功能。

@Override
    public IStrutsForward dispatch(ActionContext context) {

        //从spring取到Manager
        Object service = this.createService(context);
        if (service instanceof IStrutsForward) {
            IStrutsForward forward = (IStrutsForward) service;
            return forward;
        }

       //获取Method方法
        String methodStr = context.getForm().getMethod(); 

        Object methodObj = this.createMethod(service, methodStr);
        if (methodObj instanceof IStrutsForward) {
            IStrutsForward forward = (IStrutsForward) methodObj;
            return forward;
        } 

        try {
            Object args[] = { context };
            Method method = (Method) methodObj;
            long begin=System.currentTimeMillis();

            //调用Manager里面的Method方法
            IStrutsForward forward = (IStrutsForward) method
                    .invoke(service, args);
            long end=System.currentTimeMillis();
            StringBuffer msg=new StringBuffer();
            msg.append("[");
            msg.append(context.getRequest().getRemoteAddr());
            msg.append("]");
            msg.append(ClassUtils.getShortName(service.getClass()));
            msg.append(".");
            msg.append(methodStr);
            msg.append("(context):");
            msg.append(end-begin);
            SystemLogger.debug(msg.toString());
            return forward;
        } catch (Exception e) {
            SystemLogger.error("调用Serivce 方法出错", e);
            IStrutsForward forward = new JspForward(
                    SysFinal.STRUTS_SYSTEM_ERROR);
            return forward;
        } 

    }

    结束语,整个技术架构记录就告一段路了,不同的人由于经历不同,知识面不同,做出的架构也可能不同,只要你懂基础,懂原理,一定是大同小异。多去钻研一些牛人的源代码,你一定会受益匪浅.

posted on 2011-06-15 20:17  ESK  阅读(512)  评论(0编辑  收藏  举报