使用条件渲染时,请求范围的Bean中未处理输入值 [英] Input value not processed in request scoped bean when using conditional rendering

查看:105
本文介绍了使用条件渲染时,请求范围的Bean中未处理输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这种问题在这里已经问过百万遍了,但是我在相关的帖子中找不到解决我问题的方法.

I know this type of question has been asked million times here, but I couldn't find a solution for my problem in relevant posts.

JSF 1.2

我有一个请求范围的bean,其方法用作valueChangeListener:

I have a request-scoped bean with a method used as valueChangeListener:

class DoStuff{
    ...
    public void step1ChkStuffIncluded_CheckedChanged(ValueChangeEvent event){
        StuffDocument cd = (StuffDocument)getInfo("StuffDocument");
        if(cd == null){
            Logger.Error("DoStuff", "step1ChkStuffIncluded_CheckedChanged", "No stuff document (null)");
            return;
        }

        if (step1ChkStuffIncludedChecked){
            cd.handleChecked();
        }
        else{
            cd.handleUnchecked();
        }
    }
    ...
}

通过组件

如下(.jspx):

by a selectBooleanCheckbox component as follows (.jspx):

    ...
    </h:panelGroup> 
    <h:panelGroup rendered="#{DoStuff.pnlStep1}">
        <p>
        <label for="step1ChkStuffIncluded">#{DoStuff.step1ChkStuffIncludedText}</label>

        <h:selectBooleanCheckbox
            id="step1ChkStuffIncluded"
            onchange="submit();"
            value="#{DoStuff.step1ChkStuffIncludedChecked}"
            valueChangeListener="#{DoStuff.step1ChkStuffIncluded_CheckedChanged}">
        </h:selectBooleanCheckbox></p>
    </h:panelGroup>
    <div id="someDiv">
    ...

其中

xmlns:h="http://java.sun.com/jsf/html"

只要bean的作用域是会话,复选框的设置器和侦听器都将执行,而不是在请求作用域中.不幸的是,除此之外,我找不到其他线索.

Whenever the bean's scope is session, both setter and the listener for the checkbox are executed, but not in request scope. Unfortunately I can't find any clues other than that.

任何建议都将不胜感激.欢迎要求进一步澄清.

Any advise is greatly appreciated. Requests for further clarifications are welcome.

推荐答案

您在父组件上有一个rendered="#{DoStuff.pnlStep1}".在处理表单提交期间,JSF将作为攻击防范措施的一部分,确定是否根据服务器端条件呈现了输入组件(及其所有父组件).如果未渲染,则在处理过程中将完全跳过它.

You've there a rendered="#{DoStuff.pnlStep1}" on a parent component. During processing of the form submit, JSF will as part of attack safeguard determine if the input component (and all of its parents) is rendered according to the server side conditions. If it's not rendered, then it will simply be skipped altogether during the processing.

它在会话范围的Bean中起作用,但在请求范围的Bean中失败,表明rendered="#{DoStuff.pnlStep1}"后面的值是基于请求的变量/条件确定的,该变量/条件在显示表单的请求期间存在,但不存在在处理表单提交的请求期间.

That it works in a session scoped bean but fails in a request scoped bean indicates that the value behind rendered="#{DoStuff.pnlStep1}" is determined based on some request based variable/condition which was present during the request of displaying the form, but is absent during the request of processing the form submit.

要解决此问题,您需要确保在处理表单提交的请求期间,对于rendered="#{DoStuff.pnlStep1}"后面的值保留完全相同的变量/条件.有多种方法可以实现此目的,具体取决于条件的性质以及您如何提交表单.一种方法是<f:param><h:inputHidden>将基于请求的变量/条件作为请求参数传回.

To fix this, you need to make sure that you preserve exactly the same variable/condition for the value behind rendered="#{DoStuff.pnlStep1}" during the request of processing the form submit. There are several ways to achieve this, depending on the nature of the condition and how you're submitting the form. One of the ways is to pass the request based variable/condition back as a request parameter by <f:param> or <h:inputHidden>.

规范的JSF 2.0修复程序是将bean放入JSF 1.2中不可用的视图范围,但可以使用Tomahawk的<t:saveState>组件进行仿真.

The canonical JSF 2.0 fix would be to put the bean in the view scope which is not available in JSF 1.2, but can be simulated using Tomahawk's <t:saveState> component.

  • JSF 1.2: How to keep request scoped managed bean alive across postbacks on same view?
  • How to call an action method of a UICommand Component which was rendered conditionally?

这篇关于使用条件渲染时,请求范围的Bean中未处理输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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