3

I am calling an action from jQuery Ajax with the following code and it returns me complete code of JSP page. All I need is the array list that is defined in the action class.

dashboard.js

$.ajax({
 url : 'ELD/getAllDivisions',
 type : 'POST',
 dataType: 'text/javascript',
 success : function(data) {
   alert("success");
   var response = data;
   alert(response);
  });

DivisionAction.java

@Autowired
private DivisionService divisionService;

private List<DivisionModel> divisionList = new ArrayList<DivisionModel>();

public String getAllDivisions() {
    divisionList = divisionService.getAllDivisions();
    return SUCCESS;
}

struts.xml

<constant name="struts.devMode" value="true" />
<package name="DIVISION" namespace="/" extends="struts-default">
    <action name="getAllDivisions" method="getAllDivisions" class="foo.bar.DivisionAction">
        <result name="success">/jsp/users/AdminDashboard.jsp</result>
    </action> 

Response enter image description here

All I need is the array list being returned from the action class.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Mehmood Memon
  • 1,129
  • 2
  • 12
  • 20

1 Answers1

1

You have two ways:

Old (unnecessarily complex) way

Return a JSP, inside the JSP iterate your list and do whatever you need: create a JSON array, or write HTML elements (eg. <option> elements), etc... for example:

<action name="getAllDivisions" method="getAllDivisions" class="foo.bar.DivisionAction">
    <result name="success">/jsp/users/allDivisions.jsp</result>
</action> 

allDivisions.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
[
<s:iterator value="divisionList" status="ctr">
    {         
       "id"        : "<s:property value='modelId'   />", 
       "modelName" : "<s:property value='modelName' />"
    }
    <s:if test="%{#ctr.count < divisionList.size}"> , </s:if>
</s:iterator>
]

New (right) way

Use the JSON plugin, return a JSON result specifying your List as the root object (read more):

<package name="DIVISION" namespace="/" extends="json-default">

    <action name="getAllDivisions" method="getAllDivisions" class="foo.bar.DivisionAction">
        <result name="success" type="json>
            <param name="root"> divisionList </param>
        </result>
    </action> 
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • *Old* way isn't all that horribly if the response should be just included in jsp (i.e. no need to manipulate response in js). – Aleksandr M Mar 23 '16 at 13:51
  • The linked question should not cover the topic how to return JSON result, it's shadowing my answer which is using three ways of returning JSON result, the one that in [my question](http://stackoverflow.com/q/35753928/573032) is never yet covered by anyone. – Roman C Mar 23 '16 at 14:04
  • 1
    New(right) way worked and produced exactly what I wanted. Thanks. – Mehmood Memon Mar 23 '16 at 18:08
  • Why returning the list with `includeProperties` will **always** return an empty list? I'm having this issue and I could use this answer but can't understand why `includeProperties` wouldn't work. `divisionList` – JorgeGRC Jan 19 '17 at 16:27