Struts 2
  1. Struts 2
  2. WW-3584

convertToString in custom StrutsTypeConverter not called for multiple-valued tags (checkboxlist and select)

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.2.1.1
    • Fix Version/s: 2.3.18
    • Component/s: Core Actions
    • Labels:
      None
    • Flags:
      Important

      Description

      I wrote a custom converter to map a list of custom type (List<MyBean>) to a s:checkboxlist tag. When I submit the form the convertFromString() method is called, so I have the list correctly populated with the selected values.

      The problem is that it doesn't work the other way: when I try to pre-populate the form from the values in the list, the tag is always empty, because the convertToString() method is never called. Same happens for s:select with multiple="true".

      I think it is a bug because if I change the tag to s:textfield, mapped to the same list, convertToString() is called and text box is populated.

      1. checkboxlist.ftl
        2 kB
        sebastien b.
      2. checkboxlist.ftl
        2 kB
        Maurizio Cucchiara
      3. multivalueTest.zip
        2 kB
        Antonino Virgillito

        Activity

        Hide
        Lukasz Lenart added a comment -

        I assume it isn't a problem anymore

        Show
        Lukasz Lenart added a comment - I assume it isn't a problem anymore
        Hide
        sebastien b. added a comment - - edited

        The bug is solved for me with the checkboxlist.ftl file corrected.

        I modified only this file to add the css render.

        Thanks !
        Cordially.

        Show
        sebastien b. added a comment - - edited The bug is solved for me with the checkboxlist.ftl file corrected. I modified only this file to add the css render. Thanks ! Cordially.
        Hide
        Maurizio Cucchiara added a comment -

        Also I'm not sure that this problem has already been addressed here

        Show
        Maurizio Cucchiara added a comment - Also I'm not sure that this problem has already been addressed here
        Hide
        Maurizio Cucchiara added a comment - - edited

        Copying the attached file to webapp directory (or even to the classpath) along the /template/simple/ path (according with http://s.apache.org/tmp) would seem to solve the problem.
        Also you have to overwrite the toString method of the Color Class.

            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;
        
                Color color = (Color) o;
        
                return id == color.id;
        
            }
        
        
        Show
        Maurizio Cucchiara added a comment - - edited Copying the attached file to webapp directory (or even to the classpath) along the /template/simple/ path (according with http://s.apache.org/tmp ) would seem to solve the problem. Also you have to overwrite the toString method of the Color Class. @Override public boolean equals( Object o) { if ( this == o) return true ; if (o == null || getClass() != o.getClass()) return false ; Color color = (Color) o; return id == color.id; }
        Hide
        Maurizio Cucchiara added a comment -

        I removed the link to XW-297, which I suppose is related with the behavior depicted by Alfredo.

        Show
        Maurizio Cucchiara added a comment - I removed the link to XW-297 , which I suppose is related with the behavior depicted by Alfredo.
        Hide
        Christian Grobmeier added a comment -

        I am not sure if I am running in the same bug.

        I have two fields in my action. One is filled with a checkbox value, one not.
        Now I wrote two custom type converters. The one for the int value (non-checkbox) is working,
        the one for the boolean value (checkbox) is not working.

        Struts did reckognize both of my Converters, as I could see in the log file. But just the first one is actually executed, even when the second one seems to have an object:

        2011-06-02 19:12:37,962 DEBUG [CommonsLogger.java:57] : taskId:de.UnsignedIntegerConverter@7057323f
        2011-06-02 19:12:37,962 DEBUG [CommonsLogger.java:57] : endOfDay:de.CheckboxToBooleanConverter@771b821f

        MyAction-conversion.properties:

        taskId = de.UnsignedIntegerConverter
        endOfDay = de.CheckboxToBooleanConverter

        taskId works as expected.

        I even have tried this:

        @TypeConversion(converter = "de.CheckboxToBooleanConverter")
        public void setEndOFDay(boolean endOFDay) {

        But still no execution.

        Show
        Christian Grobmeier added a comment - I am not sure if I am running in the same bug. I have two fields in my action. One is filled with a checkbox value, one not. Now I wrote two custom type converters. The one for the int value (non-checkbox) is working, the one for the boolean value (checkbox) is not working. Struts did reckognize both of my Converters, as I could see in the log file. But just the first one is actually executed, even when the second one seems to have an object: 2011-06-02 19:12:37,962 DEBUG [CommonsLogger.java:57] : taskId:de.UnsignedIntegerConverter@7057323f 2011-06-02 19:12:37,962 DEBUG [CommonsLogger.java:57] : endOfDay:de.CheckboxToBooleanConverter@771b821f MyAction-conversion.properties: taskId = de.UnsignedIntegerConverter endOfDay = de.CheckboxToBooleanConverter taskId works as expected. I even have tried this: @TypeConversion(converter = "de.CheckboxToBooleanConverter") public void setEndOFDay(boolean endOFDay) { But still no execution.
        Hide
        Maurizio Cucchiara added a comment -

        Thanks Antonino,
        I'll take a look at the attachment you sent ASAP

        Show
        Maurizio Cucchiara added a comment - Thanks Antonino, I'll take a look at the attachment you sent ASAP
        Hide
        Antonino Virgillito added a comment -

        Maurizio, I attached the zip again. I have checked and now it should be fine.

        Show
        Antonino Virgillito added a comment - Maurizio, I attached the zip again. I have checked and now it should be fine.
        Hide
        Antonino Virgillito added a comment -

        Here are the test files showing the issue.

        The zip contains:

        • bean class
        • action class
        • conversion class (with configuration file)
        • JSP file (form and checkboxlist)
        • struts configuration

        Some annotations:

        • The conversion class containts prints for tracing the methods execution
        • The JSP contains a commented textfield tag having the same name as the checkboxlist. When uncommenting the tag convertToString is called.
        • If you change the name to colorStringValue the checkbox is populated (colorStringValue is a list of strings)
        Show
        Antonino Virgillito added a comment - Here are the test files showing the issue. The zip contains: bean class action class conversion class (with configuration file) JSP file (form and checkboxlist) struts configuration Some annotations: The conversion class containts prints for tracing the methods execution The JSP contains a commented textfield tag having the same name as the checkboxlist. When uncommenting the tag convertToString is called. If you change the name to colorStringValue the checkbox is populated (colorStringValue is a list of strings)
        Hide
        Maurizio Cucchiara added a comment -

        Don't worry, I have to admit that the attached zip maked me a little frustrated

        Show
        Maurizio Cucchiara added a comment - Don't worry, I have to admit that the attached zip maked me a little frustrated
        Hide
        Antonino Virgillito added a comment -

        Yes, it's broken.
        I apologize.

        Unfortunately I won't be able to recover the files until Monday as they are on my office pc.

        Show
        Antonino Virgillito added a comment - Yes, it's broken. I apologize. Unfortunately I won't be able to recover the files until Monday as they are on my office pc.
        Hide
        Maurizio Cucchiara added a comment -

        Hi Antonio,
        I don't know what happens to the attached zip, but I cannot open it neither with my ubuntu or my android.

        Show
        Maurizio Cucchiara added a comment - Hi Antonio, I don't know what happens to the attached zip, but I cannot open it neither with my ubuntu or my android.
        Hide
        Antonino Virgillito added a comment - - edited

        Here are the test files showing the issue.

        The zip contains:

        • bean class
        • action class
        • conversion class (with configuration file)
        • JSP file (form and checkboxlist)
        • struts configuration

        Some annotations:

        • The conversion class containts prints for tracing the methods execution
        • The JSP contains a commented textfield tag having the same name as the checkboxlist. When uncommenting the tag convertToString is called.
        • If you change the name of checkboxlist to colorStringValue the checkbox is populated (colorStringValue is a list of strings)
        Show
        Antonino Virgillito added a comment - - edited Here are the test files showing the issue. The zip contains: bean class action class conversion class (with configuration file) JSP file (form and checkboxlist) struts configuration Some annotations: The conversion class containts prints for tracing the methods execution The JSP contains a commented textfield tag having the same name as the checkboxlist. When uncommenting the tag convertToString is called. If you change the name of checkboxlist to colorStringValue the checkbox is populated (colorStringValue is a list of strings)
        Hide
        Maurizio Cucchiara added a comment -

        According with what Antonino said, the tag textfield works as expected, so I deduce that you're talking about some different behaviour.

        Show
        Maurizio Cucchiara added a comment - According with what Antonino said, the tag textfield works as expected, so I deduce that you're talking about some different behaviour.
        Hide
        Alfredo Osorio added a comment -

        It's also happening to me. I think the problem has something to do with:
        XWorkConverter
        public Object convertValue(Map<String, Object> context, Object target, Member member, String property, Object value, Class toClass);

        and the way it finds the converter. For example giving the following:

        myPage.jsp
        <s:form>
        <s:textfield name="a.b" label="myField"/>
        </s:form>

        MyAction-conversion.properties
        a.b=com.afirme.autosoffline.converter.PercentDecimalConverter

        And a corresponding property in my Action.

        class MyAction

        { A getA() // }

        class A

        { BigDecimal b; }

        Just before convertFromString it's called the next line evaluates to:
        Object lastPropertyPath = context.get(ReflectionContextState.CURRENT_PROPERTY_PATH);

        "a" (it evaluates fine)

        But when it is the case for convertToString that evaluates to:

        "a.b" (wrong)

        So when the following expression evaluates:
        String path = lastPropertyPath + "." + property;
        It is resolved to:
        "a.b.b"

        Evaluating to a wrong path for the corresponding property so no TypeConverter would be found.

        Version tested: 2.2.1.1

        Show
        Alfredo Osorio added a comment - It's also happening to me. I think the problem has something to do with: XWorkConverter public Object convertValue(Map<String, Object> context, Object target, Member member, String property, Object value, Class toClass); and the way it finds the converter. For example giving the following: myPage.jsp <s:form> <s:textfield name="a.b" label="myField"/> </s:form> MyAction-conversion.properties a.b=com.afirme.autosoffline.converter.PercentDecimalConverter And a corresponding property in my Action. class MyAction { A getA() // } class A { BigDecimal b; } Just before convertFromString it's called the next line evaluates to: Object lastPropertyPath = context.get(ReflectionContextState.CURRENT_PROPERTY_PATH); "a" (it evaluates fine) But when it is the case for convertToString that evaluates to: "a.b" (wrong) So when the following expression evaluates: String path = lastPropertyPath + "." + property; It is resolved to: "a.b.b" Evaluating to a wrong path for the corresponding property so no TypeConverter would be found. Version tested: 2.2.1.1
        Hide
        Maurizio Cucchiara added a comment -

        Ciao Antonino,
        could you please post the following elements:
        1. the -conversion.properties.
        2. the action's configuration inside your struts.xml (I'm assuming you're using it).
        3. the tag directive inside your jsp file.

        Show
        Maurizio Cucchiara added a comment - Ciao Antonino, could you please post the following elements: 1. the -conversion.properties. 2. the action's configuration inside your struts.xml (I'm assuming you're using it). 3. the tag directive inside your jsp file.

          People

          • Assignee:
            Maurizio Cucchiara
            Reporter:
            Antonino Virgillito
          • Votes:
            3 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:

              Development