How to preserve NULL state in a Boolean sending an empty String from JSP to Action

Andrea Ligios

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.

Aleksandr M

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to read Action attributes from JSP fragments included with <jsp:include />?

来自分类Dev

How to convert String to Boolean in Java, but to treat null differently than false?

来自分类Dev

Slack webhook to allow sending empty string

来自分类Dev

String empty to null custom binding

来自分类Dev

string.Empty可以等于null吗?

来自分类Dev

Preserve empty text tags with pugixml

来自分类Dev

在VBA-excel中将Boolean设置为null / Nothing / empty等,有什么办法吗?

来自分类Dev

Pass parameters from servlet to jsp using HTTPServletRequest return null

来自分类Dev

Javascript真值等效于not undefined,not null和not empty string?

来自分类Dev

在null和string.Empty上使用==或.Equals()

来自分类Dev

杰克逊忽略了ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

来自分类Dev

如何比较流利的断言中的null和string.Empty(或“”)?

来自分类Dev

INotifyDataErrorInfo.GetErrors何时以null与String.empty调用?

来自分类Dev

Mysql如果为Null或Empty String,则显示N / A

来自分类Dev

当我使用数组制作导航栏时,无法为JSP编译类,并且无法从String转换为boolean

来自分类Dev

字符串ss =“”; vs string ss = string.empty; vs string = null; 在C#中

来自分类Dev

Removing Item from List in Scheme leaves empty null list element

来自分类Dev

如何在布尔值中保留NULL状态从JSP发送空字符串到Action

来自分类Dev

如何在布尔值中保留NULL状态(从JSP发送空字符串到Action)

来自分类Dev

如何在Java中将String转换为Boolean,但将null与false区别对待?

来自分类Dev

How to remove � from a String?

来自分类Dev

如何从<jsp:include />附带的JSP片段中读取Action属性?

来自分类Dev

knockoutjs - How can I populate an empty observable array in response to user action (anchor link click)

来自分类Dev

NULL,PHP中的empty()或“”?

来自分类Dev

Struts2-从JSP访问Action变量

来自分类Dev

使用AJAX从JSP页面调用Action类

来自分类Dev

JSP登录getParameter返回null

来自分类Dev

读取一个空的Textbox.Text作为“”,null或String.Empty吗?

来自分类Dev

在Struts操作类中,来自jsp的输入值为null。如何在Action类中访问注册表单输入值

Related 相关文章

  1. 1

    How to read Action attributes from JSP fragments included with <jsp:include />?

  2. 2

    How to convert String to Boolean in Java, but to treat null differently than false?

  3. 3

    Slack webhook to allow sending empty string

  4. 4

    String empty to null custom binding

  5. 5

    string.Empty可以等于null吗?

  6. 6

    Preserve empty text tags with pugixml

  7. 7

    在VBA-excel中将Boolean设置为null / Nothing / empty等,有什么办法吗?

  8. 8

    Pass parameters from servlet to jsp using HTTPServletRequest return null

  9. 9

    Javascript真值等效于not undefined,not null和not empty string?

  10. 10

    在null和string.Empty上使用==或.Equals()

  11. 11

    杰克逊忽略了ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

  12. 12

    如何比较流利的断言中的null和string.Empty(或“”)?

  13. 13

    INotifyDataErrorInfo.GetErrors何时以null与String.empty调用?

  14. 14

    Mysql如果为Null或Empty String,则显示N / A

  15. 15

    当我使用数组制作导航栏时,无法为JSP编译类,并且无法从String转换为boolean

  16. 16

    字符串ss =“”; vs string ss = string.empty; vs string = null; 在C#中

  17. 17

    Removing Item from List in Scheme leaves empty null list element

  18. 18

    如何在布尔值中保留NULL状态从JSP发送空字符串到Action

  19. 19

    如何在布尔值中保留NULL状态(从JSP发送空字符串到Action)

  20. 20

    如何在Java中将String转换为Boolean,但将null与false区别对待?

  21. 21

    How to remove � from a String?

  22. 22

    如何从<jsp:include />附带的JSP片段中读取Action属性?

  23. 23

    knockoutjs - How can I populate an empty observable array in response to user action (anchor link click)

  24. 24

    NULL,PHP中的empty()或“”?

  25. 25

    Struts2-从JSP访问Action变量

  26. 26

    使用AJAX从JSP页面调用Action类

  27. 27

    JSP登录getParameter返回null

  28. 28

    读取一个空的Textbox.Text作为“”,null或String.Empty吗?

  29. 29

    在Struts操作类中,来自jsp的输入值为null。如何在Action类中访问注册表单输入值

热门标签

归档