[英]Why SelectOneMenu ignores the converter?
I have a list of objects, a selectOneMenu
, in which I want to display that object list. 我有一个对象列表,一个
selectOneMenu
,我想在其中显示该对象列表。 I do not want to display toString() method result in that menu, and I created a converter instead. 我不想在该菜单中显示toString()方法结果,而是创建了一个转换器。
The problem is that in h:outputText element converter is called and I see expected value. 问题是在h:outputText元素转换器被调用,我看到了预期的值。 But when I attach that converter to h:selectOneMenu, converter is still called when page is rendered, but the result is ignored and toString() result is used instead.
但是当我将该转换器附加到h:selectOneMenu时,仍会在呈现页面时调用转换器,但忽略结果并使用toString()结果。 How can I fix it?
我该如何解决?
Some example code: 一些示例代码:
Converter: 转换器:
@FacesConverter(forClass=Priority.class)
public class PriorityConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
try {
InitialContext ic = new InitialContext();
PriorityEJB priorityEJB = (PriorityEJB) ic.lookup("java:global/TicketSentinel/TicketSentinel-ejb/PriorityEJB");
return priorityEJB.getPriorityByOrd(Integer.valueOf(value.charAt(0)));
} catch (NamingException ex) {
Logger.getLogger(PriorityConverter.class.getName()).log(Level.SEVERE, null, ex);
throw new ConverterException();
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Priority p = (Priority) value;
return p.getOrder() + " - " + p.getName();
}
}
Part of the page: 部分页面:
<h:outputText value="Priority:" />
<h:selectOneMenu value="#{ticketController.ticket.priority}">
<f:selectItems value="#{priorityController.priorityList}" />
</h:selectOneMenu>
Controller bean: 控制器bean:
@Named(value = "priorityController")
@RequestScoped
public class PriorityController {
@EJB
private PriorityEJB priorityEJB;
public List<Priority> getPriorityList() {
return priorityEJB.getPriorities();
}
}
I looked at the source of the page, and found this: 我查看了页面的来源,发现了这个:
<td><select name="j_idt18:j_idt26" size="1"> <option value="1 - Fatal">1 - - Fatal</option> <option value="2 - Critical">2 - - Critical</option> <option value="3 - Very Important">3 - - Very Important</option> <option value="4 - Important">4 - - Important</option> <option value="5 - Minor">5 - - Minor</option> <option value="6 - Fix if time">6 - - Fix if time</option> <option value="7 - Insignificant">7 - - Insignificant</option> </select></td>
So that engine prints the right value, but in the wrong place! 所以引擎打印正确的值,但在错误的地方! How can I make it place the text in
value
attribute into the body of <option> block? 如何将
value
属性中的文本放入<option>块的主体?
The converter is only used to convert the value of the dropdown (whatever you see as <option value>
and not to convert the label of the dropdown (whatever you see between <option>
and </option>
. The label is the one which is visible in the dropdown menu. 转换器仅用于转换下拉列表的值 (无论您看到的是什么
<option value>
而不是转换下拉列表的标签 (无论您在<option>
和</option>
之间看到什么。)标签是一个在下拉菜单中可见。
If you want to change the label to be the same as the value, then you just need to do it as such: 如果要将标签更改为与值相同,则只需执行以下操作:
<f:selectItems value="#{priorityController.priorityList}" var="priority"
itemValue="#{priority}" itemLabel="#{priority.order} - #{priority.name}" />
For the item value of #{priority}
the converter will still be called. 对于
#{priority}
的项值,仍将调用转换器。 Although I'd prefer to use some autogenerated database ID instead of kind of a label value so that conversion is more efficient. 虽然我更喜欢使用一些自动生成的数据库ID而不是标签值,以便转换更有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.