0

I have a Struts 2 application in which i use the struts 2 json plugin for json handling.

Now i want to use the dojo data grid to populate data from an action. I can call the action. All the inbuilt data types are working in the action. However when i use a custom object in my class, i get errors in the action class.

I want to use ItemFileReadStore as a store for the grid which needs data in the format like:

items: [{obj1},{obj2},{obj3},{obj4}]

Now i have a class called Device. I want to send a list of Device objects back to the client. But how do i provide data in the above format and use it on client side.?

Edit:

I get the following error:

 E com.ibm.ws.webcontainer.webapp.WebApp logError SRVE0293E: [Servlet Error]-[com.googlecode.jsonplugin.JSONException: com.googlecode.jsonplugin.JSONException: java.lang.reflect.InvocationTargetException]: com.ibm.ws.webcontainer.webapp.WebAppErrorReport: com.googlecode.jsonplugin.JSONException: com.googlecode.jsonplugin.JSONException: java.lang.reflect.InvocationTargetException
at com.ibm.ws.webcontainer.webapp.WebAppDispatcherContext.sendError(WebAppDispatcherContext.java:624)
at com.ibm.ws.webcontainer.srt.SRTServletResponse.sendError(SRTServletResponse.java:1071)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:725)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:77)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:852)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:917)
at com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.invokeFilters(DefaultExtensionProcessor.java:924)
at com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.handleRequest(DefaultExtensionProcessor.java:852)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3610)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:274)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:926)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1557)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:173)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:455)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:384)
at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:202)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:766)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:896)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1527)

What is the reason for this error. My action class is:

jsonWrapper.setIdentifier("firstName");         

        jsonWrapper.getListItems().add(User.getUser("t2590pk"));            
        jsonWrapper.getListItems().add(User.getUser("t8923sm"));

        jsonWrapper.setItems(jsonWrapper.gson.toJson(jsonWrapper.getListItems()));

        System.out.println(jsonWrapper.getItems());

Struts config:

<action name="jsonTest" class="com.dcx.ispeed.actions.JSONTest">            
        <result type="json">                
            <param name="excludeProperties">
                gson
            </param>                
        </result>
    </action>

The jsonWrapper class:

/**


* 
 */
package com.dcx.ispeed.business;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ibm.ws.http.HttpRequest;

/**
 * @author t2590pk
 *
 */
public class JSONWrapper {
    public Gson gson = new Gson();

    private String identifier;

    private String label;

    private String items;

    private List listItems = new ArrayList();

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getItems() {
        return items;
    }

    public void setItems(String items) {
        this.items = items;
    }

    public List getListItems() {
        return listItems;
    }

    public void setListItems(List listItems) {
        this.listItems = listItems;
    }

    /**
     * 
     */
    public JSONWrapper() {
        System.out.println("Calling JSON wrapper constructor.");
    }   

}

Thanks.. :)

pratik
  • 119
  • 10
  • I use the struts2-json-plugin with all kinds of datatypes even JPA entities... so am confident that it can be done that way. Remember your action is a marshalling point for what you want to go to the view. You can create an ArrayList and populate it with what you want. You are probably aware of include and exclude params see here: http://struts.apache.org/2.2.3/docs/json-plugin.html and see here for using them with annotations (my preferred way): http://stackoverflow.com/questions/4796390/struts2-json-serialization-of-items/4796742#4796742 – Quaternion Mar 19 '12 at 21:10

2 Answers2

1

You can use google Gson package as following

import com.google.gson.Gson;

String json = "{\"name\":\"ABC\",\"address\":\"some address\"}";
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);

public class Person{
   public String name;
   public String address;
}

Note: you have to implement default constructor and all getter and setter for the Person class. The array in your case will be a Set

Julias
  • 5,752
  • 17
  • 59
  • 84
  • I have the following code in my Action: `jsonWrapper.setIdentifier("firstName"); User u = User.getUser("xyz"); jsonWrapper.getListItems().add(u); u = User.getUser("pqr"); jsonWrapper.getListItems().add(u); Gson gson = new Gson(); jsonWrapper.setItems(gson.toJson(jsonWrapper.getListItems()));` I am using Struts2 json plugin for the same. The items are set properly. However i get the following error in the Action. [Servlet Error]-[com.googlecode.jsonplugin.JSONException: com.googlecode.jsonplugin.JSONException: java.lang.reflect.InvocationTargetException. – pratik Mar 20 '12 at 10:44
0

this might be happen with your gson library so please update your gson library and etends jsonException when u use json that will show where in your code exception is coming.

neeraj bharti
  • 361
  • 1
  • 3
  • 20