繁体   English   中英

使用get参数在JSF上分页

[英]Pagination on JSF using get parameters

我在index.xhtml上,我有这段代码:

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

BlogEntryListerBean是RequestScoped 这是我在oldBlogEntries中的代码

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

这有什么不对? 我为什么不去index.xhtml?id = 2

谢谢。

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

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

您的问题有一个简单的解决方案,它基于使用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代码将是这样的:

@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;
    }
}

在这种情况下,您需要将参数传递给action方法,以便在类路径中使用EL 2.2。 如果您尚未设置,请按照以下步骤操作

生命周期应该是:

  • 首先请求,打开浏览器并对index.xhtml执行GET请求。 没有发送param。
  • 您按一下按钮,将表单作为POST请求提交。 操作方法重定向到http://localhost:8080/basic-jsf/index.xhtml?id=1 ,这会导致客户端对此URL执行GET请求。 现在,JSF捕获id视图param并将其绑定到名称为id的视图。
  • 再次按下该按钮时,JSF使用当前参数调用action方法,值为1 重定向现在转到http://localhost:8080/basic-jsf/index.xhtml?id=2

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

<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方法:

@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");
    }
}

也可以看看:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM