You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Miguel Almeida <mi...@almeida.at> on 2016/12/01 19:07:33 UTC

Can you use type conversion with JSONInterceptor?

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

Re: Can you use type conversion with JSONInterceptor?

Posted by Lukasz Lenart <lu...@apache.org>.
Super cool, thanks :)

2016-12-02 11:40 GMT+01:00 Miguel Almeida <mi...@almeida.at>:
> Thank you Lukasz!
>
> I will check if/how we can inject the converter there. If we manage to
> get it working in a way that helps the ticket resolution I'll also post
> the information there.
>
>
>
> On Sex, 2016-12-02 at 09:50 +0100, Lukasz Lenart wrote:
>
>> 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

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


Re: Can you use type conversion with JSONInterceptor?

Posted by Miguel Almeida <mi...@almeida.at>.
Thank you Lukasz!

I will check if/how we can inject the converter there. If we manage to
get it working in a way that helps the ticket resolution I'll also post
the information there.



On Sex, 2016-12-02 at 09:50 +0100, Lukasz Lenart wrote:

> 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

Re: Can you use type conversion with JSONInterceptor?

Posted by Lukasz Lenart <lu...@apache.org>.
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 <mi...@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