- package com.viwo.web.acion;
- import java.io.File;
- import java.io.IOException;
- import java.lang.reflect.Method;
- import java.util.Map;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.digester.Digester;
- import org.apache.log4j.Logger;
- import org.xml.sax.SAXException;
- import com.viwo.web.config.ConfigAction;
- import com.viwo.web.config.ConfigForward;
- import com.viwo.web.config.ViwoConfig;
- /**
- * the Frameword's entry,the Controller of All
- *
- * @author viwo
- *
- */
- public class ViwoServlet extends HttpServlet {
- private final static Logger logger = Logger.getLogger(ViwoServlet.class);
- public void doGet(HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException {
- process(request, response);
- }
- public void doPost(HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException {
- process(request, response);
- }
- /**
- * the main method of ViwoServlet
- *
- * get the properties from web.xml and viwo-config.xml
- *
- * use these properties and url info to initialize and select appropriate action
- * and method to go.
- *
- * @param request
- * @param response
- */
- protected void process(HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException {
- String config = getServletConfig().getInitParameter("config");
- String template = getServletConfig().getInitParameter("template");
- logger.info("[viwo]-->[enter process]-->[config:]"+config);
- String forward = "";
- Object returnValue = null;
- String url = request.getRealPath(config);
- logger.debug("[viwo]-->[process]-->[url:]"+url);
- String requestURI = request.getRequestURI();
- int lastdot = requestURI.lastIndexOf(".");
- int lastbias = requestURI.lastIndexOf("/");
- int lastbiasbutone = requestURI.lastIndexOf("/",lastbias-1);
- String method = requestURI.substring(lastbias+1,lastdot);
- String action = requestURI.substring(lastbiasbutone+1,lastbias);
- Object actionObject = null;
- try
- {
- ViwoConfig viwoConfig = getViwoConfig(url);
- Map cfgActionMap = viwoConfig.getActionMap();
- Map cfgForwardMap = viwoConfig.getForwardMap();
- if(cfgActionMap.get(action)!=null)
- {
- ConfigAction ca = (ConfigAction)cfgActionMap.get(action);
- action = ca.getType();
- }
- actionObject = Class.forName(action).newInstance();
- Method m = getMethod(actionObject,method);
- if(m != null)
- {
- returnValue = m.invoke(actionObject,new Object[]{request,response});
- //如果方法調用返回的是null,則直接返回。
- if(returnValue==null)
- return;
- //根據web.xml中配置的template參數決定選擇使用哪種模板引擎。
- if(template!=null)
- forward = "/"+returnValue+"."+template;
- else
- forward = "/"+returnValue+".jsp";
- if(cfgForwardMap.get(returnValue)!=null)
- {
- ConfigForward cf = (ConfigForward)cfgForwardMap.get(returnValue);
- forward = cf.getPath();
- }
- logger.debug("[viwo]-->[process]-->[forward:]"+forward);
- RequestDispatcher rd = getServletContext().getRequestDispatcher(forward);
- rd.forward(request, response);
- }
- else
- {
- logger.error("[viwo]-->[process]-->"+"There haven't the "+method+" Method,Please check URL!");
- response.getWriter().print("There haven't the "+method+" Method,Please check URL!");
- }
- }
- catch (ClassNotFoundException e)
- {
- logger.error("[viwo]-->[process]-->"+"There haven't the "+action+" Class,Please check URL!");
- response.getWriter().print("There haven't the "+action+" Class,Please check URL!");
- }
- catch (NoClassDefFoundError e)
- {
- logger.error("[viwo]-->[process]-->"+"There haven't the "+action+" Class,Please check URL!");
- response.getWriter().print("There haven't the "+action+" Class,Please check URL!");
- }
- catch (Exception e)
- {
- System.out.println(e.getMessage());
- logger.error("[viwo]-->[process]-->"+e.getMessage());
- }
- }
- /**
- * the method for parsing config file for ViwoServlet
- *
- * put the properties from viwo-config.xml to and object of ViwoConfig
- *
- * @param url
- * @return ViwoConfig
- */
- private ViwoConfig getViwoConfig(String url) throws IOException, SAXException
- {
- Digester digester = new Digester();
- digester.setValidating(false);
- digester.addObjectCreate("viwo-config", "com.viwo.web.config.ViwoConfig");
- digester.addObjectCreate("viwo-config/action", "com.viwo.web.config.ConfigAction");
- digester.addSetProperties("viwo-config/action");
- digester.addSetNext("viwo-config/action", "addAction", "com.viwo.web.config.ConfigAction");
- digester.addObjectCreate("viwo-config/forward", "com.viwo.web.config.ConfigForward");
- digester.addSetProperties("viwo-config/forward");
- digester.addSetNext("viwo-config/forward", "addForward", "com.viwo.web.config.ConfigForward");
- ViwoConfig viwoConfig = (ViwoConfig) digester.parse(new File(url));
- return viwoConfig;
- }
- /**
- * get the actionObject's method Method
- *
- * @param actionObject
- * @param method
- * @return Method
- */
- private Method getMethod(Object actionObject,String method)
- throws IOException
- {
- Method[] methods = actionObject.getClass().getMethods();
- if(methods==null||methods.length==0)
- {
- return null;
- }
- for(int i=0;i {
- if(methods[i].getName().equals(method))
- return methods[i];
- }
- return null;
- }
- }
"Struts"的簡易實現
java 代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.