我们最近了解了 DTO(数据传输对象)以及为什么我们应该使用它们而不是直接来自数据库的对象。
我们的类名为Product
,其中包含:id、code、price、name、description
。
在 View 中,我们仅显示代码、价格、名称和说明
,因此id
未使用。
我们的培训师告诉我们,如果 id
会丢失,我们应该使用 DTO。否则,她告诉我们她可以在 View 中公开 id
。
这样的事情怎么可能呢?我们使用 .jsp
文件,这些文件在服务器端编译,转换为 .html
并发送给用户。你怎么可能公开一个不使用的字段?
最佳答案
Data Transfer Object that communicate with the database, In the database you probably store only the properties like in your example, id,code,price,name,description.
DTO 是一个简单的 POJO 类,具有 getter/setter 方法
您可以从 DTO 的 getter/setter 访问数据库,并且该属性很可能是私有(private)的。
The id is uniquly identified the products in your table and no need to show or expose from your presentation .jsp.
关于java - 公开 JSP 文件中未使用的属性 (Spring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45198502/