Custom Struts type converter is not working

Daler

I do have a getter/setter for the Date variable, like so:

private Date registrationDate;

@TypeConversion(converter = "org.com.helper.DataHelper")
public Date getRegistrationDate() {
    return registrationDate;
}

@TypeConversion(converter = "org.com.helper.DataHelper")
public void setRegistrationDate(Date registrationDate) {
    this.registrationDate = registrationDate;
}

As you can see I've created a custom struts converter to convert the incoming string to the Date format and then assign it. But it doesn't seem to work. Here is the code for DateHelper:

public class DateHelper extends StrutsTypeConverter {

    private static final DateFormat FORMAT = new SimpleDateFormat("dd-MM-yyyy");

    @Override
    public Object convertFromString(Map arg0, String[] values, Class arg2) {
           try {
               System.out.println(values[0]+"called from datahelper");

                return FORMAT.parse(values[0]);
            } catch (Exception e) {
                throw new TypeConversionException(e.getMessage());
            }
    }

    @Override
    public String convertToString(Map arg0, Object value) {
        try {
            return FORMAT.format(value);
        } catch (Exception e) {
            throw new TypeConversionException(e.getMessage());
        }
    }

}

I use struts2-json plugin to get and parse the form data. This plugin does assign automatically all string values but I do have an issue with the Date.

This is how I get the data passed to Java from the form.

{"data":{"recordId":"123","registrationDate":"20-07-2016","hisId":"","herId":"","lastNameHe":"Asd","firstNameHe":"Asd","middleNameHe":"Asd","workPlaceHe":"","educationHe"}}

So, according to my understanding the code before starting to set the registrationDate should call the helper class and convert the string to date and then call the registrationDate setter.. but it doesn't seem to work.. I've even put a log call in the helper code but it doesn't show up in the eclipse.

Aleksandr M

Seems like struts2-json-plugin doesn't use default type conversions. :(

For setting date format you can use @JSON annotation which has format property.

@JSON(format = "dd.MM.yyyy")
public void setRegistrationDate(Date registrationDate) {
    this.registrationDate = registrationDate;
}

JSON plugin documentation - Customizing Serialization and Deserialization.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Custom Struts type converter is not working

From Dev

Struts 2 type converter issue

From Dev

Custom WinForms data binding with converter not working on nullable type (double?)

From Dev

Custom 404 error page not working with Struts 2

From Dev

String to String Type Conversion in Struts 2 not working?

From Dev

Spring MVC Custom Converter for JSON data not working

From Dev

C# - type converter with custom date format

From Dev

AutoMapper - how to use custom value resolver inside custom type converter

From Dev

LINQ Grouping by custom type not working

From Dev

Struts2 action not working with result type both json and dispatcher

From Dev

How do I build a (working) custom converter with opencsv

From Dev

Custom HTTP Message Converter Not Being Used, 415 Unsupproted Media Type

From Dev

Automapper inject dependencies into custom type converter using Simple injector (Ioc)

From Dev

AutoMapper null source value and custom type converter, fails to map?

From Dev

Automapper inject dependencies into custom type converter using Simple injector (Ioc)

From Dev

AutoMapper Custom type converter ITypeConverter and mapping nested objects

From Dev

Symfony custom doctrine datetime type not working

From Dev

Custom Post Type - Category Add Button Not Working

From Dev

NLog xsi:type not working with custom target

From Dev

Wordpress: Pagination not working with custom post type

From Dev

wp query not working with custom post type > category

From Dev

Custom Post Type - Category Add Button Not Working

From Dev

Taxonomy template not working for custom post type

From Dev

Not working custom domain - A record type azure

From Dev

WordPress custom post type array not working as expcted

From Dev

Xamarin custom MarkupExtension not working with Type property

From Dev

Custom wordpress taxonomies capabilities not working with custom post type

From Dev

Using Generic.List with custom type as a return type for function is not working

From Dev

Struts setSesssion() is not working

Related Related

  1. 1

    Custom Struts type converter is not working

  2. 2

    Struts 2 type converter issue

  3. 3

    Custom WinForms data binding with converter not working on nullable type (double?)

  4. 4

    Custom 404 error page not working with Struts 2

  5. 5

    String to String Type Conversion in Struts 2 not working?

  6. 6

    Spring MVC Custom Converter for JSON data not working

  7. 7

    C# - type converter with custom date format

  8. 8

    AutoMapper - how to use custom value resolver inside custom type converter

  9. 9

    LINQ Grouping by custom type not working

  10. 10

    Struts2 action not working with result type both json and dispatcher

  11. 11

    How do I build a (working) custom converter with opencsv

  12. 12

    Custom HTTP Message Converter Not Being Used, 415 Unsupproted Media Type

  13. 13

    Automapper inject dependencies into custom type converter using Simple injector (Ioc)

  14. 14

    AutoMapper null source value and custom type converter, fails to map?

  15. 15

    Automapper inject dependencies into custom type converter using Simple injector (Ioc)

  16. 16

    AutoMapper Custom type converter ITypeConverter and mapping nested objects

  17. 17

    Symfony custom doctrine datetime type not working

  18. 18

    Custom Post Type - Category Add Button Not Working

  19. 19

    NLog xsi:type not working with custom target

  20. 20

    Wordpress: Pagination not working with custom post type

  21. 21

    wp query not working with custom post type > category

  22. 22

    Custom Post Type - Category Add Button Not Working

  23. 23

    Taxonomy template not working for custom post type

  24. 24

    Not working custom domain - A record type azure

  25. 25

    WordPress custom post type array not working as expcted

  26. 26

    Xamarin custom MarkupExtension not working with Type property

  27. 27

    Custom wordpress taxonomies capabilities not working with custom post type

  28. 28

    Using Generic.List with custom type as a return type for function is not working

  29. 29

    Struts setSesssion() is not working

HotTag

Archive