3

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..

Ontonomo
  • 527
  • 1
  • 8
  • 12

2 Answers2

6

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
3

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 {
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142