haripcce haripcce - 4 months ago 25
JSON Question

Getting response from struts 1 method using json,jquery and jsp

I am using Struts 1, JSP, JSON and jQuery in my project. Now I am sending JavaScript array using JSON to action method and unable to return response from struts action method back to JSP page. Does anybody have a working example or suggestion?

JavaScript code

console.log("start calculateDifference");
console.log("route 1 lenght:"+newRoute1.length);
console.log("route 2 lenght:"+newRoute2.length);
var article = new Object();

article.routeFirst = newRoute1;
article.routeSecond = newRoute2;


$.ajax({
type: 'POST',
url: '../../admin/cabsharing/findIntersesctionPoint.do',
data : "point="+JSON.stringify(article),
dataType: 'json',


success: function(result) {
console.log("success:"+result);
},
error: function(e){
console.log("error:"+e);
}

});


Java code

Route route = new Route();
route.setX(fmt.format(Double.parseDouble(f.getX())));
route.setY(fmt.format(Double.parseDouble(f.getY())));


String jsonString = gson.toJson(route);

PrintWriter writer = response.getWriter();
writer.print(jsonString);
writer.flush();
response.setContentType("application/json");

return null;

Answer

Here is the solution i have applied

Jsp and Javascript Code :

//newRoute1 array contains objects

var a1 = new Object();

    a1.x =12;
    a1.y=34;



 var a2 = new Object();
    a2.x =12;
    a2.y=34;

newRoute1 .push(a1);
newRoute1 .push(a2);

//Similarly newRoute2 array contains object

var a3 = new Object();

        a3.x =12;
        a3.y=34;



     var a4= new Object();
        a4.x =12;
        a4.y=34;

    newRoute2 .push(a3);
    newRoute3 .push(a4);

//following sending using ajax to struts1 action

var article = new Object();

 article.routeFirst = newRoute1;
 article.routeSecond = newRoute2;


    $.ajax({
        type: 'POST',
        url: '../../admin/cabsharing/findIntersesctionPoint.do',
        data : "point="+JSON.stringify(article),
        dataType: 'json',


        success: function(result) {

            console.log("success:"+result.x);
            console.log("success:"+result.y);

        },
        error: function(e){
            console.log("error:"+e);
        }

    });

Following is my java code in struts action class

public Object doFindIntersesctionPoint(BaseActionMapping mapping,
        ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        try {
            String point = request.getParameter("point");
            System.out.println("Point : " + point);

            if (point != null) {
                ObjectMapper mapper = new ObjectMapper();
                mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
                    true);

                try {
                    // read from string, convert it to Article class object
                    Article user = mapper.readValue(point, Article.class);

                    Gson gson = new Gson();
                    TypeToken<List<Route>> token = new TypeToken<List<Route>>() {
                        };

                    List<Route> firstList = gson.fromJson(user.getRouteFirst(),
                            token.getType());
                    List<Route> secondList = gson.fromJson(user.getRouteSecond(),
                            token.getType());
                    DecimalFormat fmt = new DecimalFormat("#.000000");
                    List<Double> diffList = new ArrayList<Double>();
                    int i = 0;

                    for (Route f : firstList) {
                        for (Route s : secondList) {
                            if ((new Double(fmt.format((Double.parseDouble(
                                                fmt.format(Double.parseDouble(
                                                        f.getX()))) -
                                            Double.parseDouble(fmt.format(
                                                    Double.parseDouble(s.getX())))))).equals(
                                        Double.parseDouble("0.000000"))) &&
                                    (new Double(fmt.format((Double.parseDouble(
                                                fmt.format(Double.parseDouble(
                                                        f.getY()))) -
                                            Double.parseDouble(fmt.format(
                                                    Double.parseDouble(s.getY())))))).equals(
                                        Double.parseDouble("0.000000")))) {
                                if (i == 0) {
                                    Route route = new Route();
                                    route.setX(fmt.format(Double.parseDouble(
                                                f.getX())));
                                    route.setY(fmt.format(Double.parseDouble(
                                                f.getY())));



//following need to be set for sending response, jsonString is my object sending back
                                    String jsonString = gson.toJson(route);


                                    response.setHeader("X-JSON", jsonString);

                                    PrintWriter writer = response.getWriter();
                                    writer = response.getWriter();
                                    writer.write(jsonString);
                                    writer.flush();

                                    return null;
                                }


                            }
                        }
                    }

                    System.out.println();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            return null;
        } catch (Exception e) {
            e.printStackTrace();

            return null;
        }
    }

//here is my Article Class

public class Article {

    private String routeFirst;
    private String routeSecond;
    public String getRouteFirst() {
        return routeFirst;
    }
    public void setRouteFirst(String routeFirst) {
        this.routeFirst = routeFirst;
    }
    public String getRouteSecond() {
        return routeSecond;
    }
    public void setRouteSecond(String routeSecond) {
        this.routeSecond = routeSecond;
    }



}

//here is my Route Class

public class Route {
    private String x;
    private String y;

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

    public String getY() {
        return y;
    }

    public void setY(String y) {
        this.y = y;
    }

}

Important for me was not about using Gson but how u send back in struts method.Comment if anybody having problem in this solution