Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Convert Boolean True/False to Yes/No

  1. #11
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    @jkrfs:
    I think you should handle the null case yourself in the converter:

    Code:
    public BooleanToStringConverter implements Converter<Boolean, String> {
        public String convert(Boolean bool) {
            if (bool != null)
                return bool ? "Yes" : "No";
            else
                return ""; //or whatever you want
        }
    }
    give it a try.

    @Keith:
    I voted the Jira issue, thanks for taking my advice into consideration. I really appreciate it.

    Enrico

  2. #12
    Join Date
    Aug 2010
    Posts
    24

    Default

    By the way, I tried another way which miraculously worked. I wrapped all booleans with

    Code:
    <spring: message code="${myObject.theBoolean}" />
    And replaced true/false in my message.properties to Yes/No, and it displayed. However, when there was a null value, again the page did not complete loading and got an error. I did add

    Code:
    =
    alone in the message.properties file, and again the page loaded fine. I'm not sure if this is the most elegant solution, but at least its working...right?

  3. #13
    Join Date
    Aug 2010
    Posts
    24

    Default

    @Enrico,

    I tried checking for a null value in the BooleanToStringConverter yesterday but the page would still load with the same error that I previously posted.

  4. #14
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Converters are null safe, you never have to worry about handling nulls there. (see the JavaDocs for the Converter and Formatter SPI noting this)

    In any case, this was a known bug in 3.0.x that should be fixed in 3.0.5--I can't remember which maintenance release exactly it was fixed in but I remember seeing it.

    Here's the JIRA I was thinking of. Not exactly the same thing, but related: https://jira.springframework.org/browse/SPR-7459

    For clarity, can you paste in the snippet of your spring:eval usage from your JSP and the full stacktrace you're getting again when the value is null. If you've confirmed you're indeed running 3.0.5 and still getting this, please definitely do open a JIRA at jira.springframework.org reporting the bug.

    Keith
    Last edited by Keith Donald; Nov 11th, 2010 at 01:18 PM.
    Keith Donald
    Core Spring Development Team

  5. #15
    Join Date
    Aug 2010
    Posts
    24

    Default

    Snippet
    Code:
    <td><spring:message  code="label.financialStatementsAssembled"/>:</td>
    <td><spring:eval expression="${serviceAgreement.financialStatementsAssembled}" /></td>
    Unfortunately, I dont have the full stack trace anymore, I 'fixed' it the issue with what I did in the post above and had to move on. I can set everything back again and try it again tonight.

  6. #16
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Alright, lets move this to a JIRA issue and see if we can get it resolved.

    Keith
    Keith Donald
    Core Spring Development Team

  7. #17
    Join Date
    Aug 2010
    Posts
    24

    Default

    Keith,

    Is there anything else you need from me? I can try to recreate the error if necessary or post in JIRA.

    As of now, we're sticking with using spring:message and the message.properties file. We are forced to test each field with

    Code:
    <c:if test="${myObject.theBoolean != null}">
    Which, clearly, is very tedious and will become a hassle to maintain. We're most likely going back to the converter soon but this works for now.

    Thanks

  8. #18
    Join Date
    Apr 2011
    Posts
    28

    Default

    Hi,

    Sorry for reopening old thread.
    I'm having problems with boolean to String conversion.

    In a Spring ROO Project, I've created new converter

    Code:
        static class BoolConverter implements Converter<Boolean, String>  {
            public String convert(Boolean bool) {
            	if(bool==true) return "Yes";
            	else return "No";
            }        
        }
    and registered it

    Code:
    registry.addConverter(new BoolConverter());
    But It's not working. Other converters are working just fine.

    Any ideas?

    Is there difference between boolean primitive and Boolean for the Converter?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •