2013-01-23 53 views
0

(java7,JSF /鑽嘴魚科v2.1.11,primefaces v3.4.2)回發(或「部分」回發)後,如何使`p:autocomplete`顯示其選定值?

問題

我有與用戶需要填充,與其他沿p:autocomplete輸入字段的表單字段(如下所示)。後

眼下,在頁面返回與驗證和/或所需的錯誤,用戶已設置不再顯示值。這與其他所有仍顯示其值已由用戶輸入/設置的字段相反。

問題:

爲什麼不顯示用戶輸入值?我可以強制它顯示值嗎?

(同樣,最初選擇的值是在p:autocomplete輸入框中可見。但是,在頁面提交,並驗證erriors返回後,所選擇的值不再顯示)

標籤看起來像這樣:

<p:autoComplete 
    id="code" 
    requiredMessage="code value required" 
    converter="acConverter" 
    style="overflow: hidden" 
    maxResults="200" 
    scrollHeight="150" 
    dropdown="false" 
    value="#{testBean.parmMap['code']}" 
    completeMethod="#{testBean.codeListComplete}" 
    var="entry" 
    itemLabel="#{entry.split(':')[1]}" 
    itemValue="#{entry.split(':')[0]}" 
    minQueryLength="1" 
    forceSelection="true"> 
</p:autoComplete> 

現在,如果用戶提交表單和窗體回用各種形式驗證錯誤,那麼,即使用戶已經正確地從p:autocomplete選擇,相關的輸入字段不顯示什麼他們已經選擇了(雖然它顯然保存在請求/記憶)。






僅供參考 - 下面是詳細信息,如果您需要它(否則,你可以忽略它)...

index.xhtml ...

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:c="http://java.sun.com/jsp/jstl/core" 
     xmlns:p="http://primefaces.org/ui"> 
    <f:view contentType="text/html"> 
     <h:head> 
      <title>test autocomplete...</title> 
      <meta charset="utf-8" /> 
     </h:head> 
     <h:body> 
      <h:form id="queryForm"> 

       <f:event type="postValidate" listener="#{testBean.validate}" /> 
       <p:panel id="queryPanel" header="test autocomplete..." style="width:100%;"> 

        <p:autoComplete 
         id="code" 
         requiredMessage="code value required" 
         converter="acConverter" 
         style="overflow: hidden" 
         maxResults="200" 
         scrollHeight="150" 
         dropdown="false" 
         value="#{testBean.parmMap['code']}" 
         completeMethod="#{testBean.codeListComplete}" 
         var="entry" 
         itemLabel="#{entry.split(':')[1]}" 
         itemValue="#{entry.split(':')[0]}" 
         minQueryLength="1" 
         forceSelection="true"> 
        </p:autoComplete> 

        <br/> 
        <br/> 

        <p:commandButton 
         id="submit" 
         value="Submit" 
         type="submit" 
         update="@form" 
         process="@form" 
         action="#{testBean.submitQuery}" 
         style="width:150px;" 
         styleClass="button"/> 

        <p:commandButton 
         value="Reset" 
         update="@form" 
         onclick="location.reload();return true;" 
         process="@this" 
         actionListener="#{testBean.reset}" 
         immediate="true" 
         ajax="false"/> 

       </p:panel> 
      </h:form> 

      <h:outputStylesheet library="styles" name="query.css"  /> 
      <h:outputScript  library="primefaces" name="/jquery/jquery.js" /> 
      <h:outputScript  library="primefaces" name="/jquery/plugins/ui/jquery-ui.custom.js" /> 
      <h:outputScript  library="primefaces" name="/jquery/plugins/inputmask/maskedinput.js" /> 

     </h:body> 
    </f:view> 
</html> 

TestBean.java ...

package aaa.bbb.ccc.war; 

import java.io.Serializable; 
import java.text.ParseException; 
import java.util.ArrayList; 
import java.util.LinkedHashMap; 
import java.util.List; 
import javax.faces.application.FacesMessage; 
import javax.faces.component.UIForm; 
import javax.faces.component.UIInput; 
import javax.faces.context.FacesContext; 
import javax.faces.event.ActionEvent; 
import javax.faces.event.ComponentSystemEvent; 
import org.apache.commons.lang.StringUtils; 
import org.primefaces.context.RequestContext; 
import org.springframework.context.annotation.Scope; 
import org.springframework.stereotype.Component; 

@Component("testBean") 
@Scope("request") 
public class TestBean implements Serializable 
{ 
    public TestBean() 
    { 
     parmMap = this.getParmMap(); 
     FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("parmMap", parmMap); 
    } 

    public void reset(ActionEvent event) 
    { 
     RequestContext.getCurrentInstance().reset("queryForm:queryPanel"); 
     LinkedHashMap<String, Object> m = new LinkedHashMap<String, Object>(); 
     FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("parmMap"); 
     setParmMap(m); 
    } 

    public String submitQuery() 
    { 
     FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("hitlistData"); 

     System.out.println("TestBean_________________________submitQuery()____________________parmMap contains:" + this.getParmMap().toString()); 

     if (this.getParmMap().isEmpty()) 
     { 
      return ""; 
     } 

     return "/page2.xhtml?faces-redirect=true"; 
    } 

    private static LinkedHashMap<String, Object> parmMap; 
    public LinkedHashMap<String, Object> getParmMap() 
    { 
     LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("parmMap"); 

     if (null == map) 
     { 
      map = new LinkedHashMap<String, Object>(); 
     } 

     return map; 
    } 

    public void setParmMap(LinkedHashMap<String, Object> map) 
    { 
     parmMap = map; 
     FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("parmMap", parmMap); 
    } 

    public void validate(ComponentSystemEvent e) throws ParseException 
    { 
     LinkedHashMap parmMap = this.getParmMap(); 
     UIForm queryForm = (UIForm) e.getComponent(); 
     FacesContext fc = FacesContext.getCurrentInstance(); 

     UIInput code_c = (UIInput) queryForm.findComponent("code"); 
     String code = (String) code_c.getValue(); 

     try 
     { 
      if (StringUtils.isBlank(code)) 
      { 
       code_c.setSubmittedValue(""); 
       code_c.setValid(false); 
       fc.addMessage(code_c.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, code_c.getRequiredMessage(), code_c.getRequiredMessage())); 
      } 

      if (fc.getMessageList().size() > 0) 
      { 
       fc.renderResponse(); 
      } 
     } 
     catch (Exception e1) 
     { 
      e1.printStackTrace(); 
     } 
    } 

    private static List<String> codeList; 
    public static List<String> getCodeList() 
    { 
     codeList = (List<String>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("codeList"); 

     if (null == codeList) 
     { 
      codeList = new ArrayList<String>(); 
      codeList.add("keyaaaa:valaaaa"); 
      codeList.add("keybbbb:valbbbb"); 
      codeList.add("keycccc:valcccc"); 
      codeList.add("keydddd:valdddd"); 
      codeList.add("keyeeee:valeeee"); 
      codeList.add("keyffff:valffff"); 
      codeList.add("keygggg:valgggg"); 
      codeList.add("keyhhhh:valhhhh"); 
      FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("codeList", codeList); 
     } 

     return codeList; 
    } 

    public void setCodeList(List<String> list) 
    { 
     codeList = list; 
    } 

    public static List<String> codeListComplete(String s)  //autocomplete "completeMethod"... 
    { 
     List<String> list = getCodeList(); 
     List<String> suggestions = new ArrayList<String>(); 
     for (String ss : list) 
     { 
      if (ss.toLowerCase().contains(s.toLowerCase())) 
      { 
       suggestions.add(ss); 
      } 
     } 

     return suggestions; 
    } 

} 

ACConverter.java(我在不成功的企圖刪除字符串值 「空」 創造了這個).. 。

package aaa.bbb.ccc.war; 

import javax.faces.application.FacesMessage; 
import javax.faces.component.UIComponent; 
import javax.faces.component.UIInput; 
import javax.faces.context.FacesContext; 
import javax.faces.convert.Converter; 
import javax.faces.convert.ConverterException; 
import javax.faces.convert.FacesConverter; 
import org.apache.commons.lang.StringUtils; 

@FacesConverter("acConverter") 
public class ACConverter implements Converter 
{ 
    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) 
    { 
     try 
     { 
      if (StringUtils.isBlank(value) || String.valueOf(value).equalsIgnoreCase("null")) 
      { 
       return ""; 
      } 
     } 
     catch (Exception e) 
     { 
      UIInput input = (UIInput) component; 
      FacesMessage msg = new FacesMessage(input.getConverterMessage(),input.getConverterMessage()); 
      msg.setSeverity(FacesMessage.SEVERITY_ERROR); 
      throw new ConverterException(msg); 
     } 

     return value; 
    } 

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

回答

0

可能是因爲你的bean被請求作用域。嘗試去視圖範圍代替。見here