java JSF 1.2:valueChangeListener 事件不返回新选择的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1861359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:17:06  来源:igfitidea点击:

JSF 1.2: valueChangeListener event not returning newly selected value

javajsfvaluechangelistener

提问by icepax

I have this backing bean:

我有这个支持 bean:

public class PageBean 
{
    private List<SelectItem> selectPages;
    private List<SelectItem> selectRowsPerPage;
    private String selectedPage;
    private String selectedRowsPerPage;
    private int pages = 0;

// getter methods
public boolean getRender()
{
    boolean rendered = pages > 0? true: false;
    return rendered;
}   

public List<SelectItem> getSelectPages() 
{
    int value = 0;

    selectPages = new ArrayList<SelectItem>();
    for (int i = 1; i < (pages + 1); i++) 
    {
        if (i > 1) { value = i * 10; }
        selectPages.add(new SelectItem(Integer.toString(value), Integer.toString(i)));
    }   

    return selectPages;
}

public String getSelectedPage()
{
    if (selectedPage == null) {
        selectedPage = "1";
    }

    return selectedPage;
}

// setter methods
public void setSelectPages(List<SelectItem> selectPages) {
    this.selectPages = selectPages;
}  

public void setSelectedPage(String selectedPage) {
    this.selectedPage = selectedPage;
}    

// action methods
public void changePage(ValueChangeEvent event)
{
    PhaseId phase = event.getPhaseId();

    if (phase.equals(PhaseId.INVOKE_APPLICATION)) {
        System.out.println((String) event.getNewValue());
        setSelectedPage((String) event.getNewValue());
        FacesContext.getCurrentInstance().renderResponse();
    } else {
        event.setPhaseId(PhaseId.INVOKE_APPLICATION);
        event.queue();
    } 
}    
}

And the h:selectOneMenu is:

而 h:selectOneMenu 是:

<h:selectOneMenu id="page" value="#{pageBean.selectedPage}"
   valueChangeListener="#{pageBean.changePage}" onchange="submit()">
   <f:selectItems value="#{pageBean.selectPages}" />
</h:selectOneMenu>

The above codes for the changePage()method do not return the new selected page value from the h:selectOneMenu. Instead, it returns the page value prior to submit. I don't understand why.

changePage()方法的上述代码不会从 h:selectOneMenu 返回新的选定页面值。相反,它在提交之前返回页面值。我不明白为什么。

Can someone please help? Have been stuck on this for 2 days now.

有人可以帮忙吗?已经坚持了2天了。

回答by BalusC

Apparently you're doing a redirect instead of a forward, while the bean is request scoped. A redirect creates a newrequest, hereby garbaging all initial request scoped attributes, including request scoped beans. It will thus cause recreation of request scoped bean with all default properties. To solve this problem, remove the <redirect/>entry from the <navigation-case>entry in faces-config.xml, if any.

显然,您正在执行重定向而不是转发,而 bean 是请求范围的。重定向会创建一个请求,从而垃圾所有初始请求范围的属性,包括请求范围的 bean。因此,它将导致重新创建具有所有默认属性的请求范围 bean。要解决此问题,请<redirect/>从 中的<navigation-case>条目中删除该条目faces-config.xml(如果有)。

If this is for pure page-to-page navigation, I'd recommend another approach for this. Get rid of the valueChangeListener, have the page URL's as SelectItem values and replace submit()with:

如果这是纯粹的页面到页面导航,我会推荐另一种方法。去掉valueChangeListener,将页面 URL 作为 SelectItem 值并替换submit()为:

onchange="window.location=this.options[this.selectedIndex].value"

You namely don't want to use POSTfor simple page-to-page navigation.

您不想使用POST简单的页面到页面导航。

Hope this helps.

希望这可以帮助。

Edit:as per the comments, you actually want datatable pagination and this dropdown must instantly go to page X of the datatable at the same JSF page? In this case, forget my answer above, it wasn't clear from your question and I didn't realize that you want datatable pagination.

编辑:根据评论,您实际上想要数据表分页,并且此下拉列表必须立即转到同一 JSF 页面中数据表的第 X 页?在这种情况下,忘记我上面的回答,从你的问题中不清楚,我没有意识到你想要数据表分页。

The as far posted information and code looks fine. Have you debugged the code to see what happens with property values? Isn't it nulled out afterwards? Is the correct value returned during render? Does the dropdown list return the expected items during render? That kind of trivial things. Maybe something else which you didn't post about is colliding with this all.

到目前为止发布的信息和代码看起来不错。您是否已调试代码以查看属性值会发生什么?之后不是被清零了吗?渲染过程中是否返回了正确的值?下拉列表是否在渲染期间返回预期的项目?那种琐碎的事情。也许您没有发布的其他内容与这一切相冲突。

Edit 2:I actually created an SSCCEto see if it really works and as I expected, it just works. Your problem lies somewhere else. Maybe a buggy Converter? Maybe it was actuallya redirect?

编辑 2:我实际上创建了一个SSCCE来查看它是否真的有效,正如我所料,它确实有效。你的问题出在别处。也许是一辆越野车Converter?也许它实际上是一个重定向?

MyBean (request scoped):

MyBean(请求范围):

public class MyBean {

    private Integer page;
    private List<SelectItem> pages = new ArrayList<SelectItem>();

    public MyBean() {
        for (int i = 1; i <= 10; i++) {
            pages.add(new SelectItem((i == 1) ? i : (i * 10)));
        }
    }

    public void changePage(ValueChangeEvent event) {
        if (event.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
            setPage((Integer) event.getNewValue());
        } else {
            event.setPhaseId(PhaseId.INVOKE_APPLICATION);
            event.queue();
        }
    }

    public Integer getPage() {
        return page;
    }

    public List<SelectItem> getPages() {
        return pages;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

}

(by the way, you used Stringinstead of Integerwhich is imho the wrong type for numerical values, but here it works fine with Stringas well)

(顺便说一句,您使用的String不是Integerwhich 是 imho 错误的数值类型,但在这里它也可以正常工作String

JSF page:

JSF页面:

<h:form>
    <h:selectOneMenu value="#{myBean.page}" onchange="submit()"
        valueChangeListener="#{myBean.changePage}">
        <f:selectItems value="#{myBean.pages}" />
    </h:selectOneMenu>
</h:form>

I used Mojarra 1.2_13 on Tomcat 6.0.20.

我在 Tomcat 6.0.20 上使用了 Mojarra 1.2_13。

By the way, the valueChangeListeneris entirely superfluous. It works as fine without it. JSF just sets the selected page during Update Model Valuesphase.

顺便说一句,valueChangeListener完全是多余的。没有它它也能正常工作。JSF 只是在Update Model Values阶段设置所选页面。