java中struts 架構的實現_java

來源:互聯網
上載者:User

該文章主要簡單粗暴的實現了struts的請求轉寄功能。 其他的功能後續會慢慢補上。

最近在學習javassist的內容,看到一篇文章  大家一起寫mvc  主要簡單的描述了mvc的工作流程,同時實現了簡單的struts2功能。

這裡仿照的寫了個簡單的struts2架構,同時加上了自己的一些理解。

該文章主要簡單粗暴的實現了struts的請求轉寄功能。 其他的功能後續會慢慢補上。

首先,在struts2架構中,請求的實現、跳轉主要是通過在struts.xml進行相關配置。 一個<action>標籤表示一個請求的定義,action中包含了①請求的名稱“name”;②請求對應的實作類別“class” ;③同時還可通過“method”屬性自訂執行的方法,若沒配置預設執行execute0方法。<result》標籤定義了①結果的類型“name”,包括'SUCCESS'、'NONE'、'LOGIN'、'INPUT'、'ERROR';②請求的類型“type”,包括'dispatcher(預設)'、'chain'、'redirect'、'redirectAction'、'stream';③結果的跳轉。 在配置完struts.xml後,介面中的表單就可以通過action屬性與action定義的name屬性值相匹配找到對應的action標籤,從而找到對應的class以及執行的方法。再根據執行方法返回的string字串同result標籤中的name相匹配,根據定義的type類型,進行下一步請求操作。

好了,在瞭解了struts2是怎麼將介面請求同程式功能相串連後,我們通過自己的代碼來實現這部分的功能。

那麼,我們該如何下手了?

我們將需要實現的功能簡單的分為兩部分 ①action部分 ②result部分

   action部分

       ①我們需要根據介面的請求找到對應的類以及執行的方法

    result部分

        ①我們需要根據方法執行的邏輯返回'SUCCESS'、'NONE'、'LOGIN'、'INPUT'、'ERROR'這類型的字串
 
        ②需要對不同的傳回型別,指定不同的下一步請求地址
 
        ③需要定義請求的類型,包括'dispatcher(預設)'、'chain'、'redirect'、'redirectAction'、'stream'

在本文章中,result的傳回型別只實現了'SUCCESS'、'LOGIN'兩種,並且暫不考慮請求類型,實現的是預設的dispatcher請求轉寄類型。完善的功能後期會再補充。

那麼,下面我們來通過代碼看怎麼實現如上功能。 

首先定義了ActionAnnotation和ResultAnnotation 兩個自訂註解來請求需要對應的方法以及方法返回的字串對應的跳轉請求

 /**  * action註解:ActionName相當於web.xml配置中的url-pattern   * @author linling  *  */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface ActionAnnotation {   String ActionName() default "";   ResultAnnotation[] results() default {}; }   /**    * 返回註解對象:name相當於struts配置中的result的name,包括'SUCCESS'、'NONE'、'ERROR'、'INPUT'、'LOGIN';value相當於struts配置中對應返回跳轉內容    * @author linling    *    */   @Retention(RetentionPolicy.RUNTIME)   @Target(ElementType.METHOD)   public @interface ResultAnnotation   {     ResultType name() default ResultType.SUCCESS;     String value() default "index.jsp";   }  

然後我們定義一個ActionContext類,來儲存一個請求所需要的內容

 /**  * 實現類比struts根據設定檔跳轉至action執行相應方法中需要的內容  * @author linling  *  */ public class ActionContext {   /**    * 相當於web.xml中url-pattern,唯一的    */   private String Url;       /**    * ActionAnnotation註解對應方法,也就是action中要執行的方法    */   private String method;       /**    * ActionAnnotation中的Result,對應action方法返回的類型。例如:key:'SUCCESS';value:'index.jsp'    */   private Map<ResultType, String> results;       /**    * action的類    */   private Class<?> classType;       /**    * action的對象    */   private Object action;       /**    * 方法參數類型    */   private Class<?>[] paramsType;       /**    * 方法參數的名稱,注意這裡方法名稱需要和上面paramType參數一一對應    * 可以理解為是struts中action中的屬性    */   private String[] actionParamsName;       /**    * 本次請求的HttpServletRequest    */   private HttpServletRequest request;       /**    * 本次請求的HttpServletResponse    */   private HttpServletResponse response;   

analysePackage是在組裝ActionContext需要的方法

  /**      * 遍曆scan_package包下的class檔案,將使用了ActionAnnotation註解的方法進行解析,組裝成ActionContext對象 並放入urlMap中      * @param real_path      * @param scan_package      * @throws ClassNotFoundException      * @throws InstantiationException      * @throws IllegalAccessException      * @throws NotFoundException      */     public static void analysePackage(String real_path, String scan_package) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NotFoundException     {       File file = new File(real_path);       if(file.isDirectory())       {         File[] files = file.listFiles();         for(File f : files)         {           analysePackage(f.getAbsolutePath(),scan_package);         }       }       else       {         String str = real_path.replaceAll("/", ".");         if (str.indexOf("classes." + scan_package) <= 0 || !str.endsWith(".class"))         {           return;         }         String fileName = str.substring(str.indexOf(scan_package),str.lastIndexOf(".class"));         Class<?> classType = Class.forName(fileName);         Method[] methods = classType.getMethods();         for(Method method : methods)         {           if(method.isAnnotationPresent(ActionAnnotation.class))           {             ActionContext actionContext = new ActionContext();             ActionAnnotation actionAnnotation = (ActionAnnotation)method.getAnnotation(ActionAnnotation.class);             String url = actionAnnotation.ActionName();             ResultAnnotation[] results = actionAnnotation.results();             if(url.isEmpty() || results.length < 1)             {               throw new RuntimeException("method annotation error! method:" + method + " , ActionName:" + url + " , result.length:" + results.length);             }             actionContext.setUrl(url);             actionContext.setMethod(method.getName());             Map<ResultType, String> map = new HashMap<ResultType, String>();             for(ResultAnnotation result : results)             {               String value = result.value();               if(value.isEmpty())               {                 throw new RuntimeException("Result name() is null");               }               map.put(result.name(), value);             }             actionContext.setResults(map);             actionContext.setClassType(classType);             actionContext.setAction(classType.newInstance());             actionContext.setParamsType(method.getParameterTypes());             actionContext.setActionParamsName(getActionParamsName(classType, method.getName()));             urlMap.put(url, actionContext);           }         }       }     }   

getParams是根據httpServletRequest請求中的請求內容獲得請求參數數組,該參數數組為調用方法體的參數內容

  /**      * 根據 參數類型parasType 和 參數名actinParamsName 來解析請求request 構建參數object[]      * @param request      * @param paramsType      * @param actionParamsName      * @return      * @throws InstantiationException      * @throws IllegalAccessException      * @throws IllegalArgumentException      * @throws InvocationTargetException      * @throws NoSuchMethodException      * @throws SecurityException      */     public static Object[] getParams(HttpServletRequest request, Class<?>[] paramsType, String[] actionParamsName) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException     {       Object[] objects = new Object[paramsType.length];       for(int i = 0; i < paramsType.length; i++)       {         Object object = null;         if(ParamsUtils.isBasicType(paramsType[i]))         {           objects[i] = ParamsUtils.getParam(request, paramsType[i], actionParamsName[i]);         }         else         {           Class<?> classType = paramsType[i];           object = classType.newInstance();           Field[] fields = classType.getDeclaredFields();           for(Field field : fields)           {             Map<String, String[]> map = request.getParameterMap();             for(Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext();)             {               String key = iterator.next();               if(key.indexOf(".") <= 0)               {                 continue;               }               String[] strs = key.split("\\.");               if(strs.length != 2)               {                 continue;               }               if(!actionParamsName[i].equals(strs[0]))               {                 continue;               }               if(!field.getName().equals(strs[1]))               {                 continue;               }               String value = map.get(key)[0];               classType.getMethod(convertoFieldToSetMethod(field.getName()), field.getType()).invoke(object, value);               break;             }           }           objects[i] = object;         }       }       return objects;     }   

好了,接下來。我們可以來實現action方法了

  public class LoginAction   {     @ActionAnnotation(ActionName="login.action",results={@ResultAnnotation(name=ResultType.SUCCESS,value="index.jsp"),@ResultAnnotation(name=ResultType.LOGIN,value="login.jsp")})     public ResultType login(String name, String password)     {       if("hello".equals(name) && "world".equals(password))       {         return ResultType.SUCCESS;       }       return ResultType.LOGIN;     }           @ActionAnnotation(ActionName="loginForUser.action",results={@ResultAnnotation(name=ResultType.SUCCESS,value="index.jsp"),@ResultAnnotation(name=ResultType.LOGIN,value="login.jsp")})     public ResultType loginForUser(int number, LoginPojo loginPojo)     {       if("hello".equals(loginPojo.getUsername()) && "world".equals(loginPojo.getPassword()))       {         return ResultType.SUCCESS;       }       return ResultType.LOGIN;     }   }   

接下來,我們需要做的是讓程式在啟動的時候去遍曆工作目錄下所有類的方法,將使用了ActionAnnotation的方法找出來組裝成ActionContext,這就是我們請求需要執行的方法。這樣在請求到了的時候我們就可以根據請求的地址找到對應的ActionContext,並通過反射的機制進行方法的調用。
 
我們定了兩個Servlet。一個用於執行初始化程式。一個用來過濾所有的action請求

  <servlet>     <servlet-name>StrutsInitServlet</servlet-name>     <servlet-class>com.bayern.struts.one.servlet.StrutsInitServlet</servlet-class>     <init-param>       <param-name>scan_package</param-name>       <param-value>com.bayern.struts.one</param-value>     </init-param>     <load-on-startup>10</load-on-startup>    </servlet>         <servlet>     <servlet-name>DispatcherServlet</servlet-name>     <servlet-class>com.bayern.struts.one.servlet.DispatcherServlet</servlet-class>    </servlet>    <servlet-mapping>     <servlet-name>DispatcherServlet</servlet-name>     <url-pattern>*.action</url-pattern>    </servlet-mapping> 

DispatcherServlet實現了對所用action請求的過濾,並使之執行對應的action方法,以及進行下一步的跳轉

ublic void doPost(HttpServletRequest request, HttpServletResponse response)       throws ServletException, IOException   {       request.setCharacterEncoding("utf-8");     String url = request.getServletPath().substring(1);     ActionContext actionContext = DispatcherServletUtil.urlMap.get(url);     if(actionContext != null)     {       actionContext.setRequest(request);       actionContext.setResponse(response);       try       {         Object[] params = DispatcherServletUtil.getParams(request, actionContext.getParamsType(), actionContext.getActionParamsName());         Class<?> classType = actionContext.getClassType();         Method method = classType.getMethod(actionContext.getMethod(), actionContext.getParamsType());         ResultType result = (ResultType)method.invoke(actionContext.getAction(), params);         Map<ResultType,String> results = actionContext.getResults();         if(results.containsKey(result))         {           String toUrl = results.get(result);           request.getRequestDispatcher(toUrl).forward(request, response);         }         else         {           throw new RuntimeException("result is error! result:" + result);         }                 }  

好了,現在我們已經實現了最簡單的strut2架構的請求轉寄的功能。功能寫得很粗糙,很多情況都還未考慮進來,希望大家多多指點~

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.