为什么要选择OneMenu将ItemLabel发送到转换器? [英] Why selectOneMenu Send ItemLabel to the converter?

查看:103
本文介绍了为什么要选择OneMenu将ItemLabel发送到转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSF页面

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

converter class(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 我为什么会这样? 我将itemValue放在此字符串上

on this converter the Argument 'String value' gives the itemLabel i why it happens?? i wand the itemValue on this string

推荐答案

我不确定为什么要在getAsObject()中得到项目标签而不是项目值.可能您的getAsString()做错了,并且正在根据学生ID返回学生姓名.

I'm not sure why you got the item label instead of the item value inside getAsObject(). Perhaps your getAsString() is doing it wrong and it is returning student name based on the student ID.

无论如何,我可以说您的itemValue绝对不正确.

At any way, I can tell that your itemValue is definitely not right.

<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>

旨在使用转换器在复杂的Java对象和String表示形式之间进行转换,以便可以将其作为HTTP请求参数传递.但是,您将学生ID指定为项目值,而不是整个学生对象.您需要改为指定整个学生对象.您还应该确保#{studBean.selectedStudent}引用的是Student属性,而不是某些表示学生ID的Long属性.

A converter is intented to be used to convert between a complex Java object and a String representation so that it can be passed around as a HTTP request parameter. However, you're specifying the student ID as item value instead of the whole student object. You need to specify the whole student object instead. You should also ensure that #{studBean.selectedStudent} refers to a Student property, not some Long property representing the student ID.

按如下所示修复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):

and your converter as follows (trivial nullchecks omitted):

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>,但是随后必须更改#{studBean.selectedStudent}指向指向代表学生ID的Long属性.

Alternatively, you could keep your itemValue as you initially had and remove the <f:converter> altogether, but then you have to change #{studBean.selectedStudent} to point to a Long property representing the student ID.

这篇关于为什么要选择OneMenu将ItemLabel发送到转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆