Servlets 尝试使用映射注入控制器时出现NullPointerException

Servlets 尝试使用映射注入控制器时出现NullPointerException,servlets,jakarta-ee,model-view-controller,ejb,apache-tomee,Servlets,Jakarta Ee,Model View Controller,Ejb,Apache Tomee,我正在构建一个基于MVC架构的webapp。来自JSP页面的表单数据被转发到分派器Servlet,该分派器Servlet委托给控制器。我成功地注入了一个控制器,它工作得很好 对于多个控制器,我在servlet中创建了一个映射处理程序,通过读取属性文件将请求映射到相关控制器。我已经注入了映射处理程序,但仍然得到一个NullPointerException 另一个ControllerBundle只是将控制器和方法名称抽象为一个对象。我正在使用tomee webprofile 1.7.4 Dispat

我正在构建一个基于MVC架构的webapp。来自JSP页面的表单数据被转发到分派器Servlet,该分派器Servlet委托给控制器。我成功地注入了一个控制器,它工作得很好

对于多个控制器,我在servlet中创建了一个映射处理程序,通过读取属性文件将请求映射到相关控制器。我已经注入了映射处理程序,但仍然得到一个
NullPointerException

另一个ControllerBundle只是将控制器和方法名称抽象为一个对象。我正在使用tomee webprofile 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;
    }
}
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>


您在WEB-INF或WEB-INF/classes/META-INF中有beans.xml吗?否则,tomee 1.x上的CDI将不会激活。

是的,我刚刚添加了beans.xml。您添加的代码片段是EE 7 beans.xml,对tomee 1无效(对tomee 7有效)。试试看“
/customer/add=CustomerController#addCustomer
/customer/update=CustomerController#updateCustomer
/account/create=AccountController#createAccount
/account/freeze=AccountController#freezeAccount
<?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>