How do I pass a parameter value to a Conversion class in java?

后端 未结 2 1117
孤城傲影
孤城傲影 2020-12-17 05:37

I am trying to pass a value to a conversion class in JSF/SEAM

public class ValueConverter implements Converter {

public Object getAsObject(FacesContext arg0         


        
相关标签:
2条回答
  • 2020-12-17 06:15

    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.

    0 讨论(0)
  • 2020-12-17 06:15

    That's something you should be doing with Validator. Converter is just to convert from String to Object and Object to String. You are trying to validate in a Converter.

    How do I pass a parameter value to a Conversion class in java?

    That's not correct, you don't need to pass a parameter to a Converter. it should be -

    How do I access a parameter in a Converter in JSF?

    You can use FacesContext -

    context.getExternalContext().getRequestParameterMap();
    

    I think you got a whole lot of readings to do. Best of luck!

    If you want to add attributes to your converter, then use StateHolder -

    public class ValueConverter implements Converter, StateHolder {
    
    0 讨论(0)
提交回复
热议问题