मुझे यकीन नहीं है कि आपको 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}
बदलना होगा।
हमेशा बलुससी के रूप में शानदार! धन्यवाद – Mariah