将字符(与传递字符串相比)传递给EL中的后备bean方法 [英] Passing a Character (vs passing String) to backing bean method in EL

查看:118
本文介绍了将字符(与传递字符串相比)传递给EL中的后备bean方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接从命令按钮调用设置器并传递一个值.我的问题是,如果以字符串形式传递回setter,则setter期望使用Character和jsf.有什么好方法可以在前端修复"此问题,而不必在我的支持bean上超载setter了?

I would like to call a setter directly from a command button and pass a value. My problem is that the setter is expecting a Character and jsf if passing it back as a String. Is there a good way to 'fix' this on the front end instead of having to over load the setter on my backing bean?

commandButton:

commandButton:

<p:commandButton value="SignOff"
    actionListener="#{manageItemHandler.dataEntryOp.setBomComplete('Y')}"
    rendered="#{speed2Session.isRendered('editManageItemOp')}"/>

从支持者那里获取/设置:

getter/setter from backing bean:

protected Character bomComplete;

/**
 * @return the bomComplete
 */
public Character getBomComplete() {
    return bomComplete;
}
/**
 * @param bomComplete the bomComplete to set
 */
public void setBomComplete(Character bomComplete) {
    this.bomComplete = bomComplete;
}

当我单击命令按钮时,我会得到

When I click the commandbutton I get

11:47:19,270 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-steves-172.16.8.26-15081-1) JSF1073: javax.faces.event.AbortProcessingException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=centerForm:j_idt271, Message=Method not found: data.operation.OperationData@595025a.setBomComplete(java.lang.String)
11:47:19,273 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-steves-172.16.8.26-15081-1) Method not found: data.operation.OperationData@595025a.setBomComplete(java.lang.String): javax.faces.event.AbortProcessingException: Method not found: data.operation.OperationData@595025a.setBomComplete(java.lang.String)

推荐答案

不幸的是,这是设计使然.引号中的所有内容在EL中均视为String.一种解决方法是改为传递String#charAt().

This is unfortunately by design. Everything in quotes is in EL treated as String. A workaround would be to pass String#charAt() instead.

#{manageItemHandler.dataEntryOp.setBomComplete('Y'.charAt(0))}

这只是丑陋的.另一种方法是传递其int代码点,对于Y,该代码点为89.

This is only ugly. An alternative is to pass its int codepoint instead, which is 89 for Y.

#{manageItemHandler.dataEntryOp.setBomComplete(89)}

但这并不是完全自我记录.更好的是只使用枚举.

But this is not exactly self-documenting. Much better is to just make use of enums.

public enum Choice {
    Y, N;
}

使用

protected Choice bomComplete;

您可以调用所需的方式

#{manageItemHandler.dataEntryOp.setBomComplete('Y')}

字符串'Y'将自动转换为该枚举.另外,枚举还具有更多其他优点,例如编译时类型安全.

The string 'Y' will be automatically converted to that enum. As a bonus, enums have many more additional advantages, such as compile time type safety.

这篇关于将字符(与传递字符串相比)传递给EL中的后备bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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