I have struts2-json-plugin lib in my project and I want to use JSONWriter to get JSON string and put it to JavaScript for one parameter only (not JSON result for the all Action) only one getter:
public String getCarTypesJson() throws JSONException {
JSONWriter writer = new JSONWriter();
return writer.write(carTypes);
}
Its work but can I skip creation of new JSONWriter()
and inject JSONWriter
object in my Action from Struts2?
The Struts2 json plugin doesn't declare JSONWriter
as an injectable bean. And inside the plugin JSONWriter
is used exactly as you do it right now, by creating an instance of it each time there is a need to serialize something.
Of course you can create bean by yourself in some DI container and inject it into action. If you decide to do so make sure you declare the JSONWriter
bean as prototype
.