java - 在 Spring MVC 3.2.x 中实现级联下拉菜单的正确方法

标签 java spring jsp spring-mvc

我正在尝试找出使用 Spring MVC 3.2.x 实现下拉菜单的正确/最佳方法。我的 JSP 页面有 4 个下拉菜单、2 个级联菜单和 1 个静态菜单。这是有效的典型实现:

@Controller
@RequestMapping("/employeeSearch")
public class EmployeeSearchController {
.......
@ModelAttribute("employeeOrgList")
public List<EmployeeOrgList> populateOrgList(HttpServletRequest request) {
   return employeeService.getEmployeeOrgList();
}

@ModelAttribute("employeeSubOrg1List")
public List<EmployeeSubOrg1List> populateSubOrg1List(HttpServletRequest request) {
   return employeeService.getEmployeeSubOrg1List();
}

@ModelAttribute("employeeSubOrg2List")
public List<EmployeeSubOrg2List> populateSubOrg2List(HttpServletRequest request) {
   return employeeService.getEmployeeSubOrg2List();
}

@ModelAttribute("employeeStatusList")
public List<EmployeeStatusList> populateStatusList(HttpServletRequest request) {
   return employeeService.getEmployeeStatusList();
}

...............

问题: 在上述实现中,用户首先选择员工组织列表,页面刷新以填充员工子组织 1 列表,当员工选择子组织 1 时,将填充子组织 2,并且员工状态下拉列表保持填充状态。这段代码按原样工作得很好。但问题是我在 EmployeeSearchController 中有 7 个 RequestMappings,包括那些处理下拉选择的请求映射。因此,对于每次调用,我都会访问数据库 7 x 4 = 28 次,我认为这是 Not Acceptable 。

为了解决这个问题,我删除了所有 4 个方法上的 @ModelAttribute,并在第一次访问页面时手动将它们添加到模型中(RequestMapping 设置为 GET)。 JSP 加载正常,但一旦我选择员工组织列表,员工子组织 1 列表就会被填充,但员工组织列表和员工状态列表为空。具体方法如下:

//This is to initialize
@RequestMapping(method = { RequestMethod.GET })
public ModelAndView handlePageEntry(HttpServletRequest request, ModelMap modelMap) {
    EmployeeSearchForm form = new EmployeeSearchForm();
    ModelAndView modelView = new ModelAndView("employeeSearchTile");
    modelView.addAttribute("form", form);
    modelView.addAttribute("employeeOrgList", getEmployeeOrgList());
    modelView.addAttribute("employeeSubOrg1ist", new ArrayList<EmployeeSubOrg1ist>());
    modelView.addAttribute("employeeSubOrg2ist", new ArrayList<EmployeeSubOrg1ist>());
    modelView.addAttribute("employeeStatusList", getEmployeeStatusList());
    return modelView;
}

//This one is called when we select employee org list
@RequestMapping(method = { RequestMethod.POST }, params = { "submitAction=employeeOrgSelected" })
public ModelAndView populateEmployeeSubOrg1(@ModelAttribute(value = "form") EmployeeSearchForm form, ModelMap modelMap) {
   ModelAndView modelView = new ModelAndView("employeeSearchTile");
    modelView.addAttribute("form", form);
    modelView.addAttribute("employeeSubOrg1ist", getEmployeeSubOrg1ist(form.getSelectedEmployeeOrg));
    return modelView;
}

显然,我既没有将员工组织和员工状态列表添加回模型,也没有在 session 中设置它们(我不想这样做)。如果我想在模型上设置它们,那么我必须再次访问数据库来获取这两个列表。这比击中 28 次要好,但我仍然希望专家能够权衡一下。

那么,在不多次访问数据库的情况下完成此任务的最佳方法是什么?

最佳答案

为了减少数据库访问,您可以:

a) 如果信息是相关的,并且您可以使用联接来获取所需信息,请尝试通过更少的查询返回更多信息。

b) 您可以缓存服务调用结果。我通常使用 ehcache 和 Spring 注释来实现这一点:那么你只是缓存在一个地方,而不是每个人的 session

@Cacheable(cacheName = "employeeDataCache")
public List<EmployeeOrgList> getEmployeeSubOrg1List() {
   // do whatever
}

https://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable

关于java - 在 Spring MVC 3.2.x 中实现级联下拉菜单的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24959546/

相关文章:

html - 浏览器中的链接 href 无效?

java - JSP、Java、Spring、迭代 HashMap 并从其值调用基本 getter 方法?

java - 禁用选项卡滑动

java - 来自 JAR 的线程 "main"java.lang.NoClassDefFoundError 中出现异常

java - Spring:向 websocket 客户端发送消息

java - 输入标签无法正常使用 JQuery Ajax

Java错误: java -v command throws errors

java - 无法在 Ubuntu 中使用 Java 压缩空文件夹

java - 即使使用 @Transactional,Hibernate 也无法延迟初始化实体服务中的角色集合和 session

jquery - 客户端发送的请求在语法上不正确