使用F:viewParam所需的属性和命令所需、属性、命令、viewParam

2023-09-11 22:31:10 作者:江山

我想用primefaces,女分享我的经验:viewParam和p:命令按钮,和问几个questions.Take看看这个页面:

 < XML版本=1.0编码=UTF-8&GT?;
<!DOCTYPE HTML>
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml
      的xmlns:H =htt​​p://java.sun.com/jsf/html
      的xmlns:F =htt​​p://java.sun.com/jsf/core
      的xmlns:UI =htt​​p://java.sun.com/jsf/facelets
      的xmlns:P =htt​​p://primefaces.org/ui>
  < H:头>< / H:头>
  < H:身体GT;
    < F:元数据>
      < F:viewParam所需=真正的NAME =id_file值=#{bean.idFile}/>
    < / F:元数据>
    < H:格式ID =tableformprependId =假>
      <电话号码:的commandButton的ActionListener =#{bean.myMethod())}图标=UI图标搜索标题=查看/>
    < /小时:形式GT;
    <电话号码:邮件ID =消息showDetail =真正的自动更新=真正的可关闭=真/>
  < / H:身体GT;
< / HTML>
 

支持bean有一个myMethod的()方法,什么也不做。当你进入的页面,预​​计id_file参数,并把它放到后台bean的idFile财产。然后单击按钮,myMethod的叫。然后你再次点击,你会得到一个不起眼的验证错误和myMethod的是从来没有所谓的:

  j_idt1:验证错误:值是required.j_idt1:验证错误:值是必需的。
 

首先,请记住,如果没有号码:信息你看不到这条消息,你要挖的primefaces发送关于Ajax调用的XML。其次,经过4个小时的调试,我试图改变F:viewParam是这样的:

 < F:viewParam名=id_file值=#{bean.idFile}/>
 
top 命令 Linux系列之学会使用Top命令进行系统监控

不需要:一切都奇迹般地开始工作,我可以点击1,2,3等,并在myMethod被称为每次。所以,问题是,AJAX提交验证与F的具体参数:viewParam,这听起来愚蠢的我,但没关系,我可以接受它。

我的问题是:

为什么这个验证错误不会出现第一次按钮被点击?如果你看一下AJAX的POST它们是相同的。

这应该是OK确认在部分AJAX调用的视图参数(即,在我的想法,属于视图)?

有没有办法告诉给primefaces不验证对特定的ajax请求(进程=@这个没有解决)?

感谢你,我希望我的经验可以让你避免花时间做调试!

解决方案

viewParam UIComponent 。这意味着它是从&LT语义上没有什么不同; H:的commandButton /> < H:inputText的/> ,它是容易走通每prescribed JSF请求处理生命周期阶段,直至并包括验证和转换。实际上,标签本身导致任何给定的,以便进入任何给定的页面,充分处理只是在那里

<电话号码:的commandButton /> 会做一个的回发的,这意味着,这将是重请求相同的观点,使用POST。因此,要解决你目前的问题,则需要基于你所需要的条件的事实:

 < F:viewParam所需=#{facesContext.postback!}NAME =id_file值={bean.idFile}/>
 

您在新的条件下得到的是该参数将被要求只在第一次请求。随后回发不会触发条件。只要确保你没有任何逻辑(也许在一个 @PostConstruct 就是建立在这种期望

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>

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.

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}" />

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.

My questions are:

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

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

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!

解决方案

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

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}"/>

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