SpringMVC:带有动态bean对象的输入表单

SpringMVC:带有动态bean对象的输入表单,spring,spring-mvc,model-view-controller,controller,Spring,Spring Mvc,Model View Controller,Controller,我有一个等级:Employee->(经理、服务员、厨师)。 我正在尝试使用SpringMVC控制器构建一个web表单,以便能够在DB中创建并保存继承Employee的所有类型。 因此,在推送表单时,我必须将一些模型绑定到视图(JSP-page)。问题是,我的用户将通过{waterer,Cook,Manager}下拉列表来决定创建哪种类型。基于这些信息,我想创建实例并将它们保存在DB中。 我该怎么做? 标准方法意味着我绑定了这样一个表单: @RequestMapping("/new_emp

我有一个等级:Employee->(经理、服务员、厨师)。 我正在尝试使用SpringMVC控制器构建一个web表单,以便能够在DB中创建并保存继承Employee的所有类型。 因此,在推送表单时,我必须将一些模型绑定到视图(JSP-page)。问题是,我的用户将通过{waterer,Cook,Manager}下拉列表来决定创建哪种类型。基于这些信息,我想创建实例并将它们保存在DB中。 我该怎么做? 标准方法意味着我绑定了这样一个表单:

    @RequestMapping("/new_employee")
    public ModelAndView showEmployeeForm(){
            return new ModelAndView("new_employee","employee",new Employee());
    }
    String pos = webRequest.getParameter("position");
    Employee employee;
    switch (pos) {
            case "1":
                    employee = new Manager();
                    break;
            case "2":
                    employee = new Chef();
                    break;
            case "3":
                    employee = new Waiter();
                    break;
            default:
                    throw new RuntimeException();
    }
但在处理表单的方法中,我将被迫创建给定类型的另一个实例。Smth。像这样:

    @RequestMapping("/new_employee")
    public ModelAndView showEmployeeForm(){
            return new ModelAndView("new_employee","employee",new Employee());
    }
    String pos = webRequest.getParameter("position");
    Employee employee;
    switch (pos) {
            case "1":
                    employee = new Manager();
                    break;
            case "2":
                    employee = new Chef();
                    break;
            case "3":
                    employee = new Waiter();
                    break;
            default:
                    throw new RuntimeException();
    }
有没有更可爱、更受欢迎的方法

问题是,我的用户将决定使用 {服务员、厨师、经理}的下拉列表。根据这一信息,我 要创建实例并将其保存在DB中

我认为您的目标是在不修改现有类的情况下,将设计松散地耦合起来,以添加新类型的员工&创建和保存逻辑,您需要遵循以下步骤:

(1) 创建Employee接口或抽象类类型

(2) 创建经理、服务员、厨师类型(所有实施/扩展员工类型)

(3) 创建
EmployeeService
接口类型并实现
ManagerService
WaiterService
等。。通过编写
create
所涉及的特定逻辑(如下所述,如果您对每种类型都有复杂的create逻辑,则可以添加该逻辑以提高灵活性)

(4) 将
showEmployeeForm()
createEmployee()
添加到您的
Controller
中,并使用
ApplicationContext
动态获取相应的
EmployeeService
对象(它根据用户选择的输入检索对象)。请注意,我们有单独的服务类(bean),我们使用
ApplicationContext
来获得正确的bean(服务员服务、经理服务、厨师服务)来调用相应的
create()

代码如下所示:

public interface EmployeeService {
   public void create();
}
@Service("WaiterService")
public class WaiterService extends EmployeeService {
   public void create(Waiter waiter) {
     //add Waiter save logic
   }
}
@Service("ChefService")
public void create(Chef chef) {
      //add Chef save logic
   }
}
    @Controller
    public class EmployeeController {

        @Autowired
        private ApplicationContext appContext;

        @RequestMapping("/new_employee", method=RequestMethod.GET)
        public ModelAndView showEmployeeForm(){
          String position = webRequest.getParameter("position");

          Employee employee = (EmployeeService)appContext.getBean(position);
          //Put position to session
           session.setAttribute("position", position);
                return new ModelAndView("new_employee","employee", employee);
        }

        @RequestMapping("/create", method=RequestMethod.POST)
        public ModelAndView createEmployee() {

           //Get position from session and then getBean
           String position = session.getAttribute("position");
           EmployeeService empService = (EmployeeService)appContext.getBean(position);

           empService.create();
        }
    }
(1)创建员工界面或抽象类类型

@Component
public class Manager implements Employee {
 //define props
}
(2)创建经理、服务员、厨师类(所有工具/扩展员工类型)

(2)员工服务界面:

public interface EmployeeService {
   public void create();
}
@Service("WaiterService")
public class WaiterService extends EmployeeService {
   public void create(Waiter waiter) {
     //add Waiter save logic
   }
}
@Service("ChefService")
public void create(Chef chef) {
      //add Chef save logic
   }
}
    @Controller
    public class EmployeeController {

        @Autowired
        private ApplicationContext appContext;

        @RequestMapping("/new_employee", method=RequestMethod.GET)
        public ModelAndView showEmployeeForm(){
          String position = webRequest.getParameter("position");

          Employee employee = (EmployeeService)appContext.getBean(position);
          //Put position to session
           session.setAttribute("position", position);
                return new ModelAndView("new_employee","employee", employee);
        }

        @RequestMapping("/create", method=RequestMethod.POST)
        public ModelAndView createEmployee() {

           //Get position from session and then getBean
           String position = session.getAttribute("position");
           EmployeeService empService = (EmployeeService)appContext.getBean(position);

           empService.create();
        }
    }
**ManagerService类:**

@Service("ManagerService")
public class ManagerService extends EmployeeService {
   public void create(Manager manager) {
      //add Manager save logic
   }
}
服务员服务类:

public interface EmployeeService {
   public void create();
}
@Service("WaiterService")
public class WaiterService extends EmployeeService {
   public void create(Waiter waiter) {
     //add Waiter save logic
   }
}
@Service("ChefService")
public void create(Chef chef) {
      //add Chef save logic
   }
}
    @Controller
    public class EmployeeController {

        @Autowired
        private ApplicationContext appContext;

        @RequestMapping("/new_employee", method=RequestMethod.GET)
        public ModelAndView showEmployeeForm(){
          String position = webRequest.getParameter("position");

          Employee employee = (EmployeeService)appContext.getBean(position);
          //Put position to session
           session.setAttribute("position", position);
                return new ModelAndView("new_employee","employee", employee);
        }

        @RequestMapping("/create", method=RequestMethod.POST)
        public ModelAndView createEmployee() {

           //Get position from session and then getBean
           String position = session.getAttribute("position");
           EmployeeService empService = (EmployeeService)appContext.getBean(position);

           empService.create();
        }
    }
ChefService类:

public interface EmployeeService {
   public void create();
}
@Service("WaiterService")
public class WaiterService extends EmployeeService {
   public void create(Waiter waiter) {
     //add Waiter save logic
   }
}
@Service("ChefService")
public void create(Chef chef) {
      //add Chef save logic
   }
}
    @Controller
    public class EmployeeController {

        @Autowired
        private ApplicationContext appContext;

        @RequestMapping("/new_employee", method=RequestMethod.GET)
        public ModelAndView showEmployeeForm(){
          String position = webRequest.getParameter("position");

          Employee employee = (EmployeeService)appContext.getBean(position);
          //Put position to session
           session.setAttribute("position", position);
                return new ModelAndView("new_employee","employee", employee);
        }

        @RequestMapping("/create", method=RequestMethod.POST)
        public ModelAndView createEmployee() {

           //Get position from session and then getBean
           String position = session.getAttribute("position");
           EmployeeService empService = (EmployeeService)appContext.getBean(position);

           empService.create();
        }
    }
(3)控制器类方法:

public interface EmployeeService {
   public void create();
}
@Service("WaiterService")
public class WaiterService extends EmployeeService {
   public void create(Waiter waiter) {
     //add Waiter save logic
   }
}
@Service("ChefService")
public void create(Chef chef) {
      //add Chef save logic
   }
}
    @Controller
    public class EmployeeController {

        @Autowired
        private ApplicationContext appContext;

        @RequestMapping("/new_employee", method=RequestMethod.GET)
        public ModelAndView showEmployeeForm(){
          String position = webRequest.getParameter("position");

          Employee employee = (EmployeeService)appContext.getBean(position);
          //Put position to session
           session.setAttribute("position", position);
                return new ModelAndView("new_employee","employee", employee);
        }

        @RequestMapping("/create", method=RequestMethod.POST)
        public ModelAndView createEmployee() {

           //Get position from session and then getBean
           String position = session.getAttribute("position");
           EmployeeService empService = (EmployeeService)appContext.getBean(position);

           empService.create();
        }
    }
备注:我添加了具有不同EmployeeService类型的服务层 为了获得更多的灵活性,但这取决于您使用它(按原样)或 这并不取决于create()逻辑的复杂程度 不同的员工类型

更新: 如果用户希望添加新类型的员工,则不进行编程就无法创建新类型的员工?


上述设计遵循流行的OOP,即我们应该能够在不修改现有类的情况下添加新类(新员工类型或新功能)。因此,您可以在不修改现有类别的情况下添加任意数量的新员工类型。

谢谢!我想在这里实现多态性,而不是开关块。。但没想到会将其移动到服务层!但是你不觉得新员工()在这里有点多余吗。。。在showEmployeeForm()中?由于我将从WebRequest对象中检索所有请求参数以从头构建后代,所以我根本不需要员工模型,对吗?我已经修改了上述代码,您现在可以参考它,具体的服务层完全是可选的是的,但直觉上我认为员工应该是抽象的。我们无法实例化。。。这一事实打破了Spring的注入逻辑。还有一件事,如果用户想要添加新类型的Employee,不编程就不可能创建类型……是的,Employee应该是接口或抽象类型,Spring容器应该检测到它(标记为@Component)(为您更新上述答案)