使用get参数在JSF上分页 [英] Pagination on JSF using get parameters

查看:142
本文介绍了使用get参数在JSF上分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在index.xhtml上,并且有以下代码段:

I am on index.xhtml and I have this code snippet:

<h:form>
    <h:commandButton action="#{blogentryListerBean.olderBlogEntries}"
                     value="Older Blog Entries"/>
</h:form>

BlogEntryListerBean为 RequestScoped .这是我在 olderBlogEntries

BlogEntryListerBean is RequestScoped. This is the code I have in olderBlogEntries

public String olderBlogEntries() {

    HttpServletRequest request
            = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

    String id = request.getParameter("id");

    if (id == null || id.equals("")) {
        id = "0";
    }

    int pageIamOn = Integer.valueOf(id);

    String stringToBeReturned = "index.xhtml?id=" + (pageIamOn + 1) + "&faces-redirect=true";
    return stringToBeReturned;
}

因此,当我第一次访问index.xhtml时,看到的URL是:index.xhtml如预期的那样. 第一次单击旧博客条目"按钮时,我看到URL:index.xhtml?id = 1 现在,当我点击旧博客条目"按钮时,我再次进入index.xhtml?id = 1

So when I hit index.xhtml for the first time the URL I see is: index.xhtml as expected. The first time I click button "Older Blog Entries" I see URL: index.xhtml?id=1 Now when I hit Older Blog Entries button, I am again on index.xhtml?id=1

这是怎么了?为什么我不索引index.xhtml?id = 2

What is wrong here? Why am I not going to index.xhtml?id=2

?

谢谢.

推荐答案

您实际上是在误解当前的Http生命周期.您的问题基本上是目标页面(对于您的情况 index.xhtml 本身)没有在任何地方捕获发送的参数.

You're actually misunderstanding the current Http lifecycle. Your problem basically is destination page (for your case index.xhtml itself) is not catching the sent param anywhere.

请记住,您最初是在执行重定向,但是当您下次按下按钮时,当前是另一个不知道您的参数的不同请求,因此,在处理操作时,您的request.getParameter("id")返回null一次又一次,因为没有发送参数.

Remember you're performing a redirection at first, but when you press the button next time, it's currently another different request which knows nothing about your param, so, when the action is processed, your request.getParameter("id") returns null once and again as the param is not being sent.

您的问题有一个简单的解决方案,该解决方案基于使用f:viewParam将接收到的参数绑定到您的视图:

Your problem has an easy solution, which is based in binding the received param to your view using f:viewParam:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <f:metadata>
        <f:viewParam name="id" />
    </f:metadata>
</h:head>
<h:body>
    <h:form>
        <h:commandButton action="#{bean.olderBlogEntries(id)}"
            value="Older Blog Entries" />
    </h:form>
</h:body>
</html>

您的Java代码如下:

And your java code would be like this:

@ManagedBean
@RequestScoped
public class Bean {

    public String olderBlogEntries(Integer id) {

        if (id == null) {
            id = 0;
        }

        String stringToBeReturned = "index.xhtml?id=" + (id + 1)
                + "&faces-redirect=true";
        return stringToBeReturned;
    }
}

在这种情况下,您需要将参数传递给操作方法,以实现在类路径中需要EL 2.2的目的.如果尚未设置,请遵循

In this case you need to pass a parameter to an action method, to do like that you need to have EL 2.2 available in your classpath. If you don't have it already set up, follow that steps.

生命周期可能是:

  • 第一个请求,您打开浏览器并针对index.xhtml执行GET请求.没有发送任何参数.
  • 您按一次按钮,将表单作为POST请求提交.该操作方法重定向到http://localhost:8080/basic-jsf/index.xhtml?id=1,这将导致客户端对该URL执行GET请求.现在,JSF捕获了id视图参数,并将其绑定到名称为id的视图.
  • 再次按下按钮时,JSF会使用当前参数(值1)调用action方法.重定向现在转到http://localhost:8080/basic-jsf/index.xhtml?id=2.
  • First request, you open the browser and perform a GET request against index.xhtml. No param is sent.
  • You press the button once, submitting the form as a POST request. The action method redirects to http://localhost:8080/basic-jsf/index.xhtml?id=1, which causes a GET request to be done by the client to this url. Now JSF catches the id view param and binds it to the view with the name id.
  • When you press the button again, JSF calls the action method with the current param, valued 1. The redirection now goes to http://localhost:8080/basic-jsf/index.xhtml?id=2.

或者,您可以将视图参数绑定到bean属性.在这种情况下,将不需要将其作为方法参数传递:

Alternatively, you could bind the view param to a bean property. In that case, there would be no need to pass it as method parameter:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <f:metadata>
        <f:viewParam name="id" value="#{bean.id}" />
    </f:metadata>
</h:head>
<h:body>
    <h:form>
        <h:commandButton action="#{bean.olderBlogEntries}"
            value="Older Blog Entries" />
    </h:form>
</h:body>
</html>

实现媒体资源的get/set方法:

Implementing your property's get/set methods:

@ManagedBean
@RequestScoped
public class Bean {

    private Integer id = 0;

    public Integer getId() {
        return id;
    }

    public String olderBlogEntries() {
        String stringToBeReturned = "index.xhtml?id=" + (id + 1)
                + "&faces-redirect=true";
        return stringToBeReturned;
    }

    public void setId(Integer id) {
        this.id = id;
        System.out.println("id " + id + " received");
    }
}

另请参见:

这篇关于使用get参数在JSF上分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆