为实体创建主从页面、如何链接它们以及选择哪个 bean 范围
我已经开始学习 JSF,但遗憾的是大多数教程仅提供登录或注册部分。
你能给我指出一些更深入的例子吗?我感兴趣的一件事是显示产品列表的页面。我在主页页面上,然后按产品页面,以便可以看到添加的最新产品。每次我访问该页面时,都会根据数据库中的最新条目创建产品列表。我该如何处理这个问题?
解决这个问题的一种方法是创建一个会话范围的托管 bean,在其中我将放置通过其他托管 bean 更新的不同实体。我在一些教程中发现了这种方法,但看起来相当困难和笨拙。
解决此类问题的最佳方法是什么?在两页主从用户界面中,会话范围的正确用法是什么?
I have started learning JSF, but sadly most tutorials out there present only a log in or a register section.
Can you point me to some more in depth examples? One thing I'm interested in is a page presenting a list of products. I'm on page home and I press on page products so that I can see the latest products added. And every time I visit the page, the product list will be created from the latest entries in the database. How can I handle this?
One way to solve this would be to create a session scoped managed bean in which I would place different entities updated through other managed beans. I found this kind of approach in some tutorials, but it seems quite difficult and clumsy.
Which would be the best approach to solve a thing like this? What is the correct usage of session scope in two-page master-detail user interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅将其用于会话范围的数据,而不用于其他用途。例如,登录的用户、其设置、选择的语言等。
另请参阅:
通常您使用请求或查看范围。列表的加载应该在
@PostConstruct
方法中进行。如果页面不包含任何
,则请求范围没问题。无论如何,当没有
时,视图作用域 bean 的行为就像请求作用域一样。所有仅检索信息(即幂等)的“查看产品”和“编辑产品”链接/按钮都应该是简单的 GET
/
,其中您通过
将实体标识符作为请求参数传递。所有将操作信息(即非幂等)的“删除产品”和“保存产品”链接/按钮应通过
/
(您不希望它们可添加书签/可搜索机器人索引!)。这又需要
。为了保留验证和 ajax 请求的数据(这样您就不需要在每个请求上重新加载/预初始化实体),bean 最好是视图范围的。请注意,您基本上应该为每个视图拥有一个单独的 bean,并且还要注意,这些 bean 不一定需要相互引用。
因此,给定这个“产品”实体:
以及这个“产品服务”EJB:
您可以在
/products.xhtml
上“查看产品”:您可以在
/products.xhtml
上“编辑产品” >/products/edit.xhtml:这个转换器用于“编辑产品”的
:您甚至可以使用通用转换器,这在 使用 Java 泛型实现实体转换器。
另请参阅:
Use it for session scoped data only, nothing else. For example, the logged-in user, its settings, the chosen language, etcetera.
See also:
Typically you use the request or view scope for it. Loading of the list should happen in a
@PostConstruct
method. If the page doesn't contain any<h:form>
, then the request scope is fine. A view scoped bean would behave like a request scoped when there's no<h:form>
anyway.All "view product" and "edit product" links/buttons which just retrieve information (i.e. idempotent) whould be just plain GET
<h:link>
/<h:button>
wherein you pass the entity identifier as a request parameter by<f:param>
.All "delete product" and "save product" links/buttons which will manipulate information (i.e. non-idempotent) should perform POST by
<h:commandLink>
/<h:commandButton>
(you don't want them to be bookmarkable/searchbot-indexable!). This in turn requires a<h:form>
. In order to preserve the data for validations and ajax requests (so that you don't need to reload/preinitialize the entity on every request), the bean should preferably be view scoped.Note that you should basically have a separate bean for each view and also note that those beans doesn't necessarily need to reference each other.
So, given this "product" entity:
And this "product service" EJB:
You can have this "view products" on
/products.xhtml
:And you can have this "edit product" on
/products/edit.xhtml
:And this converter for
<f:viewParam>
of "edit product":You can even use a generic converter, this is explained in Implement converters for entities with Java Generics.
See also:
作为对 BalusC 建议的一个小改进,有时您可以从“的
中删除required
/requiredMessage
部分”详细信息”屏幕,而是使用编辑表单的条件渲染(如 BalusC 所做的)和反向条件,为“列表/主”屏幕推荐特定链接,甚至使用 viewAction 来测试参数并强制重定向到那个清单。As a small improvement to what BalusC recommended, sometimes you can remove the
required
/requiredMessage
part from the<f:viewParam>
of your "details" screen and instead use the conditional rendering of the editing form (as BalusC did) with a reverse condition for recommending a specific link for the "list/master" screen or, even use a viewAction that would test the param and force a redirect to that list.