Home >Java >javaTutorial >How to Customize Struts2 JSON Plugin Serialization for Specific Objects?
The Struts2 JSON Plugin operates by serializing the entire action into JSON, excluding transient properties and those without getters.
To serialize only a specific object, you can leverage the "root" attribute in struts.xml:
<result type="json"> <param name="root"> objectToBeSerialized </param> </result>
Given a data structure with multiple rows represented as "[col1, col2]", you can create:
Value Object (MyRow.java):
public class MyRow implements Serializable { private String col1; private String col2; // Getters and setters omitted for brevity }
Action class (PartAction.java):
public class PartAction implements Serializable { private List<MyRow> rows; public List<MyRow> getRows() { return rows; } public String finder() { rows = new ArrayList<>(); // Loop through search results and populate rows return Action.SUCCESS; } }
Struts.xml:
<package name="default" namespace="/ajax" extends="json-default"> <action name="finder" class="action.Part" method="finder"> <result type="json"> <param name="root">rows</param> </result> </action> </package>
AJAX Callback Function:
var handledata = function(data) { $.each(data, function(index) { alert(data[index].col1); alert(data[index].col2); }); }
The above is the detailed content of How to Customize Struts2 JSON Plugin Serialization for Specific Objects?. For more information, please follow other related articles on the PHP Chinese website!