Spring: Convert String from View to a Calendar Object

Alex

How can I convert a string from a form input (easyui-datetimebox, in case) to a Calendar property in an object in Controller, autobinded by Spring?

I've read http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html but I couldn't find anything nearly right-to-the-point there.

JSP:

<input id="DeadLineDate"
  class="easyui-datetimebox" 
  name="DeadLineDate"
  value="${SessionDeadLineDate}"
  data-options="formatter:myformatter,
                parser:myparser
/>

When submited, Spring validation throws an error:

Failed to convert property value of type java.lang.String to required type java.util.Calendar for property DeadLineDate; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Calendar] for property DeadLineDate: no matching editors or conversion strategy found.

PS: Spring 3

Edit: adding controller's method to perform operation:

@Controller
@RequestMapping("/project/MaintainProjectFrm")
@SessionAttributes({"project","SessionDeadLineDate"})
public class MaintainProjectController {

    /* ... many methods... */

    @RequestMapping(params = "update", method = RequestMethod.POST, produces={"text/plain; charset=UTF-8"})
    public String update(@ModelAttribute("project") Project project, 
                            BindingResult result, 
                                    SessionStatus status, 
                                        ModelMap model,
                                            HttpServletRequest req,
                                                HttpServletResponse resp) throws IOException {

        projectValidator.validate(project, result);

        if (result.hasErrors()) {
             //has errors, in this case, that one shown in text above, which is rendered again in view (JSP)
            return "/project/MaintainProjectFrm";
        } else {

            try{
                mpService.updateProject(project);
            }
            catch(Exception e){
                resp.setStatus(500);
                resp.getWriter().write("Error updating project: " + e.getMessage());
                return "/project/MaintainProjectFrm";
            }

            status.setComplete();

        }
    }

    /* ... yet other methods ... */
}
Sotirios Delimanolis

I'm assuming your Project class has the field DeadLineDate (fields should start with a lowercase character).

Annotate it with @DateTimeFormat like so

@DateTimeFormat(pattern = "yyyy/MM/dd") // or whatever pattern you want
private Calendar DeadLineDate;

Your client will then need to send the appropriate pattern.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trying to get the date as a string from a Calendar object

From Dev

cannot convert from 'object' to 'string'

From Dev

Cannot convert from object to string

From Dev

Convert java Calendar object from local time to UTC

From Dev

Parsing String into a Calendar object

From Dev

Convert Java Gregorian Calendar to String

From Dev

convert from Map<Object,Set<Object>> to Map<String,Set<String>>

From Dev

how to convert string build from variables to an object

From Dev

Remove datetetime object from variable and convert to string

From Dev

Spring Mongo convert to document from json string

From Dev

Spring Mongo convert to document from json string

From Dev

DatePicker view calendar from textbox

From Dev

convert json string object to integer in ng-view

From Dev

I wanted to convert this string into calendar object of the specific date, but all it does is giving me the current date

From Dev

Jackson convert from json string to Java object results in default object

From Dev

Spring XD: pipe (>) from file source to batch job fails (IllegalArgumentException: Unable to convert provided JSON to Map<String, Object>)

From Dev

Spring XD: pipe (>) from file source to batch job fails (IllegalArgumentException: Unable to convert provided JSON to Map<String, Object>)

From Dev

how to convert calendar object to a specific time zone

From Dev

Django view: Convert unicode retrieved from db to string

From Dev

How to convert Gregorian string to Gregorian Calendar?

From Dev

How to convert Gregorian string to Gregorian Calendar?

From Dev

convert date into string in json bootstrap calendar

From Dev

Convert date locale in google sheet from Gregorian calendar to Jalali calendar

From Dev

Evaluate string in dot notation to get the corresponding value from an object in the view

From Dev

Get String from the ArrayList of an object for AutoComplete Text View

From Dev

Disable the create option from calendar view ODOO

From Dev

How to convert from a Hybris enumtype to a Java Calendar?

From Dev

angularjs - javascript - Convert object values from array to string

From Dev

How to convert a string to a JSON and then extract required values from the JSON object?

Related Related

  1. 1

    Trying to get the date as a string from a Calendar object

  2. 2

    cannot convert from 'object' to 'string'

  3. 3

    Cannot convert from object to string

  4. 4

    Convert java Calendar object from local time to UTC

  5. 5

    Parsing String into a Calendar object

  6. 6

    Convert Java Gregorian Calendar to String

  7. 7

    convert from Map<Object,Set<Object>> to Map<String,Set<String>>

  8. 8

    how to convert string build from variables to an object

  9. 9

    Remove datetetime object from variable and convert to string

  10. 10

    Spring Mongo convert to document from json string

  11. 11

    Spring Mongo convert to document from json string

  12. 12

    DatePicker view calendar from textbox

  13. 13

    convert json string object to integer in ng-view

  14. 14

    I wanted to convert this string into calendar object of the specific date, but all it does is giving me the current date

  15. 15

    Jackson convert from json string to Java object results in default object

  16. 16

    Spring XD: pipe (>) from file source to batch job fails (IllegalArgumentException: Unable to convert provided JSON to Map<String, Object>)

  17. 17

    Spring XD: pipe (>) from file source to batch job fails (IllegalArgumentException: Unable to convert provided JSON to Map<String, Object>)

  18. 18

    how to convert calendar object to a specific time zone

  19. 19

    Django view: Convert unicode retrieved from db to string

  20. 20

    How to convert Gregorian string to Gregorian Calendar?

  21. 21

    How to convert Gregorian string to Gregorian Calendar?

  22. 22

    convert date into string in json bootstrap calendar

  23. 23

    Convert date locale in google sheet from Gregorian calendar to Jalali calendar

  24. 24

    Evaluate string in dot notation to get the corresponding value from an object in the view

  25. 25

    Get String from the ArrayList of an object for AutoComplete Text View

  26. 26

    Disable the create option from calendar view ODOO

  27. 27

    How to convert from a Hybris enumtype to a Java Calendar?

  28. 28

    angularjs - javascript - Convert object values from array to string

  29. 29

    How to convert a string to a JSON and then extract required values from the JSON object?

HotTag

Archive