如何从outputLink调用托管bean方法? [英] How to call a managed bean method from an outputLink?
问题描述
这可能在某处已经解决,但我在为搜索引擎提出问题时遇到了麻烦,到目前为止,尚无商品线索.
This may have been covered somewhere but I'm having trouble forming the question for search engine and no goods leads thus far.
我正在处理一个充当实体视图的页面.许多结果来自数据库,一次只显示少数几个结果.因此,您可以想象我想要建立一个链接列表,这些链接将用户带到另一个实体页面.这就是我的全部代码-没有PrimeFaces或任何其他前端漂亮的分页解决方案.至少现在是这样.
I'm working on a page that acts as entity view. Lots of results come from database and only a handful are displayed at a time. So you can imagine that I want to build a list of links that take user to another page of entities. This is all my code - no PrimeFaces or any other front-end nifty pagination solutions. At least for now.
输入代码:
@Named
@SessionScoped
public class ArticleIndexBean {
List<Article> articleList=new ArrayList<>();
List<Article> articleSubList=new ArrayList<>();
@PostConstruct
public void loadScreenSupport() {
search();
toEntityPage(1);
}
protected void search() {
// this method sets articleList which is the full list fetched from the database
}
public void toEntityPage(int pageNumber) {
// this method sets articleSubList which is a subset of articleList
}
每个页面链接都需要调用toEntiyPage(n)
.我知道commandLink,但是我想避免POST请求.另外,该bean当前处于会话作用域内,稍后我将尝试使其成为会话作用域.它肯定不会在请求范围内,因为我不想每次用户想要跳到另一个页面时都进行完整的数据库搜索.因此,@ PostConstruct也无济于事.
Each page link needs to call toEntiyPage(n)
. I am aware of commandLink but I want to avoid a POST request. Also, the bean is currently session scoped and I will try to make it conversation scoped later. It will certainly NOT be request scoped, as I don't want to do a full db search each time a user wants to jump to another page. So @PostConstruct won't help, either.
因此,使用这样的菜单:1 * 2 * 3 * 4 * 5
如何编写outputLink或通过GET请求调用我的ArticleIndexBean.toEntityPage(int)
的任何其他类型的链接?
So with a menu like this: 1 * 2 * 3 * 4 * 5
how do I code an outputLink or any other type of link that will call my ArticleIndexBean.toEntityPage(int)
via a GET request?
解决方案
基于Laurent的输入,我在bean中添加了currentEntityPageNumber
属性和toCurrentEntityPage()
方法. toCurrentEntityPage()
只需调用toEntityPage(getCurrentEntityPageNumber())
.
Based on input from Laurent, I added a currentEntityPageNumber
property and a toCurrentEntityPage()
method to my bean. The toCurrentEntityPage()
simply calls toEntityPage(getCurrentEntityPageNumber())
.
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<f:metadata>
<f:viewParam name="pn" value="#{articleIndexBean.currentEntityPageNumber}" />
<f:event type="preRenderView" listener="#{articleIndexBean.toCurrentEntityPage()}" />
</f:metadata>
<c:forEach var="pageNumber" begin="1" end="${articleIndexBean.getEntityPageCount()}">
<h:outputLink value="ar_index.xhtml">
<h:outputText value="${pageNumber}" />
<f:param name="pn" value="${pageNumber}" />
</h:outputLink>
</c:forEach>
如果我们可以直接调用toEntityPage(pageNumber)
肯定会更好,但这可以正常工作.
It would certainly be better if we could call toEntityPage(pageNumber)
directly but this works fine.
推荐答案
假设您使用的是JSF 2.2,则可以使用viewParam
检索GET参数中的页面,并使用viewAction
调用呈现前的方法. (默认情况下实际上是在INVOKE_APPLICATION
阶段调用的.)
Assuming you are using JSF 2.2, you could use the viewParam
to retrieve the page in the GET parameters and viewAction
to call a method before the rendering (actually called in the INVOKE_APPLICATION
phase by default).
手镯:
<f:metadata>
<f:viewParam name="page" value="#{articleIndexBean.entityPage}" />
<f:viewAction action="#{articleIndexBean.loadScreenSupport}" />
</f:metadata>
如果您使用的是JSF 2.0或JSF 2.1,则必须将viewAction
替换为:
If you are using JSF 2.0 or JSF 2.1, then you have to replace viewAction
by:
<f:event type="preRenderView" listener="#{articleIndexBean.loadScreenSupport}" />
Java:
@Named
@SessionScoped
public class ArticleIndexBean {
List<Article> articleList=new ArrayList<>();
List<Article> articleSubList=new ArrayList<>();
int pageNumber = 1; // by default first page
public void loadScreenSupport() {
search();
toEntityPage(pageNumber);
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
protected void search() {
// this method sets articleList which is the full list fetched from the database
}
public void toEntityPage(int pageNumber) {
// this method sets articleSubList which is a subset of articleList
}
}
到页面的链接很简单:
<h:outputLink value="resultPage.xhtml">
<h:outputText value="2" />
<f:param name="page" value="2" />
</h:outputLink>
参考:
这篇关于如何从outputLink调用托管bean方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!