繁体   English   中英

无法验证在jsf 2.0中使用ajax呈现的输入文本字段

[英]Not able to validate input text field rendered using ajax in jsf 2.0

<h:form>
 <h:selectManyMenu id="carsList"
                   value="#{bean.carList}"
                   style="width:400px; height:100px" label="List">
                    <f:selectItems  value="#{bean.cars}" var="i"
                                    itemLabel="#{i.code}" itemValue="#{i.Name}" />
                    <f:selectItem  itemLabel="Other" itemValue="other"/>
                    <f:ajax event="change" execute="@this" render="othermodel"/>
        </h:selectManyMenu>
                    <br></br>
                    <h:panelGroup id="othermodel">
                            <h:outputText  value="Others:" style="margin-top:10px;color:red;font-weight:bold;"
                            rendered="#{bean.carList.contains('other')}" />
                            <h:inputText size="50" id="otherinput" required="true"
                            rendered="#{bean.carList.contains('other')}"/>
                            <h:message for="otherinput"></h:message>
                    </h:panelGroup>
                       <h:commandButton value="Next" action="#{bean.submitForm}"/>
    </h:form>

我的bean是requestScoped ,当我的carList具有other值时,我能够显示panelGrid但是当用户未在使用AJAX呈现的输入字段中输入任何值时,即使我指定了required=true但它也没有得到验证。 在后端代码中,输入文本框的值为null。

如何验证使用AJAX渲染的输入字段,为什么我的值变成null? 我正在使用JSF 2.0

在提交表单的请求期间,将重新评估rendered属性。 您的bean受请求范围限制,因此在所有属性均设置为default的每个单个请求(也包括ajax请求)上重新创建。 因此,在更新模型值阶段填充carList之前, carList属性也将被清空,并且rendered属性将评估为false 因此,对于JSF, <h:inputText>不会被渲染,因此不会被处理。

而是将bean放入视图范围。 只要您通过(ajax)回发与同一视图进行交互,它就会一直存在。 这是具有很多条件渲染的基于动态Ajax表单的正确范围。

也可以看看:

我尝试了@ViewScope 当我将Spring managed service设为瞬态时,反序列化后我得到的是null引用。 因此,我尝试了以下方法来获取Spring Service

    @ManagedBean(name="bean")
    @ViewScoped
    public class Bean implements Serializable {


    @ManagedProperty(value="#{appService}")
    private transient AppService appService;

      // Getting AppService Object which is singleton in the application during deserialization 
     private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
              stream.defaultReadObject();
              FacesContext context = FacesContext.getCurrentInstance();
              appService = (AppService)context.getApplication()
                    .evaluateExpressionGet(context, "#{appService}", AppService.class);
                  stream.close();  // not sure i have to do this
           }
    }

这对我来说没有任何问题。

暂无
暂无

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

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