将EJB委派给Controller类,而不是直接将它放在Servlet中

时间:2014-03-03 08:58:37

标签: java java-ee servlets ejb ejb-3.0

我试图在我的Servlet中没有注入EJB,目前我将它直接注入到我的Servlet中并且它工作正常但我想将控制权传递给将注入EJB的控制器类,而不是控制器方法或构造函数,它接受Bean的引用变量并使用它。我正在做的一切都是在JSP中,我尝试在我的Controller上使用@ManagedBean注释,但没有任何作用。我只使用JSP而不是JSF,因为我不熟悉它们。我想要做什么是可以实现的,除了传递bean的参考之外还有其他替代品吗?任何光线都非常受欢迎。谢谢。目前我的Servlet中有以下内容。

 package com.ejb.web;

 import java.io.IOException;

 import javax.ejb.EJB;
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import com.ejb.customer.domain.Customer;
 import com.ejb.customer.service.CustomerLocal;
 import com.ejb.customer.util.DateUtil;

 /**
   * Servlet implementation class Servlet
 */
   @WebServlet("/customer")
   public class Dispatcher extends HttpServlet {
 private static final long serialVersionUID = 1L;

 @EJB
 CustomerLocal customerLocal;

   /**
     * @see HttpServlet#HttpServlet()
     */
      public Dispatcher() {
        super();
     }

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {


    String name = request.getParameter("name");
    String surname = request.getParameter("surname");
    String dateOfBirth = request.getParameter("dateOfBirth");

    Customer customer = new Customer();
    customer.setName(name);
    customer.setSurname(surname);
    customer.setDateOfBirth(DateUtil.convertToDate(dateOfBirth));

    System.out.println("Starting .....Adding");
    System.out.println(customerLocal);
        customerLocal.addCustomer(customer);
    System.out.println("Done .....Adding");
    request.setAttribute("customers",customerLocal.getCustomers());

    RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");

    rd.forward(request, response);

 }


}

我想要实现的是这些方面。

 package com.ejb.web;

 import java.io.IOException;

 import javax.ejb.EJB;
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import com.ejb.customer.domain.Customer;
 import com.ejb.customer.service.CustomerLocal;
 import com.ejb.customer.util.DateUtil;

 /**
   * Servlet implementation class Servlet
 */
   @WebServlet("/customer")
   public class Dispatcher extends HttpServlet {
 private static final long serialVersionUID = 1L;

 @Inject
 Controller controller;

   /**
     * @see HttpServlet#HttpServlet()
     */
      public Dispatcher() {
        super();
     }

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {


    String name = request.getParameter("name");
    String surname = request.getParameter("surname");
    String dateOfBirth = request.getParameter("dateOfBirth");

    Customer customer = new Customer();
    customer.setName(name);
    customer.setSurname(surname);
    customer.setDateOfBirth(DateUtil.convertToDate(dateOfBirth));

    System.out.println("Starting .....Adding");
    System.out.println(customerLocal);
        ccontroller.addCustomer(customer);
    System.out.println("Done .....Adding");
    request.setAttribute("customers",controller.getCustomers());

    RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");

    rd.forward(request, response);

 }


}

控制器类执行类似以下操作的内容。

    package com.ejb.customer.util;

    import java.util.List;
    import javax.annotation.ManagedBean;
    import javax.ejb.EJB;
    import javax.enterprise.context.SessionScoped;
    import com.ejb.customer.domain.Customer;
    import com.ejb.customer.service.CustomerLocal;

     @ManagedBean
      @SessionScoped
    public class Controller {

 @EJB
 CustomerLocal customerLocal;

public void addCustomer(Customer customer) {
customerLocal.addCustomer(customer);
}

public List getCustomers() {
    return customerLocal.getCustomers();
}

    }

对于糟糕的缩进感到抱歉

1 个答案:

答案 0 :(得分:0)

假设此代码编译,那么您将引用不同的控制器类。你从com.ejb.web包注入一个Controller,但你在这里的控制器来自com.ejb.customer.util包。您也可以删除ManagedBean引用,因为您正在使用CDI范围。

如果要在JSF页面中使用此控制器,请使用@Named而不是@ManagedBean。