简体   繁体   English

来自对象还是数据库的Jsp / Servlet列表?

[英]Jsp/Servlet list from object or from database?

I have a PHP background and now I am a JSP/Servlet beginner. 我有PHP背景,现在是JSP / Servlet初学者。

I have a doubt about the approach I need to use, for example, let's say I have a dynamic list of items, i can implement in two ways with JSP 我对我需要使用的方法有疑问,例如,说我有一个动态的项目列表,我可以使用JSP以两种方式实现

  1. Reading the list of items from a database 从数据库中读取项目列表
  2. Reading the list of items from a Collection inside an Object 从对象内部的集合中读取项目列表

Is the first the right approach? 第一种是正确的方法吗? Or is there any case where you need to use the second one? 还是在任何情况下都需要使用第二个?

First of all, I certainly wouldn't recommend reading the list from a database directly into a JSP. 首先,我当然不建议将列表从数据库直接读取到JSP中。 Best practice would involve a layer of your application (eg a DAO) that would read from the database into a collection that would be passed to another layer that would render the view. 最佳实践将涉及应用程序的某个层(例如DAO),该层将从数据库中读取到一个集合中,该集合将传递到另一个将呈现视图的层中。

If the set of values is significantly long, and/or may change often, than of course it should be in the database (examples: a list of countries available when filling out an address, a list of products, a list of agents to whom a ticket may be assigned). 如果值的集合很长,并且/或者可能经常更改,那么它当然应该在数据库中(例如:填写地址时可用的国家/地区列表,产品列表,代理人列表)可以分配票证)。

However, for a shorter list, that is primarily used to provide possible values (where the values may end up in the database, but the list of values itself need not be), an Enum may be a better choice (examples: a list of possible statuses for a ticket (open, pending, solved, closed), a list of types for a phone number (home, work, mobile)). 但是,对于较短的列表(主要用于提供可能的值)(这些值可能最终存储在数据库中,但值列表本身不一定是),Enum可能是更好的选择(例如:票证的可能状态(开放,待处理,已解决,已关闭),电话号码的类型列表(家庭,工作,移动)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM