`
viwo
  • 浏览: 220282 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

"Struts"的简易实现

    博客分类:
  • JAVA
阅读更多
java 代码
  1. package com.viwo.web.acion;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5. import java.lang.reflect.Method;   
  6. import java.util.Map;   
  7.   
  8. import javax.servlet.RequestDispatcher;   
  9. import javax.servlet.ServletException;   
  10. import javax.servlet.http.HttpServlet;   
  11. import javax.servlet.http.HttpServletRequest;   
  12. import javax.servlet.http.HttpServletResponse;   
  13.   
  14. import org.apache.commons.digester.Digester;   
  15. import org.apache.log4j.Logger;   
  16. import org.xml.sax.SAXException;   
  17.   
  18. import com.viwo.web.config.ConfigAction;   
  19. import com.viwo.web.config.ConfigForward;   
  20. import com.viwo.web.config.ViwoConfig;   
  21.   
  22. /**  
  23.  * the Frameword's entry,the Controller of All  
  24.  *   
  25.  * @author viwo   
  26.  *    
  27.  */  
  28. public class ViwoServlet extends HttpServlet {   
  29.      
  30.  private final static Logger logger = Logger.getLogger(ViwoServlet.class);   
  31.     public void doGet(HttpServletRequest request,   
  32.                HttpServletResponse response)   
  33.          throws IOException, ServletException {   
  34.     
  35.         process(request, response);   
  36.   
  37.      }   
  38.   
  39.      public void doPost(HttpServletRequest request,   
  40.                 HttpServletResponse response)   
  41.          throws IOException, ServletException {   
  42.        
  43.          process(request, response);   
  44.   
  45.      }   
  46.      
  47.      /**  
  48.       * the main method of ViwoServlet  
  49.       *   
  50.       * get the properties from web.xml and viwo-config.xml  
  51.       *   
  52.       * use these properties and url info to initialize and select appropriate action   
  53.       * and method to go.  
  54.       *   
  55.       * @param request  
  56.       * @param response  
  57.       */  
  58.      protected void process(HttpServletRequest request,   
  59.                 HttpServletResponse response)   
  60.       throws IOException, ServletException {   
  61.          String config = getServletConfig().getInitParameter("config");   
  62.          String template = getServletConfig().getInitParameter("template");   
  63.          logger.info("[viwo]-->[enter process]-->[config:]"+config);   
  64.             
  65.          String forward = "";   
  66.          Object returnValue = null;   
  67.             String url = request.getRealPath(config);   
  68.             logger.debug("[viwo]-->[process]-->[url:]"+url);   
  69.                
  70.             String requestURI = request.getRequestURI();   
  71.             int lastdot = requestURI.lastIndexOf(".");   
  72.             int lastbias = requestURI.lastIndexOf("/");   
  73.             int lastbiasbutone = requestURI.lastIndexOf("/",lastbias-1);   
  74.             String method = requestURI.substring(lastbias+1,lastdot);   
  75.             String action = requestURI.substring(lastbiasbutone+1,lastbias);   
  76.             Object actionObject = null;   
  77.             try  
  78.             {      
  79.              ViwoConfig viwoConfig = getViwoConfig(url);   
  80.              Map cfgActionMap  = viwoConfig.getActionMap();   
  81.              Map cfgForwardMap = viwoConfig.getForwardMap();   
  82.                 
  83.              if(cfgActionMap.get(action)!=null)   
  84.              {   
  85.               ConfigAction ca = (ConfigAction)cfgActionMap.get(action);   
  86.               action = ca.getType();   
  87.              }   
  88.                 
  89.              actionObject = Class.forName(action).newInstance();   
  90.                 Method m = getMethod(actionObject,method);   
  91.                 if(m != null)   
  92.                 {   
  93.                  returnValue = m.invoke(actionObject,new Object[]{request,response});   
  94.                  //如果方法调用返回的是null,则直接返回。   
  95.                  if(returnValue==null)   
  96.                   return;   
  97.                  //根据web.xml中配置的template参数决定选择使用哪种模板引擎。   
  98.                  if(template!=null)   
  99.                   forward = "/"+returnValue+"."+template;   
  100.                  else  
  101.                   forward = "/"+returnValue+".jsp";   
  102.                     
  103.                  if(cfgForwardMap.get(returnValue)!=null)   
  104.                  {   
  105.                   ConfigForward cf = (ConfigForward)cfgForwardMap.get(returnValue);   
  106.                   forward = cf.getPath();   
  107.                  }   
  108.        
  109.                  logger.debug("[viwo]-->[process]-->[forward:]"+forward);   
  110.                  RequestDispatcher rd = getServletContext().getRequestDispatcher(forward);   
  111.                  rd.forward(request, response);   
  112.                 }   
  113.                 else  
  114.                 {   
  115.                  logger.error("[viwo]-->[process]-->"+"There haven't the "+method+" Method,Please check URL!");   
  116.                  response.getWriter().print("There haven't the "+method+" Method,Please check URL!");   
  117.                 }   
  118.                    
  119.    }    
  120.             catch (ClassNotFoundException e)    
  121.    {   
  122.              logger.error("[viwo]-->[process]-->"+"There haven't the "+action+" Class,Please check URL!");   
  123.              response.getWriter().print("There haven't the "+action+" Class,Please check URL!");   
  124.    }    
  125.             catch (NoClassDefFoundError e)   
  126.             {   
  127.              logger.error("[viwo]-->[process]-->"+"There haven't the "+action+" Class,Please check URL!");   
  128.              response.getWriter().print("There haven't the "+action+" Class,Please check URL!");   
  129.             }   
  130.             catch (Exception e)   
  131.             {   
  132.              System.out.println(e.getMessage());   
  133.              logger.error("[viwo]-->[process]-->"+e.getMessage());   
  134.             }   
  135.     
  136.      }   
  137.         
  138.      /**  
  139.       * the method for parsing config file for ViwoServlet  
  140.       *   
  141.       * put the properties from viwo-config.xml to and object of ViwoConfig  
  142.       *   
  143.       * @param url  
  144.       * @return ViwoConfig  
  145.       */  
  146.      private ViwoConfig getViwoConfig(String url) throws IOException, SAXException   
  147.      {   
  148.       Digester digester = new Digester();     
  149.          digester.setValidating(false);    
  150.          digester.addObjectCreate("viwo-config""com.viwo.web.config.ViwoConfig");   
  151.             
  152.          digester.addObjectCreate("viwo-config/action""com.viwo.web.config.ConfigAction");   
  153.          digester.addSetProperties("viwo-config/action");   
  154.          digester.addSetNext("viwo-config/action""addAction""com.viwo.web.config.ConfigAction");   
  155.             
  156.          digester.addObjectCreate("viwo-config/forward""com.viwo.web.config.ConfigForward");   
  157.          digester.addSetProperties("viwo-config/forward");   
  158.          digester.addSetNext("viwo-config/forward""addForward""com.viwo.web.config.ConfigForward");   
  159.          ViwoConfig viwoConfig = (ViwoConfig) digester.parse(new File(url));   
  160.          return viwoConfig;   
  161.             
  162.      }   
  163.         
  164.      /**  
  165.       * get the actionObject's method Method  
  166.       *  
  167.       * @param actionObject  
  168.       * @param method  
  169.       * @return Method  
  170.       */  
  171.      private Method getMethod(Object actionObject,String method)    
  172.        throws IOException   
  173.      {   
  174.               
  175.      Method[] methods = actionObject.getClass().getMethods();   
  176.      if(methods==null||methods.length==0)   
  177.      {   
  178.       return null;   
  179.      }   
  180.      for(int i=0;i      {   
  181.       if(methods[i].getName().equals(method))   
  182.        return methods[i];   
  183.      }   
  184.      return null;   
  185.       
  186.      }   
  187.   
  188. }   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics