将一个Character(vs传递String)传递给EL中的backing bean方法

时间:2012-10-22 16:05:59

标签: jsf primefaces el

我想直接从命令按钮调用一个setter并传递一个值。我的问题是,如果将set作为String传回,则setter期望一个Character和jsf。有没有一种好方法可以在前端“修复”这个,而不必过度加载我的支持bean上的setter?

的commandButton:

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

getter / setter:

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

当我点击命令按钮时,我得到了

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)

2 个答案:

答案 0 :(得分:3)

遗憾的是,这是设计上的。引号中的所有内容都在EL中视为String。解决方法是改为通过String#charAt()

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

这只是丑陋的。另一种方法是改为传递int代码点,89Y

#{manageItemHandler.dataEntryOp.setBomComplete(89)}

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

public enum Choice {
    Y, N;
}

protected Choice bomComplete;

你可以调用所需的方式

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

字符串'Y'将自动转换为该枚举。作为奖励,枚举还有许多额外的优点,例如编译时类型安全性。

答案 1 :(得分:1)

我尝试不使用charAt(0)技巧直接使用简单的引号...

,当参数类型为charCharacter时,此方法可以正常工作。

传递字符的示例:

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

传递字符串文字和字符常量的示例:

<p:panel style="border:none;#{vC.traceUpdate('p:panel','X')}">

traceUpdate()java方法的代码如下:

@ManagedBean(name = "vC")
@ViewScoped
public class ActionViewController
extends AbstractViewController
    {
    public String traceUpdate(String s, Character c)
        {
        System.out.println("s:" + s + " " + c);
        return "";
        }

OR

@ManagedBean(name = "vC")
@ViewScoped
public class ActionViewController
extends AbstractViewController
    {
    public String traceUpdate(String s, char c)
        {
        System.out.println("s:" + s + " " + c);
        return "";
        }

其中Characterchar取代

此示例使用Primefaces 6.2.4在Glassfish 4.1上运行。