使用带有必需属性和命令的 f:viewParam [英] using f:viewParam with required attribute and commands

查看:15
本文介绍了使用带有必需属性和命令的 f:viewParam的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分享我使用 primefaces、f:viewParam 和 p:commandButton 的经验,并问几个问题.看看这个页面:

I want to share my experience using primefaces, f:viewParam and p:commandButton, and ask a few questions.Take a look at this page:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
  <h:head></h:head>
  <h:body>  
    <f:metadata>
      <f:viewParam required="true" name="id_file" value="#{bean.idFile}" />
    </f:metadata>
    <h:form id="tableform" prependId="false">              
      <p:commandButton actionListener="#{bean.myMethod())}" icon="ui-icon-search" title="View" />
    </h:form>
    <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />      
  </h:body>
</html>

支持 bean 有一个myMethod()"方法,它什么都不做.当您进入页面时,它需要id_file"参数并将其放入支持 bean 的 idFile 属性中.然后单击按钮并调用 myMethod.然后你再次点击,你会得到一个模糊的验证错误并且 myMethod 永远不会被调用:

The backing bean have a "myMethod()" method that does nothing. When you enter the page it expects the "id_file" parameter and put it in the idFile property of the backing bean. Then you click the button and the myMethod is called. Then you click again and you get an obscure validation error and myMethod is never called:

j_idt1: Validation Error: Value is required.j_idt1: Validation Error: Value is required.

首先记住,没有p:messages你是看不到这条消息的,你必须挖掘primefaces在ajax调用上发送的XML.其次,经过 4 个小时的调试,我尝试像这样更改 f:viewParam:

First of all, remember that without p:messages you can't see this message, you have to dig the XML that primefaces send on ajax calls. Secondly, after 4 hours of debugging I've tried to change the f:viewParam like this:

<f:viewParam name="id_file" value="#{bean.idFile}" />

没有必需":神奇地一切都开始工作,我可以点击1、2、3等,每次都会调用myMethod.所以,问题是 ajax 提交验证 f:viewParam 指定的参数,这对我来说听起来很傻,但好吧,我可以忍受它.

without "required": magically everything start working, I can click 1,2,3,etc and myMethod is called every time. So, the problem is that the ajax submit validate the parameter specified with f:viewParam, it sounds silly to me, but ok, I can live with it.

我的问题是:

  • 为什么第一次单击按钮时不会出现此验证错误?如果您查看 ajax POST,它们是相同的

  • why this validation error doesn't appear the first time button is clicked? If you look at the ajax POSTs they are identical

在部分 ajax 调用中验证视图参数(在我看来,属于视图)应该没问题吗?

it is supposed to be ok to validate the view parameters (that, in my idea, belongs to the view) on a partial ajax call?

有没有办法告诉 primefaces 不要验证特定的 ajax 请求(process="@this" 无法解析)?

is there a way to tell to primefaces not to validate on particular ajax request (process="@this" does not resolve)?

谢谢,我希望我的经验能让您避免花费数小时进行调试!

Thank you, I hope that my experience will allow you to avoid spending hours doing debugging!

推荐答案

  1. viewParam 是一个 UIComponent.这意味着它在语义上与 <h:commandButton/> 没有区别,并且它很可能通过每个规定的 JSF 请求处理生命周期阶段,直到并包括验证和转换.事实上,标签本身会导致任何给定的视图进入任何给定页面的完整处理,只要在那里

  1. The viewParam is a UIComponent. That means it's semantically no different from a <h:commandButton/> or a <h:inputText/> and it's liable to go thru every prescribed JSF request processing lifecycle phase, up to and including validation and conversion. In fact, the tag itself causes any given view to go into the full processing of any given page, just by being there

<p:commandButton/> 将执行 postback,这意味着它将重新请求相同的视图,使用一个帖子.因此,要解决您当前的问题,您需要根据该事实确定所需条件:

The <p:commandButton/> is going to do a postback, meaning, it's going to be re-requesting the same view, using a POST. So to solve your current problem, you need to base your required condition on that fact:

<f:viewParam  required="#{!facesContext.postback}" name="id_file" value="{bean.idFile}"/>

您从新条件中得到的是,仅在第一次请求时才需要该参数.后续回发不会触发条件.请确保您没有任何逻辑(可能在围绕该期望构建的 @PostConstruct

What you get from the new condition is that the parameter will be required only on the first request. Subsequent postbacks will not trigger the condition. Just be sure you don't have any logic (maybe in a @PostConstruct that's built around that expectation

这篇关于使用带有必需属性和命令的 f:viewParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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