我想直接从命令按钮调用设置器并传递一个值。我的问题是,如果将其作为字符串传回,则设置器需要一个字符和 jsf。有没有一种好方法可以在前端“修复”这个问题,而不必在我的支持 bean 上超载 setter?
命令按钮:
<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: [email protected](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: [email protected](java.lang.String): javax.faces.event.AbortProcessingException: Method not found: data.o[email protected](java.lang.String)
不幸的是,这是设计使然。引号中的所有内容在 EL 中都被视为String
。解决方法是通过String#charAt()
反而。
#{manageItemHandler.dataEntryOp.setBomComplete('Y'.charAt(0))}
这只是丑陋的。另一种方法是通过它int
相反,代码点是89
for Y
.
#{manageItemHandler.dataEntryOp.setBomComplete(89)}
但这并不完全是自我记录的。更好的是只使用枚举。
public enum Choice {
Y, N;
}
with
protected Choice bomComplete;
您可以按照所需的方式调用它
#{manageItemHandler.dataEntryOp.setBomComplete('Y')}
字符串'Y'
将自动转换为该枚举。作为奖励,枚举还有更多额外的优点,例如编译时类型安全性。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)