Domanda

I have a criterion in a search page that is a boolean. Since it is not mandatory, I can't use a checkbox, because I may want to skip it;

JSP

<s:select list = '#{ "":"Make your choice...", true:"FOO", false:"BAR" }'
          name = "myBooleanCriterion" />

Action

private Boolean myBooleanCriterion;

/* Getter and Setter */

We know that boolean defaults to false, while Boolean defaults to null, then the first time the page is rendered, it is ok ("Make your choice" is displayed).

After the POST, however, Struts instantiates the Boolean property (defaulting it to false) even when it finds "" as value, so when I come back to the JSP, the value is preselected to false ("BAR").

How can I instruct Struts to not create an instance of that variable if the value is "", leaving it to its original, null state ? Do I need to create a Converter, or to specify something in the .properties file ? (I've basically the same code as in example .2 from this answer)

I'm using (a slightly custom) paramsPrepareParamsStack Interceptor Stack.

I feel like if I'm missing something stupid.

È stato utile?

Soluzione

Because the XWorkBasicConverter converts boolean-s exactly the same way it converts Boolean-s, by using Boolean.valueOf() method, the empty string or null will be converted to false.

In order to have null after submit you need to create a custom converter for Boolean or don't submit this parameter at all. (E.g. Change name attribute with javascript if selected value is "").

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top