struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lukasz Lenart <lukaszlen...@apache.org>
Subject Re: Can you use type conversion with JSONInterceptor?
Date Fri, 02 Dec 2016 08:50:32 GMT
Hi,

JSON plugin uses its own conversation mechanism, check JSONPopulator
but I think it should be possible to inject XWorkConverter and use it
to convert objects - this requires implementing your own
JSONPopulator.

There is a task to do it so it should happen in Struts 2.5 ;-)
https://issues.apache.org/jira/browse/WW-3364


Regards
-- 
Ɓukasz
+ 48 606 323 122 http://www.lenart.org.pl/

2016-12-01 20:07 GMT+01:00 Miguel Almeida <miguel@almeida.at>:
> Imagine SearchAction{
>
> private List<Record> evaluatedRecords;
> }
>
> Record is an interface so to get auto-wiring to work you'd usually
> set-up a
>
> SearchAction-conversion.properties:
> KeyProperty_evaluatedRecords=id
> Element_evaluatedRecords=com.bibliovigilance.model.RecordImpl
> CreateIfNull_evaluatedRecords=true
>
>
> I now want to create an Ajax post on this action. I tried the following:
>
>                             var evaluatedRecords = [
>                                              {"id": "10672"}
>                                              ]
>
>                      var json_parameters = {evaluatedRecords:
> evaluatedRecords};
>
>                         $.ajax({
>                           url: 'SearchmarkSelectedArticlesJSON.action',
>                           cache: false,
>                           contentType: 'application/json',
>                           data: JSON.stringify(json_parameters),
>                           type: "POST"
>                         });
>
> I believe I need to set-up the JSON interceptor on my action, so I
> added:
> <action name="SearchmarkSelectedArticlesJSON" class="searchAction"
> method="markSelectedArticles">
>         <interceptor-ref name="json"/>
> </action>
>
> The problem is that this interceptor doesn't seem to be using the
> -conversion.properties, so it throws an error when it tries to
> instantiate the Record class:
>
> java.lang.InstantiationException: com.bibliovigilance.model.Record
>         at java.lang.Class.newInstance(Class.java:368)
>         at
> org.apache.struts2.json.JSONPopulator.convertToCollection(JSONPopulator.java:250)
>
>
> I also tried removing the Record type in the List ( List
> evaluatedRecords). But in this case the JSON deserialization of the
> above mentioned ajax call will create a List evaluatedRecords with 1
> element, but that element is a Map (I didn't investigate, but I suppose
> it'll have id in the key and 10672 in the value).
>
> How would we configure this correctly? Is it possible for the JSON
> interceptor to be aware of the -conversion.properties? If not, what
> alternatives do you envision?
>
> Thanks!
>
> Miguel

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message