- viwo
- 等级: 初级会员
- 性别:
- 文章: 37
- 积分: 94
- 来自: 大连
|
java 代码
- 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;
-
-
-
-
-
-
-
- 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);
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
- 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});
-
- if(returnValue==null)
- return;
-
- 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());
- }
-
- }
-
-
-
-
-
-
-
-
-
- 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;
-
- }
-
-
-
-
-
-
-
-
- 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;
-
- }
-
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|