【问题标题】:p:selectOneMenu value set to null, when in p:inplace in refreshed tab viewp:selectOneMenu 值设置为 null,当在 p:inplace 在刷新的选项卡视图中时
【发布时间】:2012-12-25 22:21:34
【问题描述】:

我正在使用p:tabView 将可编辑数据拆分为多个部分。所有编辑元素都位于p:inplace 组件内。问题出在p:selectOneMenu 组件上。

刷新整个选项卡视图时,其他选项卡上的p:selectOneMenu 的值设置为空。我不知道为什么。这是一个错误,还是 PrimeFaces 组件的错误用法?

环境:

  • PrimeFaces 3.4
  • MyFaces 2.0.7
  • IBM WebSphere 7.0

重现错误的方式:

  • 制作一个包含多个选项卡的 tabView
  • 将 selectOneMenu 放在一个选项卡中就地
  • 制作将更新 tabView 的按钮
  • 选择 selectOneMenu 的值,更改选项卡,单击刷新并使用 selectOneMenu 返回选项卡

示例页面和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>

bean 类:

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);
   }

}

【问题讨论】:

  • 只是为了让情况更笼统,&lt;h:selectOneMenu /&gt; 似乎也发生了这种情况,而没有&lt;p:inplace&gt;
  • 除了不使用动态标签还有其他解决方案吗?
  • 一种解决方法可能是在设置器中测试null,如果null 不更改type 的值。

标签: java jsf primefaces


【解决方案1】:

这种情况你需要一个转换器

公共类 EmptyToNullConverter 实现 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();
}

}

并在你的 faces-config.xml 中定义

<converter>
    <converter-for-class>java.lang.String</converter-for-class>
    <converter-class>org.converter.EmptyToNullConverter</converter-class>
</converter>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
相关资源
最近更新 更多