JSF 链接查看页面
我有一个带有数据表的页面。我希望表中的某些项目链接到相应的视图页面。
例如,现在我有一个没有链接的表:
<h:dataTable var="bean" value="#{beanServiceImpl.beans}" border="1">
<h:column>#{bean.id}</h:column>
</h:dataTable>
我想向某些条目添加超链接,并让它们转到视图页面,根据其 id 显示更多信息:
<h:dataTable var="bean" value="#{beanServiceImpl.beans}" border="1">
<h:column>
<a href="viewBean.xhtml?id=#{bean.id}">#{bean.id}</a>
</h:column>
</h:dataTable>
ViewBean.xhtml 将包含如下内容:
ViewBean.xhtml
<ul>
<li>ID: #{bean.id}</li>
<li>Field 1: #{bean.field1}</li>
<li>Field 2: #{bean.field2}</li>
</ul>
我如何在 JSF 中完成类似的事情?我知道我必须编写一个控制器来查询其他字段的 id。但是如何让 viewBean.xhtml 运行业务逻辑来获取其他字段并呈现它?
I have a page with a data table. I want some of the items in the tables to be linked to a corresponding view page.
For example, right now I have a table with no links:
<h:dataTable var="bean" value="#{beanServiceImpl.beans}" border="1">
<h:column>#{bean.id}</h:column>
</h:dataTable>
I want to add hyperlinks to some entries and have them go to a view page showing them more info based on their id:
<h:dataTable var="bean" value="#{beanServiceImpl.beans}" border="1">
<h:column>
<a href="viewBean.xhtml?id=#{bean.id}">#{bean.id}</a>
</h:column>
</h:dataTable>
ViewBean.xhtml will contain something like this:
ViewBean.xhtml
<ul>
<li>ID: #{bean.id}</li>
<li>Field 1: #{bean.field1}</li>
<li>Field 2: #{bean.field2}</li>
</ul>
How do I accomplish something like this in JSF? I know that I'll have to write a controller to query the id for the other fields. But how do I make viewBean.xhtml run the business logic to get the other fields and render it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BalusC 的答案几乎很好,但行不通(编辑:现在可以了)。
您已经知道如何将值添加到参数中。顺便说一句,如果我是你,我不会使用
,而是:
现在你必须在捕获值时做出选择。最简单的方法是在 id 属性上方添加注释:
最后一件事:拥有一个名为“bean”的变量并不比称其为“变量”(或拥有一只名为 Dog 的狗和名为 Cat 的猫)更有意义。它不携带任何信息,更糟糕的是,它使应用程序中的所有 Bean 无法区分(除非您构建豆类管理器)。
The BalusC's answer is almost good, but will not work (edit: it works now).
You already know, how to add the value to the params. BTW, if I were you, I would not use
<a href>
, but instead:Now you have to choices when it comes to catching the value. The simplest would be to add annotation above your id property:
And the last thing: having a variable named "bean" has no more sense than calling it "variable" (or having a dog named Dog and cat named Cat). It carries no information and worse, it makes all the beans in your application indistinguishable (unless you build a legumes manager).
我假设是 JSF 2.x。将其添加到您的
Bean
(每当视图加载时,这基本上都会执行
bean.setId(request.getParameter("id"))
)'将在
Bean
的@PostConstruct
方法中可用。I'll assume JSF 2.x. Add this to your
Bean
(this does basically a
bean.setId(request.getParameter("id"))
whenever the view loads)It'll be available in
@PostConstruct
method ofBean
.这就是我所做的。
在我的 Bean.java 类中,我添加了操作控制器:
我的 ViewBean.xhtml 是相同的:
This is what I did.
In my Bean.java class, I added the action controller:
My ViewBean.xhtml is the same: