Question

I am struggling to get the JSON response from my Struts2 Action class, I think i am missing something. The following set up I have in my Project.

in my module level action definition , The configuration looks like :

<package name="customer" namespace="/" extends="struts-default,json-default">
   <action name="getCustomer" method="getCustomerBusiness" class="CustomerAction">
      <result type="json"/>
   </action>
</package>

in my Struts.xml I have

 <result-types>
    <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
 </result-types>

 <interceptors>
    <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
 </interceptors>

In My Action Class:

  public class CustomerAction extends ActionSupport implements ServletRequestAware,
        ServletResponseAware, ModelDriven {
    private List<CustomerBean> cpbeanList;

    public List<CustomerBean> getCpbeanList() {
        return cpbeanList;
    }

    public void setCpbeanList(List<CustomerBean> cpbeanList) {
        this.cpbeanList = cpbeanList;
    }

    public String getCustomerBusiness() {
        cpbeanList = new ArrayList<CustomerPortfolioBean>();
        // jsonData = new LinkedHashMap<String, Object>();
        CustomerBean cb1 = new CustomerPortfolioBean();
        cb1.setBusinessNm("IBM");
        cb1.setBusinessAddr("475 Anton Blvd");
        cb1.setBusinessPh("00000000");
        cb1.setBusinessCity("Costamesa");
        cb1.setBusinessStateCd("CA");
        c1.setBusinessZip("92704");

        similarly cb2, cb3, cb4.

        cpbeanList.add(cb1);
        cpbeanList.add(cb2);
        cpbeanList.add(cb3);
        cpbeanList.add(cb4);
        return SUCCESS;
    }

}

The JSON request http://localhost:8080/customer/getCustomer returns me empty array {} In the firebug ...I am able to see.

Also I am trying the out put as data table input in JQuery. which doesn't have row because of this.

Any one's help is greatly appreciated.

Was it helpful?

Solution

Your action's superclass implements ModelDriven, hence so does your subclass. It's the model that will be serialized as JSON. If the model is empty, there's nothing to be serialized, so you get nothing back.

Your subclass should override getModel() and return the data you want to be serialized to JSON.

OTHER TIPS

Since

  • The ModelDriven Interceptor pushes the model on top of the ValueStack
  • The Json Result serializes the whole action

and knowing that

  • The Json Result has a root parameter that can be configured to restrict the Json serialization to a single element instead of that to the whole action's attributes
  • The root parameter accepts OGNL

we can make the assumption that it could be instructed to go backwards,
to be less restrictive (from a modelDriven point of view), instead of more restrictive as usual.

You could try to do something like

<result type="json">
    <param name="root">
        [1]
    </param>
</result>

or even better (since it is not guaranteed that [1] is the action)

<result type="json">
    <param name="root">
        #action
    </param>
</result>

to discover if it's actually possible to serialize the whole action while maintaining the Model on top of the ValueStack.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top