我正在尝试将值传递给JSF/SEAM中的转换类
public class ValueConverter implements Converter { public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) { if (StringUtils.isNotBlank(value)) { // logic etc here.我的xhtml是:
<f:converter converterId="ValueConverter"> <f:attribute name="theMaxOrderSize" id="maxorder" value="#{_cartItem.item.maxOrderSize}"/> </f:converter>如何在Java中将参数值传递给Conversion类?我是从错误开始吗?我认为我正在使用JSF 1.2.
解决方案Bhesh是完全正确的.您应该在 Validator 中进行验证工作. /p>
对于具体问题,将<f:attribute>从<f:converter>中移出(如果您正在听我们的话,则从<f:validator>移至)输入组件,然后使用 UIComponent#getAttributes() 即可获取.例如
<h:inputText ...> <f:validator validatorId="valueValidator" /> <f:attribute name="theMaxOrderSize" id="maxorder" value="#{_cartItem.item.maxOrderSize}"/> </h:inputText>使用
Object theMaxOrderSize = component.getAttributes().get("theMaxOrderSize"); // ...(其中component是validate()方法的UIComponent自变量,它表示父输入组件)
您可以将其强制转换为Integer或#{_cartItem.item.maxOrderSize}表示的任何对象类型.
I am trying to pass a value to a conversion class in JSF/SEAM
public class ValueConverter implements Converter { public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) { if (StringUtils.isNotBlank(value)) { // logic etc here.My xhtml is:
<f:converter converterId="ValueConverter"> <f:attribute name="theMaxOrderSize" id="maxorder" value="#{_cartItem.item.maxOrderSize}"/> </f:converter>How do I pass a parameter value to a Conversion class in java? Am I starting off wrong? I am using JSF 1.2 I think..
解决方案Bhesh is entirely right. You should be doing the validating job inside a Validator.
As to the concrete problem, move the <f:attribute> out of the <f:converter> (or <f:validator> if you're listening to us) into the input component and then use UIComponent#getAttributes() to obtain it. E.g.
<h:inputText ...> <f:validator validatorId="valueValidator" /> <f:attribute name="theMaxOrderSize" id="maxorder" value="#{_cartItem.item.maxOrderSize}"/> </h:inputText>with
Object theMaxOrderSize = component.getAttributes().get("theMaxOrderSize"); // ...(where component is the UIComponent argument of the validate() method, it represents the parent input component)
You can cast it to Integer or whatever object type the #{_cartItem.item.maxOrderSize} represents.
更多推荐
如何在Java中将参数值传递给Conversion类?
发布评论