【问题标题】:Not able to validate input text field rendered using ajax in jsf 2.0无法验证在 jsf 2.0 中使用 ajax 呈现的输入文本字段
【发布时间】:2013-01-21 03:01:15
【问题描述】:
<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 呈现的输入字段,为什么我的值是空值?我正在使用 JSF 2.0

【问题讨论】:

    标签: ajax jsf-2 rendering requiredfieldvalidator


    【解决方案1】:

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

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

    另见:

    【讨论】:

    • @BalusC .....嘿,在我将 bean 作为 viewscoped 并将用于进行数据库调用的 service 类注入 @Managedproperty 但它从未被初始化之后......我把它变成了暂时的。它是一个接口。如何初始化该服务。
    • 服务类根本不应该是 JSF 托管的 bean(它们不能直接在视图中使用,也不能正确管理 DB 事务),而是一个 EJB(例如@Stateless)。它们作为可序列化代理注入,因此您无需将它们设为瞬态。
    • 是的,我的服务类是 Spring Managed bean。如果我不使用transient,它会抛出NotserializableException。
    • 嗯嗯。厄运。这确实不是我第一次看到有人对 Spring 托管 bean 有这种问题。好吧,除了建议只使用标准 Java EE 堆栈之外,我无能为力。 EJB 更加流畅(不需要 XML 混乱)并且默认情况下已经可序列化。
    • 很遗憾,我现在无法更改配置。因为我们将在几天后投入生产。viewScopedonly 解决了我的上述问题,还是有其他我可以解决的问题。跨度>
    【解决方案2】:

    我用@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
               }
        }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多