2016-05-31 24 views
0

MVCアーキテクチャをモデルにしたWebアプリケーションを構築しています。 JSPページのフォーム・データは、コントローラーにデリゲートするDispatcherサーブレットに転送されます。私は単一のコントローラを注入することができましたし、うまく動作します。マップを使用してコントローラを挿入しようとするとNullPointerExceptionが発生する

複数のコントローラの場合、プロパティファイルを読み込んで関連するコントローラに要求をマップするマッピングハンドラをサーブレットに作成しました。私はマッピングハンドラを注入しましたが、私はまだNullPointerExceptionを取得しています。

ControllerBundleを追加すると、コントローラとメソッド名が1つのオブジェクトに抽象化されます。私はwebprofile tomeeを使用しています1.7.4

DispatcherServlet.java:

package a1; 

// imports 

public class DispatcherServlet extends HttpServlet { 

    @Inject 
    @Named("MappingHandler") 
    private MappingHandler MAPPING_HANDLER; 

    // ** Single controller ** 
    // @Inject 
    // @Named("customerController") 
    // private CustomerController controller; 

    @Override 
    public void init() throws ServletException { 
     MAPPING_HANDLER.generate(); 
    } 

    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     try { 
      String pathInfo = req.getPathInfo(); 
      Controller controller = MAPPING_HANDLER.getController(pathInfo); 
      String methodName = MAPPING_HANDLER.getMethod(pathInfo); 
      Method method = controller.getClass().getDeclaredMethod(methodName, HttpServletRequest.class); 

      method.setAccessible(true); 
      method.invoke(controller, req); 
      req.getRequestDispatcher("/results.jsp").forward(req, resp); 

      // ** Single controller ** 
      // String methodName = "addCustomer"; 
      // Method method = controller.getClass().getDeclaredMethod(methodName, HttpServletRequest.class); 
      // 
      // method.setAccessible(true); 
      // method.invoke(controller, req); 
      // req.getRequestDispatcher("/success.jsp").forward(req, resp); 

     } catch (SecurityException | IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 

MappingHandler.java:

package a1; 

// imports 

@Named("MappingHandler") 
public class MappingHandler { 

    private static final Map<String, ControllerBundle> CONTROLLER_BUNDLE_MAP = new HashMap<>(); 

    private static final String PACK = "a1."; 

    void generate() { 
     try { 
      Properties props = new Properties(); 
      InputStream is1 = this.getClass().getClassLoader().getResourceAsStream("mappings_h6.properties"); 
      props.load(is1); 

      Enumeration e = props.propertyNames(); 
      while (e.hasMoreElements()) { 
       String left = (String) e.nextElement(); 
       String right = props.getProperty(left); 

       // ControllerBundle: abstraction containing both the Controller and the name of the method 
       ControllerBundle controllerBundle = new ControllerBundle(right, PACK); 
       CONTROLLER_BUNDLE_MAP.put(left, controllerBundle); 
      } 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    Controller getController(String pathInfo) { 
     ControllerBundle controllerBundle = CONTROLLER_BUNDLE_MAP.get(pathInfo); 
     Controller controller = controllerBundle.getController(); 
     return controller; 
    } 

    String getMethod(String pathInfo) { 
     ControllerBundle controllerEntity1 = CONTROLLER_BUNDLE_MAP.get(pathInfo); 
     String methodName = controllerEntity1.getMethodName(); 
     return methodName; 
    } 
} 

ControllerBundle.java:

package a1; 

// imports 

public class ControllerBundle { 

    private Controller controller; 
    private String methodName; 
    private static final Map<String, Controller> CONTROLLER_MAP = new HashMap<>(); 

    public ControllerBundle(String value, String pack) { 
     String[] valueSplit = value.split("#"); 

     String controllerName = valueSplit[0]; 
     Controller controller = findController(pack, controllerName); 
     String methodName = valueSplit[1]; 

     this.controller = controller; 
     this.methodName = methodName; 
    } 

    // Keep only one copy of every controller 
    private static Controller findController(String pack, String controllerName) { 
     try { 

      Class currentControllerClass = Class.forName(pack + controllerName); 
      for (String s : CONTROLLER_MAP.keySet()) { 
       Class existingControllerClass = CONTROLLER_MAP.get(s).getClass(); 
       if (currentControllerClass == existingControllerClass) { 
        return (Controller) existingControllerClass.newInstance(); 
       } 
      } 

      Controller currentController = (Controller) currentControllerClass.newInstance(); 
      CONTROLLER_MAP.put(controllerName, currentController); 
      return currentController; 

     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    Controller getController() { 
     return controller; 
    } 

    String getMethodName() { 
     return methodName; 
    } 
} 

mappings_h6.properties:

あなたは、WEB-INFまたはWEB-INF /クラス/ META-INFでbeans.xmlのを

beans.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" 
     bean-discovery-mode="all"> 
</beans> 

答えて

-1

を持っているのですか?それ以外の場合、tomee 1.x CDIはアクティブ化されません。

+0

はい、私はbeans.xmlを追加しました。 – Irfaan

+0

あなたが追加したスニペットは、TomEE 1に対してはEE 7 beans.xmlが無効です(TomEE 7には有効です)。 「」と試してみてください –

関連する問題