Monday, February 2, 2015

HOW TO SEND AND RECEIVE ARRAYLIST TO ACTION IN STRUTS2

jars
gson-2.2.4.jar
struts2-json-plugin-2.3.16.3.jar
and other required jar files for struts2


STRUTS.XML

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />

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

<action name="*/*/*" class="com.elitecore.nvsmx.policydesigner.controller.{1}.{2}CTRL" method="{3}">
<interceptor-ref name="basicStack"></interceptor-ref>
<result type="json">
<param name="root">pageDataList</param>
</result>
</action>

</package>  


</struts>

ACTION/CONTROLLER

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

/**
 * @author Kirpalsinh.raj
 *
 */
public class PkgCTRL extends ActionSupport {
private List<PkgData> pageDataList = new ArrayList<PkgData>(10);

public String getPageData(){
 
try {        
            Gson gson = new Gson();
            List<PkgData> pkgDatas = gson.fromJson(listData, new TypeToken<List<PkgData>>() {}.getType());             
           
            int iBeginIndex = 3;                        
            int iEndIndex = 10;
           
            if(iEndIndex > pkgDatas.size()){
               iEndIndex = pkgDatas.size();
            }
           
            pageDataList.clear();
            pageDataList =  pkgDatas.subList(iBeginIndex, iEndIndex);    
       }
   }      
}catch (Exception e) {
   LogManager.getLogger().trace(MODULE,e);
}

return Results.SUCCESS;
}

}


JSP
<%@page import="com.google.gson.reflect.TypeToken"%>
<%@page import="java.util.*"  %>
<%@page import="com.raj.nvsmx.pd.model.pkg.PkgData,com.google.gson.Gson" %>
<%@page import="com.google.gson.Gson" %>
<%@page import="java.lang.reflect.Type" %>

<%
List<PkgData> pkgsList = (List<PkgData>) request.getAttribute("pkgsList");
Type pkgListType = new TypeToken<List<PkgData>>() {}.getType();
String json = new Gson().toJson(pkgsList,pkgListType);
%>
<script>

function getListData(){
 var url = "/searchNs/pkg/Pkg/getPageData";

 $.ajax({
   type: "POST",    
   url: contextPath+url,
   data: {listData:'<%=json%>'},    
   success: function(jsonDataArray){     
    for (var i = 0; i < jsonDataArray.length; i++) {    
    jsonDataArray[i].description
    jsonDataArray[i].packageMode
    jsonDataArray[i].status
    jsonDataArray[i].validityPeriod            
        }
   },
   error: function(jsonData){
   }
});
}


</script>

POJO

public class PkgData implements Serializable {


private static final long serialVersionUID = 1L;

private String id;          
private String name;                
private String description;         
private String type;                
private String status;              
private String packageMode;                
private Integer validityPeriod;     
private String validityPeriodUnit;
private Double price;               
private Timestamp createdDate;        
private Long createByStaffId;

...setter and getter method

}

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...