3

I'm using p:tabView to split the editable data into the sections. All edit elements are places inside p:inplace components. The problem is specifically with p:selectOneMenu component.

When the whole tab view is refreshed, the value of the p:selectOneMenu that are on the other tabs as active, are set to null. I don't know why. Is this a bug, or is this a false usage of PrimeFaces componenents?

The environment:

  • PrimeFaces 3.4
  • MyFaces 2.0.7
  • IBM WebSphere 7.0

The way to reproduce the error:

  • Make a tabView with more that one tab
  • place in one tab the selectOneMenu inside inplace
  • make button that will update the tabView
  • choose the value for selectOneMenu, change the tab, click refresh and return to the tab with selectOneMenu

The code of the sample page and bean:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:p="http://primefaces.org/ui">
<h:head>
   <f:facet name="first">
      <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   </f:facet>
   <title>QMWPSUI</title>
   <h:outputScript library="qm" name="qmutils.js" />
   <h:outputScript library="qmwpsui" name="process.js" />
</h:head>
<h:body>


<h:form id="main">
   <p:blockUI block="main" trigger="refreshButton" widgetVar="block">
   <p:graphicImage
            value="#{resource['primefaces-qmui:images/waitprogress.gif']}" />
   </p:blockUI> 

   <h3>Test</h3>

      <p:commandButton id="refreshButton" widgetVar="refreshButton"
               action="#{test.refresh}" 
               icon="ui-icon-refresh"   title="#{i18n['action.reload']}"
               onclick="block.show()"
               update="tabView"/>

      <p:tabView id="tabView" orientation="top" dynamic="TRUE">
         <p:tab id="tab1" title="Tab 1" process="@this">
            <h:outputLabel value="Type*:" for="type"/>
            <p:inplace emptyLabel="Click here to change">
               <p:selectOneMenu id="typ" value="#{test.type}" effect="fade"
                  style="width:300px">
                  <f:selectItem itemLabel="" itemValue="" />
                  <f:selectItem itemLabel="Type a" itemValue="a" />
                  <f:selectItem itemLabel="Type b" itemValue="b" />
                  <f:selectItem itemLabel="Type c" itemValue="c" />
               </p:selectOneMenu>
            </p:inplace>
         </p:tab>
         <p:tab id="tab2" title="Tab 2" process="@this">
         </p:tab>
         <p:tab id="tab3" title="Tab 3" process="@this">
         </p:tab>
      </p:tabView>      

</h:form>

</h:body>

The bean class:

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name = "test")
@ViewScoped
public class TestBean implements Serializable {

   private static final long serialVersionUID = 1L;

   private static final Logger log = LoggerFactory.getLogger(TestBean.class);
   private String type;

   public String getType() {
      return type;
   }

   public void setType(String type) {
      this.type = type;
   }

   @Override
   public String toString() {
      StringBuilder builder = new StringBuilder();
      builder.append("TestBean [");
      if (type != null) {
         builder.append("type=");
         builder.append(type);
      }
      builder.append("]");
      return builder.toString();
   }

   public void refresh() {
      log.info("Refresh");
      log.info("Test = <{}>", this);
   }

}
Danubian Sailor
  • 1
  • 38
  • 145
  • 223

1 Answers1

-1

you need a converter for this case

public class EmptyToNullConverter implements Converter {

@Override
public Object getAsObject(FacesContext facesContext, UIComponent component,
        String value) {
    Object retorno = value;
    if (value == null || value.isEmpty()) {
        if (component instanceof EditableValueHolder) {
            ((EditableValueHolder) component).setSubmittedValue(null);
        }
        retorno = null;
    }
    return retorno;
}

@Override
public String getAsString(FacesContext facesContext, UIComponent component,
        Object value) {
    return (value == null) ? null : value.toString();
}

}

and define in your faces-config.xml

<converter>
    <converter-for-class>java.lang.String</converter-for-class>
    <converter-class>org.converter.EmptyToNullConverter</converter-class>
</converter>
ggarridov
  • 87
  • 1
  • 9