Problem binding to a converter

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem binding to a converter

Gerry King
In a Spring (3.2.1) Hibernate (4.1.1) Thymeleaf (2.0.16) project I have a 'submission' entity that has a property 'journal'.

In the form to create new submissions users have to select a journal from a drop down list. The value of the select option is the id (String) of the journal. I have created a converter that should convert the id String into a Journal object.

conversionService.canConvert(String.class,Journal.class) returns true so the converter is registered and yet I get  a null and an error in the results object:

Cannot convert value of type [java.lang.String] to required type [com.example.entity.Journal] for property 'journal':  no matching editors or conversion strategy found]


Any idea where I am going wrong?

In the controller I have a request mapping:

 @RequestMapping(value = "/newsubmission", method = RequestMethod.POST)
    public ModelAndView newSubmission( @ModelAttribute("submission") Submission submission, BindingResult result) throws Exception {...}

and to populate the dropdown:

@ModelAttribute("journalList")
    public List<Journal> populateJournals() {
    return journalService.getAllJournals();
    }
 
My template looks like this:

<form id="subform" th:action="@{/newsubmission}" th:object="${submission}" method="post">

Manuscript Title

<p >Enter the title.</p>
<input type="text"  id="title" name="title"/>
                                                       

Submitting to

<select name='journal' th:field="*{journal}">
        <option th:each="jrnl : ${journalList}"
                    th:value="${jrnl.id}"
                    th:text="${jrnl.name}">Journal Name</option>
</select>

Gerry
Reply | Threaded
Open this post in threaded view
|

Re: Problem binding to a converter

Zemi
Administrator
Your code seems OK to me.

Have you check that the id is correctly sent in the request? (with firebug or something similar)

If it is correct, I'd look again for a configuration issue, or try to map something simpler than a select, like an input hidden field.

Reply | Threaded
Open this post in threaded view
|

Re: Problem binding to a converter

Gerry King
>ry to map something simpler than a select, like an input hidden field

That was a good suggestion... and yet the converter does not get invoked. I set a breakpoint in the converter which does not get triggered. Meanwhile  the test in the controller says the conversionService.canConvert(String.class,Journal.class)

Here's the error in the results

[Field error in object 'submission' on field 'journal': rejected value [ijo];
codes [typeMismatch.submission.journal,typeMismatch.journal,typeMismatch.com.sp.journalweb.entity.Journal,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable:
codes [submission.journal,journal]; arguments []; default message [journal]];
default message [Failed to convert property value of type 'java.lang.String' to required type 'com.sp.journalweb.entity.Journal' for property 'journal';
nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.sp.journalweb.entity.Journal] for property 'journal': no matching editors or conversion strategy found]]

It looks like a Spring issue so I'll try asking there/SO unless someone can spot what I am doing wrong.

cheers,


Gerry