JSF 2:h:link和getrowdata [英] JSF 2: h:link and getrowdata
问题描述
如果我在 books.xhtml 中有一个h:dataTable
,它提供了记录列表,并且我想查看特定的记录,然后添加或编辑该记录,目前,我拥有以下记录:
If I have a h:dataTable
in books.xhtml providing a list of records and I want to view a particular record, then add or edit that record, currently I have this:
<h:dataTable #{BookBean.books}" var="books">
<h:link outcome="bookview" value="#{books[1]}">
<f:param name="id" value="#{books[2]}" />
</h:link>
</h:dataTable>
我发现我需要包括<f:param>
才能显示单击链接的CSS状态.否则,如果我没有<f:param>
,则每次我单击上面代码中由h:link标记呈现的链接时,所有链接都将更改为CSS单击状态.
I found out that I need to include <f:param>
in order to show the CSS status of clicked link; otherwise if I don't have <f:param>
, every time I clicked on a link rendered by h:link tags in the codes above, all links are changed to CSS clicked status.
此外,我在getrowdata()
上读过一些地方,但无法使其正常工作.这是使用<f:param>
更好的选择吗?
Also, I read somewhere about the getrowdata()
but I haven't been able to get it work. Is this a better alternative to using <f:param>
?
我已经在我的BookBean类中尝试了getrowdata()
方法,如下所示:
I have tried the getrowdata()
method in my BookBean class as followed:
private DataModel<BookModel> books;
private BookModel currentBook;
public String view()
{
currentBook = books.getRowData();
return "bookview";
}
在bookview.xhtml中,我有这个:
and in bookview.xhtml I have this:
<h:dataTable value="#{BookBean.view}" var="item">
... // render content here
<h:dataTable>
但是我收到关于找不到该属性的错误.很抱歉提出这个问题,但我仍然不了解JSF 2的一些强大功能.可以理解h:link
和getrowdata
用法的一些专家可以用通俗易懂的方式或者也许用一些基本代码向我解释.例子.谢谢.
but I get an error about the property not found. Sorry for asking this question but I still don't understand yet some of the powerful features of JSF 2. Can some expert who understand the usage of h:link
and getrowdata
please explain to me in layman terms or perhaps with some basic code example. Thank you.
更新: 根据下面的@BalusC建议更改了类. BookModel 类为:
UPDATE: Changed classes based on @BalusC suggestions below. BookModel class is:
@Entity
public class BookModel implements Serializable
{
private Long id;
private String title;
private String author;
// getters and setters here
}
BookService 类如下:
@Stateless
public class BookService
{
@PersistenceContext(unitName = "persistentUnit")
protected EntityManager entityManager;
public BookModel create() {
return new BookModel();
}
public void delete(BookModel bookModel) {
bookModel = entityManager.merge(bookModel);
entityManager.remove(bookModel);
}
public BookModel update(BookModel bookModel) {
return entityManager.merge(bookModel);
}
public BookModel find(Long id) {
return entityManager.find(BookModel.class, id);
}
}
BookBean 类为:
@ManagedBean(name = "bookBean")
@RequestScoped
public class BookBean implements Serializable
{
@EJB
private BookService bookService;
@ManagedProperty(value = "#{param.id}")
private Long id;
private DataModel<BookModel> books;
private BookModel currentBook;
@PostConstruct
public void init() {
currentBook = bookService.find(id);
}
public BookModel getCurrentBook() {
return currentBook;
}
public void setCurrentBook(BookModel currentBook) {
this.currentBook = currentBook;
}
}
运行上面的BookBean类导致此错误:java.lang.IllegalStateException: WEB9031: WebappClassLoader unable to load resource [org.apache.openjpa.util.LongId], because it has not yet been started, or was already stopped
.这是我目前所处的位置.
Running the BookBean class above caused this error: java.lang.IllegalStateException: WEB9031: WebappClassLoader unable to load resource [org.apache.openjpa.util.LongId], because it has not yet been started, or was already stopped
. This is where I'm stuck at the moment.
仅供参考:我的开发环境是Glassfish 3.1,Apache OpenJPA 2.1和JSF 2.1.0(与Glassfish捆绑在一起)
FYI: My dev environment is Glassfish 3.1, Apache OpenJPA 2.1 and JSF 2.1.0 (that comes bundled with Glassfish)
推荐答案
代码中有两个缺陷:
-
h:link
触发GET请求,而不是POST请求.DataModel#getRowData()
在这里也没有用,因为您不能将bean操作附加到触发GET请求的组件上.
The
h:link
fires a GET request, not a POST request. TheDataModel#getRowData()
is not useful here either since you cannot attach bean actions to components which fire a GET request.
带有public String view()
的<h:dataTable value="#{BookBean.view}">
没有意义.数据表的值必须是项目的集合,而不是bean操作方法.
The <h:dataTable value="#{BookBean.view}">
with public String view()
makes no sense. The datatable's value has got to be a collection of items, not a bean action method.
我了解您希望在表格中的每个书本上都具有一个GET链接,该链接指向有关该书本的一些详细信息页面.修改详细信息页面,如下所示:
I understand that you want a GET link on every book item in the table which points to some detail page about the book item. Fix the detail page as follows:
bookview.xhtml
<h:outputText value="#{bookBean.currentBook.id}" />
<h:outputText value="#{bookBean.currentBook.author}" />
<h:outputText value="#{bookBean.currentBook.title}" />
...
和BookBean
如下:
@ManagedBean
@RequestScoped
public BookBean {
@ManagedProperty(value="#{param.id}")
private Long id;
private BookModel currentBook;
@PostConstruct
public void init() {
currentBook = bookDAO.find(id);
}
// ...
}
@ManagedProperty
将设置GET请求参数. @PostConstruct
将根据该参数预加载正确的书.
The @ManagedProperty
will set the GET request parameter. The @PostConstruct
will preload the right book based on the parameter.
请注意,这与POST-Redirect-GET模式无关.
Please note that this has nothing to do with POST-Redirect-GET pattern.
这篇关于JSF 2:h:link和getrowdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!