コード例 #1
0
 @Override
 public void init(ServletConfig servletConfig) throws ServletException {
   // 初始化 Helper 类
   HelperLoader.init();
   // 获取 ServletContext 对象(用于注册Servlet)
   ServletContext servletContext = servletConfig.getServletContext();
   // 注册处理 JSP 的 Servlet
   ServletRegistration jspServlet = servletContext.getServletRegistration("jsp");
   jspServlet.addMapping(ConfigHelper.getAppJspPath() + "*");
   // 注册处理静态资源的默认 Servlet
   ServletRegistration defaultServlet = servletContext.getServletRegistration("default");
   defaultServlet.addMapping(ConfigHelper.getAppAssetPath() + "*");
 }
コード例 #2
0
 @Override
 protected void service(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   //  获取请求方法与请求路径
   String requestMethod = req.getMethod().toLowerCase();
   String requestPath = req.getPathInfo();
   // 获取 Action 处理器
   Handler handler = ControllerHelper.getHandler(requestMethod, requestPath);
   if (handler != null) {
     // 获取 Controller 类及其 Bean 实例
     Class<?> controllerClass = handler.getControllerClass();
     Object controllerBean = BeanHelper.getBean(controllerClass);
     // 创建请求参数对象
     Map<String, Object> paramMap = new HashMap<String, Object>();
     Enumeration<String> paramNames = req.getParameterNames();
     while (paramNames.hasMoreElements()) {
       String paramName = paramNames.nextElement();
       String paramValue = req.getParameter(paramName);
       paramMap.put(paramName, paramValue);
     }
     String body = CodecUtil.decodeURL(StreamUtil.getString(req.getInputStream()));
     if (StringUtil.isNotEmpty(body)) {
       String[] params = StringUtil.splitString(body, "&");
       if (ArrayUtil.isNotEmpty(params)) {
         for (String param : params) {
           String[] array = StringUtil.splitString(param, "=");
           if (ArrayUtil.isNotEmpty(array) && array.length == 2) {
             String paramName = array[0];
             String paramValue = array[1];
             paramMap.put(paramName, paramValue);
           }
         }
       }
     }
     Param param = new Param(paramMap);
     // 调用 Action 方法
     Method actionMethod = handler.getActionMethod();
     Object result = ReflectionUtil.invokeMethod(controllerBean, actionMethod, param);
     // 处理 Action 方法返回值
     if (result instanceof View) {
       // 返回 JSP 页面
       View view = (View) result;
       String path = view.getPath();
       if (StringUtil.isNotEmpty(path)) {
         if (path.startsWith("/")) {
           resp.sendRedirect(req.getContextPath());
         } else {
           Map<String, Object> model = view.getModel();
           for (Map.Entry<String, Object> entry : model.entrySet()) {
             req.setAttribute(entry.getKey(), entry.getValue());
           }
           req.getRequestDispatcher(ConfigHelper.getAppJspPath() + path).forward(req, resp);
         }
       }
     } else if (result instanceof Data) {
       // 返回 Json 数据
       Data data = (Data) result;
       Object model = data.getModel();
       if (model != null) {
         resp.setContentType("application/json");
         resp.setCharacterEncoding("UTF-8");
         PrintWriter writer = resp.getWriter();
         String json = JsonUtil.toJson(model);
         writer.write(json);
         writer.flush();
         writer.close();
       }
     }
   }
 }