2011-08-11 15 views
10

मेरी JSF पेजक्यों चुनेंOneMenu कनवर्टर को ItemLabel भेजें? क्यों ऐसा होता है


<h:form> 
    <h:selectOneMenu id="studlist" value="#{studBean.selectedStudent}">     
    <p:ajax event="change" process="studlist" update="studdep" ></p:ajax> 
    <f:selectItems value="#{studBean.student}" var="s" 
        itemValue="#{s.studid}" itemLabel="#{s.name}"/> 
    <f:converter converterId="studentconverter"/> 
    </h:selectOneMenu> 
</h:form> 

कनवर्टर वर्ग इस कनवर्टर पर (StudentConverter)


public Object getAsObject(FacesContext context, UIComponent component, String value) { 

Student studConvert= new Student(); 
List<Student> students=new ArrayList<Student>(); 
students=(ArrayList<Student>)((UISelectItems  
      component.getChildren().get(0)).getValue(); 
} 

तर्क 'स्ट्रिंग मान' itemLabel मैं देता ?? मैं इस स्ट्रिंग

उत्तर

17

मुझे यकीन नहीं है कि आपको getAsObject() के अंदर आइटम मूल्य के बजाय आइटम लेबल क्यों मिला। शायद आपका getAsString() गलत कर रहा है और यह छात्र आईडी के आधार पर छात्र का नाम लौटा रहा है।

किसी भी तरह से, मैं बता सकता हूं कि आपका itemValue निश्चित रूप से सही नहीं है।

<h:selectOneMenu id="studlist" value="#{studBean.selectedStudent}"> 
    <f:selectItems value="#{studBean.student}" var="s" 
     itemValue="#{s.studid}" itemLabel="#{s.name}" /> 
    <f:converter converterId="studentconverter" /> 
</h:selectOneMenu> 

एक कनवर्टर एक जटिल जावा वस्तु और एक स्ट्रिंग प्रतिनिधित्व इतना है कि यह एक HTTP अनुरोध पैरामीटर के रूप में चारों ओर पारित किया जा सकता के बीच परिवर्तित करने के लिए प्रयोग की जाने वाली intented है। हालांकि, आप छात्र आईडी को पूरे छात्र ऑब्जेक्ट के बजाय आइटम वैल्यू के रूप में निर्दिष्ट कर रहे हैं। आपको इसके बजाय पूरे छात्र ऑब्जेक्ट को निर्दिष्ट करने की आवश्यकता है। आपको यह भी सुनिश्चित करना चाहिए कि #{studBean.selectedStudent}Student संपत्ति को संदर्भित करता है, कुछ Long छात्र आईडी का प्रतिनिधित्व करने वाली संपत्ति नहीं।

इस प्रकार जब आप itemValue तय:

<h:selectOneMenu id="studlist" value="#{studBean.selectedStudent}"> 
    <f:selectItems value="#{studBean.student}" var="s" 
     itemValue="#{s}" itemLabel="#{s.name}" /> 
    <f:converter converterId="studentconverter" /> 
</h:selectOneMenu> 

और अपने कनवर्टर इस प्रकार है (तुच्छ nullchecks छोड़े गए):

public String getAsString(FacesContext context, UIComponent component, Object value) { 
    // This method is called when item value is to be converted to HTTP request parameter. 
    // Normal practice is to return an unique identifier here, such as student ID. 
    Student student = (Student) value; 
    Long id = student.getStudid(); 
    return String.valueOf(id); 
} 

public Object getAsObject(FacesContext context, UIComponent component, String value) { 
    // This method is called when HTTP request parameter is to be converted to item value. 
    // You need to convert the student ID back to Student. 
    Long id = Long.valueOf(value); 
    Student student = someStudentService.find(id); 
    return student; 
} 

तो यह काम करना चाहिए।

फिर, आप अपने itemValue रख सकता है के रूप में आप शुरू में था और <f:converter> पूरी तरह निकाल दें, लेकिन फिर आप एक Long संपत्ति छात्र आईडी का प्रतिनिधित्व करने के लिए बात करने के लिए #{studBean.selectedStudent} बदलना होगा।

+0

हमेशा बलुससी के रूप में शानदार! धन्यवाद – Mariah

1

आप की h:selectOneMenu

आपका पेज f:selecitems विशेषता में selectitem सूची का उपयोग करने के लिए इस तरह होगा है पर itemValue छड़ी;

<h:form> 
    <h:selectOneMenu id="studlist" value="#{studBean.selectedStudent}"> 
    <p:ajax event="change" process="studlist" update="studdep" ></p:ajax>  
    <f:selectItems value="#{studBean.studentSelectItemList}" />  
    <f:converter converterId="studentconverter"/>  
    </h:selectOneMenu> 
</h:form> 

बैकिंग बीन पक्ष पर आपको छात्र चयन करने के लिए चयन करें।

private List<SelectItem> studentSelectItemList; 

//fill studentSelectItemList at the appropriate place 
    studentSelectItemList.add(new SelectItem(studentId,studentName)); 

इन सेटिंग्स के बाद आपके पास छात्र आईडी को चुनिंदा मूल्य के रूप में होना चाहिए।

+0

इससे कोई फर्क नहीं पड़ता। – BalusC

0

आज मुझे एक ही समस्या थी।

यह इस गलत प्रतिपादन के कारण होता है:

<select ...> 
    <option>None</option> 
    <option value="1">First</option> 
    <option value="2">Second</option> 
</select> 

एक रिक्त स्ट्रिंग के बजाय लेबल प्रस्तुत करने में "कोई नहीं" विकल्प परिणाम के लिए value="" को छोड़ते हुए।

हालांकि इस को हल करने और पहले विकल्प के लिए रेंडरर लिखने value="" बस सुनिश्चित getAsString()रिटर्न कभी नहीं अशक्त बनाने बनाने के लिए, "" (रिक्त स्ट्रिंग) लौटने के बजाय करने के लिए।


@BalusC

<h:form id="form"> 
    ... 

    <p:selectOneMenu id="targetStep" value="#{action.targetStep}" required="true"> 
     <o:converter converterId="omnifaces.ListIndexConverter" list="#{entity.stepList}" /> 
     <f:selectItems var="step" value="#{entity.stepList}" itemLabel="#{step.name}" 
      itemValue="#{step}" /> 
    </p:selectOneMenu> 

    <p:commandButton process="@this" update="@widgetVar(stepDialog)" 
     oncomplete="PF('stepDialog').show()" icon="#{icons.addStep}" 
     value="#{bundle.addStep}"> 
     <f:setPropertyActionListener target="#{viewScope.step}" 
      value="#{s:newInstance('it.shape.edea2.jpa.WorkflowStep')}" /> 
    </p:commandButton> 

    <p:message for="targetStep" /> 

    ... 
</h:form> 

<p:dialog widgetVar="stepDialog" header="#{bundle.addStep}" modal="true" dynamic="true" 
    resizable="false"> 
    <h:form> 
     <p:panelGrid columns="2" styleClass="app-full-width"> 
      <h:outputLabel value="#{bundle.name}" /> 
      <h:panelGroup> 
       <p:inputText id="name" value="#{step.name}" required="true" /> 
       <p:message for="name" /> 
      </h:panelGroup> 

      ... 

      <f:facet name="footer"> 
       <p:commandButton process="@form" update="@form :form" 
        oncomplete="hideDialog('stepDialog', args)" icon="#{icons.confirm}" 
        value="#{bundle.confirm}"> 
        <p:collector value="#{step}" addTo="#{entity.stepList}" /> 
        <f:setPropertyActionListener target="#{action.targetStep}" 
         value="#{step}" /> 
       </p:commandButton> 
      </f:facet> 
     </p:panelGrid> 
    </h:form> 
</p:dialog> 

आपका omnifaces.ListIndexConverter बचाव :)

+0

असहमत। यदि '# {s.studid}' शून्य है (संभवतः एक नया छात्र उदाहरण जो किसी सेवा द्वारा लोड नहीं किया जाता है) और 'getAsString()' शून्य सबमिट किए जाने के लिए शून्य लौटा रहा है (संभवतः), यह स्थिति हो सकती है। और यह स्निपेट बताता है * क्यों चुनें IneMenu कनवर्टर को ItemLabel भेजें? *। हालांकि, अगर मुझे कुछ घंटे पहले यह प्रश्न मिलेगा, तो मैं उन घंटों से बच जाऊंगा :) –

+0

मुझे पता है कि यह सामान्य नहीं है, लेकिन मैं कभी-कभी इस परिदृश्य को पूरा करता हूं, जहां उपयोगकर्ता को एक इकाई का चयन करना होता है और चयन स्थान का विस्तार करना पड़ सकता है खुद को। अद्यतन उत्तर में वास्तविक उपयोग केस। –

0

BalusC (फिर) के लिए मेरे लिए यह एक किसी न किसी। मुझे एक ही समस्या थी और जैसा कि बलुस ने पहले बताया था कि मेरे कनवर्टर की getAsString() विधि ने मेरी ऑब्जेक्ट की "firstname" प्रॉपर्टी वापस कर दी है।

@Override सार्वजनिक स्ट्रिंग getAsString (FacesContext संदर्भ, UIComponent घटक, वस्तु मूल्य) {

if (value == null || value.equals("")) { 
     return ""; 
    } 
    else { 
     return String.valueOf(((Employee) value).getfirstname()); 
    } 

} 

मैं इस बदल आईडी वापस जाने के लिए और यह उम्मीद के रूप में काम करना शुरू किया।

@Override सार्वजनिक स्ट्रिंग getAsString (FacesContext संदर्भ, UIComponent घटक, वस्तु मूल्य) {

if (value == null || value.equals("")) { 
     return ""; 
    } 
    else { 
     return String.valueOf(((Employee) value).getId()); 
    } 

} 

BalusC अपने सिद्धांत की व्याख्या करने के प्रयास बेहद सराहनीय है। तुम स्वर्ग हो!

संबंधित मुद्दे