Struts method for returning data in json format from an object

  • 2020-04-01 01:40:38
  • OfStack

You want to return json data for an object in struts, by definition


<action name="getUserByName" class="com.action.UserManagerAction" method="getUserByName">
<result name="success" type="json">
userInfo
</result>
</action>

The result returns json data for all the member variables of the entire action class. To return data from the userInfo object, you need to define the return parameter as a root object


<action name="getUserByName" class="com.action.UserManagerAction" method="getUserByName">
<result name="success" type="json">
<param name="root">
userInfo
</param>
</result>
</action>

The following is the definition that returns the various json data


<!--  Encapsulate all get Opening method  --> 
<result type="json" name="user"> 
</result> 

<!--  Contains only user.id attribute  --> 
<result type="json" name="user"> 
<param name="includeProperties"> 
user.id 
</param> 
</result> 

<!--  return user the JSON List List, where userInfosList is action One of the List Properties of type, userInfosList [d+]. userName Said, userInfosList Object stored in 0..end the userName Properties ( list The object stored in must have userName Attributes)  
--> 
<result name="success" type="json"> 
<param name="includeProperties"> 
userInfosList[d+].userName,userInfosList[d+].password 
</param> 
</result> 

<!--  Does not contain user attribute  --> 
<result type="json" name="list"> 
<param name="excludeProperties"> 
user 
</param> 
</result> 

<!--  The root object contains only user --> 
<result type="json"> 
<param name="root"> 
user 
</param> 
</result> 

<!-- "root" Of the parent class in the object field( attribute ) Don't ( Will be? )  Default to  JSON In the data, if you don't want to do this, you need to specify it at configuration time  ignoreHierarchy  for  false: --> 
<result type="json"> 
<param name="ignoreHierarchy">false</param> 
</result> 


Related articles: