1
<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了但它没有得到验证。并且在后端代码中输入文本框的值为空。

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

4

2 回答 2

1

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

而是将 bean 放在视图范围内。只要您通过(ajax)回发与相同的视图交互,它就会存在。这是具有大量条件渲染的基于动态 ajax 的表单的正确范围。

也可以看看:

于 2013-02-06T01:41:21.247 回答
0

我试过这个@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
           }
    }

这对我有用,没有任何问题。

于 2013-02-15T03:41:01.660 回答