<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fabio Brandão - Blog &#187; postback</title>
	<atom:link href="http://www.fnbrandao.com.br/blog/tag/postback/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fnbrandao.com.br/blog</link>
	<description></description>
	<lastBuildDate>Fri, 06 Jan 2012 15:22:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PostBack no JSF</title>
		<link>http://www.fnbrandao.com.br/blog/2008/06/postback-no-jsf/</link>
		<comments>http://www.fnbrandao.com.br/blog/2008/06/postback-no-jsf/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 12:06:32 +0000</pubDate>
		<dc:creator>fabio</dc:creator>
				<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JEE]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[postback]]></category>

		<guid isPermaLink="false">http://www.fnbrandao.com.br/blog/?p=7</guid>
		<description><![CDATA[Um grande problema com o JSF é que na especificação 1.1 não existe algum método para saber se a página está sendo executada no postback (quando você reenvia os dados para ela mesma). Mas existe o seguinte workaround:

package web.jsf.vh;

import java.io.IOException;
import java.util.Locale;
import java.util.Map;

import javax.faces.FacesException;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

public class PostBackViewHandler extends ViewHandler {

  protected ViewHandler baseViewHandler;

 [...]]]></description>
			<content:encoded><![CDATA[<p>Um grande problema com o JSF é que na especificação 1.1 não existe algum método para saber se a página está sendo executada no postback (quando você reenvia os dados para ela mesma). Mas existe o seguinte workaround:</p>
<p><code>
<pre>package web.jsf.vh;

import java.io.IOException;
import java.util.Locale;
import java.util.Map;

import javax.faces.FacesException;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

public class PostBackViewHandler extends ViewHandler {

  protected ViewHandler baseViewHandler;

  public CustomViewHandler(ViewHandler viewHandler) {
    super();
    this.baseViewHandler = viewHandler;
  }

  public Locale calculateLocale(FacesContext facesContext) {
    return baseViewHandler.calculateLocale(facesContext);
  }

  public String calculateRenderKitId(FacesContext facesContext) {
    return baseViewHandler.calculateRenderKitId(facesContext);
  }

  public UIViewRoot createView(FacesContext facesContext, String arg1) {
    setPostback(facesContext, false);
    return baseViewHandler.createView(facesContext, arg1);
  }

  public String getActionURL(FacesContext facesContext, String arg1) {
    return baseViewHandler.getActionURL(facesContext, arg1);
  }

  public String getResourceURL(FacesContext facesContext, String arg1) {
    return baseViewHandler.getResourceURL(facesContext, arg1);
  }

  public void renderView(FacesContext facesContext, UIViewRoot arg1) throws IOException, FacesException {
    baseViewHandler.renderView(facesContext, arg1);

  }

  public UIViewRoot restoreView(FacesContext facesContext, String arg1) {
    setPostback(facesContext, true);
    return baseViewHandler.restoreView(facesContext, arg1);
  }

  public void writeState(FacesContext facesContext) throws IOException {
    baseViewHandler.writeState(facesContext);
  }

  public Map getRequestScope(FacesContext facesContext) {
    return (Map)facesContext.getApplication().createValueBinding(“#{requestScope}”).getValue(facesContext);
  }

  public void setPostback(FacesContext facesContext, boolean value) {
    getRequestScope(facesContext).put(“ispostback”, new Boolean(value));
  }

}</pre>
<p></code></p>
<p>O código acima é um View Handler do JSF, agora basta registrá-lo no <i>&#8220;facesconfig.xml&#8221;</i>:</p>
<p><code>
<pre>&lt;application&gt;
   &lt;view-handler&gt;web.jsf.vh.PostBackViewHandler&lt;/view-handler&gt;
&lt;/application&gt;</pre>
<p></code></p>
<p>E para saber em sua página se é um postback:</p>
<p><code>
<pre>public boolean isPostback() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    Map requestScope = (Map)facesContext.getApplication().createValueBinding(“#{requestScope}”).getValue(facesContext);
    boolean ispostback = ((Boolean)requestScope.get(“ispostback”)).booleanValue();
    return ispostback;
}</pre>
<p></code></p>
<p>Obs.: Esse workaround só funciona se o estado da view não é salva no servidor.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fnbrandao.com.br/blog/2008/06/postback-no-jsf/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

