JSF 链接查看页面

发布于 2024-10-15 03:34:19 字数 857 浏览 3 评论 0原文

我有一个带有数据表的页面。我希望表中的某些项目链接到相应的视图页面。

例如,现在我有一个没有链接的表:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十六岁半 2024-10-22 03:34:19

BalusC 的答案几乎很好,但行不通(编辑:现在可以了)。

您已经知道如何将值添加到参数中。顺便说一句,如果我是你,我不会使用 ,而是:

<h:link outcome='viewBean'>
    <f:param name='id' value='#{bean.id}' />
</h:link>

现在你必须在捕获值时做出选择。最简单的方法是在 id 属性上方添加注释:

@ManagedProperty("#{param.id}") // this will inject id from param into id
private Long id;


//  (getters and setters are obligatory)


@PostConstruct // this will execute init() after id is injected
public void init() {

}

最后一件事:拥有一个名为“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:

<h:link outcome='viewBean'>
    <f:param name='id' value='#{bean.id}' />
</h:link>

Now you have to choices when it comes to catching the value. The simplest would be to add annotation above your id property:

@ManagedProperty("#{param.id}") // this will inject id from param into id
private Long id;


//  (getters and setters are obligatory)


@PostConstruct // this will execute init() after id is injected
public void init() {

}

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).

高冷爸爸 2024-10-22 03:34:19

我假设是 JSF 2.x。将其添加到您的 Bean

@ManagedProperty(value="#{param.id}")
private Long id;

(每当视图加载时,这基本上都会执行 bean.setId(request.getParameter("id"))

'将在 Bean@PostConstruct 方法中可用。

@PostConstruct
public void init() {
    // Fill model based on id.
}

I'll assume JSF 2.x. Add this to your Bean

@ManagedProperty(value="#{param.id}")
private Long id;

(this does basically a bean.setId(request.getParameter("id")) whenever the view loads)

It'll be available in @PostConstruct method of Bean.

@PostConstruct
public void init() {
    // Fill model based on id.
}
失而复得 2024-10-22 03:34:19

这就是我所做的。

    <h:form>
        <h:commandLink action="#{bean.populateBean}" value="#{bean.id}">
            <f:setPropertyActionListener target="#{bean.id}" value="#{bean.id}" />
        </h:commandLink>
    </h:form>

在我的 Bean.java 类中,我添加了操作控制器:

public String populateBean(){
    Bean bean = BeanServiceImpl.getBean(id); //id was injected by the commandLink
    this.field1 = tenure.getField1();
    this.field2 = tenure.getField2();

    return("viewBean");
}

我的 ViewBean.xhtml 是相同的:

<ul>
  <li>ID: #{bean.id}</li>
  <li>Field 1: #{bean.field1}</li>
  <li>Field 2: #{bean.field2}</li>
</ul>

This is what I did.

    <h:form>
        <h:commandLink action="#{bean.populateBean}" value="#{bean.id}">
            <f:setPropertyActionListener target="#{bean.id}" value="#{bean.id}" />
        </h:commandLink>
    </h:form>

In my Bean.java class, I added the action controller:

public String populateBean(){
    Bean bean = BeanServiceImpl.getBean(id); //id was injected by the commandLink
    this.field1 = tenure.getField1();
    this.field2 = tenure.getField2();

    return("viewBean");
}

My ViewBean.xhtml is the same:

<ul>
  <li>ID: #{bean.id}</li>
  <li>Field 1: #{bean.field1}</li>
  <li>Field 2: #{bean.field2}</li>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文