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

查看:29
本文介绍了为什么 selectOneMenu 将 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 对象和字符串表示之间进行转换,以便它可以作为 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 时:

When you fix the itemValue as follows:

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

和您的转换器如下(省略了琐碎的空检查):

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 并完全删除 ,但随后您必须更改 #{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.

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

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