Ashok.N
Ashok.N

Reputation: 11

Struts2 JQGrid, need example to do

New to the struts2 framework.

I want to see customer details in a grid format.

Searched in web, best result said to use jqgrid.

I have done the following:

  1. Added necessary Jar Files Like , jquery,json,jqgrid,convention...
  2. Created design view using jsp and jqgrid tag,
  3. Crated action class with annotations...

Its not showing any error but in deployment or at run time its not working.

Please any one give complete working example for my reference

Upvotes: 0

Views: 7925

Answers (2)

Raghavender Reddy
Raghavender Reddy

Reputation: 180

Here is the Solution for your problem create a grid action which is shown as below 


    port java.util.ArrayList;
    import java.util.List;
    import java.util.Map;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts2.interceptor.ServletRequestAware;

    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opsbuds.rju.modules.usermgmt.bo.RoleManagementBo;
    import com.opsbuds.rju.modules.usermgmt.bo.UserManagementBo;
    import com.opsbuds.rju.modules.usermgmt.model.Role;


    public class viewRolesGridAction extends ActionSupport  implements 
    ServletRequestAware{
        private List<String> module = new ArrayList<String>();
        private Map session;
        HttpServletRequest request;
        private List<Role> roles; 
        private List<Role> roles1; 
        public List<Role> getRoles() {
            return roles;
        }

        /**
         * @return the roles1
         */
        public List<Role> getRoles1() {
            return roles1;
        }

        /**
         * @param roles the roles to set
         */
        public void setRoles(List<Role> roles) {
            this.roles = roles;
        }
            public void setRoles1(List<Role> roles1) {
            this.roles1 = roles1;
        }
        public List getModule() {
            return module;
        }
        public void setModule(List module) {
            this.module = module;
        }      
        public void getModules() {       
        Map session = ActionContext.getContext().getSession();
        session.get("module");
        setModule((List) session.get("module"));
        }
         public List<Role> getGridModel() {
                return gridModel;
            }

            public void setGridModel(List<Role> gridModel) {
                this.gridModel = gridModel;
            }

          private List<Role>      gridModel;

          //get how many rows we want to have into the grid - rowNum attribute in the grid
          private Integer             rows             = 0;

          //Get the requested page. By default grid sets this to 1.
          private Integer             page             = 0;

          // sorting order - asc or desc
          private String              sord;

            public Integer getPage() {
                return page;
            }

            public void setPage(Integer page) {
                this.page = page;
            }

          // get index row - i.e. user click to sort.
          private String              sidx;

          // Search Field
          private String              searchField;

          // The Search String
          private String              searchString;

          // he Search Operation ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
          private String              searchOper;

          // Your Total Pages
          private Integer             total            = 0;


            public Integer getRecords() {
                return records;
            }

            public void setRecords(Integer records) {
                this.records = records;
            }

            public Integer getTotal() {
                return total;
            }

            public void setTotal(Integer total) {
                this.total = total;
            }

          // All Record
          private Integer             records          = 0;

             public Integer getRows() {
                 return rows;
         }

         public void setRows(Integer rows) {
                 this.rows = rows;
         }
         public void setServletRequest(HttpServletRequest request) {
                this.request = request;
            }

        //  @Actions( {
    //      @Action(value = "/viewRolestemp", results = {
    //        @Result(name = "success", type = "json")
    //      })
        //  })
          public String execute()
          {
              getModules();

             // rows=10;
              System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxInside VIEWRolesAct execute method---------------------------------------------------------------------");

        //
            int to = (rows * page);
            int from = to - rows;

            //Count Rows (select count(*) from custumer)
          //records = 9;
              RoleManagementBo rolemgmt = new RoleManagementBo();
                if (RoleAction.rolesinserttatus) {
                    System.out.println("role status");
                    addActionMessage(("Role saved successfully"));
                    RoleAction.rolesinserttatus = false;
                }
                if (RoleAction.roleupdatestatus) {
                    addActionMessage(("Role updated successfully"));

                }
                try{
                roles = rolemgmt.viewRoles();
                records=roles.size();
                getModules();
                RoleAction.rolesinserttatus = false;
                RoleAction.roleupdatestatus = false;
                }catch (Exception e) {
                    addActionError(getText(""));
                }


            to = (getRows() * getPage());
            if (to > getRecords())
                    to = getRecords();
            roles1 =  new ArrayList();

            for(int i=from;i<to;i++)
                {
                roles1.add(roles.get(i));


                }


              gridModel = roles1;


            setTotal((int) Math.ceil((double) getRecords() / (double) getRows()));

            return SUCCESS;
          }

          public String getJSON()
          {
              getModules();
            return execute();
          }
    }

create a jsp which is shown here

<s:url id="remoteurl" action="viewRolestemp" />


                <sjg:grid id="gridtable" caption="Roles" dataType="json"
                    href="%{remoteurl}" pager="true" gridModel="gridModel"
                    rowList="10,15,20" rowNum="10" rownumbers="true"
                    width="800" navigator="true" navigatorView="false" navigatorDelete="false" navigatorAdd="false" navigatorEdit="false" navigatorSearch="false">

                    <sjg:gridColumn name="roleName" index="roleName" title="Role Name"
                        sortable="true" formatter="editrole" />
                    <sjg:gridColumn name="createdDate" index="createdDate"
                        title="Created Date" sortable="true" />
                    <sjg:gridColumn name="modificationDate" index="modificationDate"
                        title="Modified on" sortable="true" />


                </sjg:grid>

configuring in action classs

<action name="viewRolestemp"
            class="com.opsbuds.rju.modules.usermgmt.action.viewRolesGridAction">
            <result name="input">/jsp/usermgmt/viewRoles.jsp</result>
            <result name="success" type="json">>/jsp/usermgmt/viewRoles.jsp
            </result>
            <result name="error">/jsp/usermgmt/viewRoles.jsp</result>
        </action> 

Upvotes: 0

Johannes
Johannes

Reputation: 2070

Have you seen the Showcase Apps? This is a complete working example. Maybe the following Slides give you an Start.

http://www.slideshare.net/johannesgeppert1/introduction-into-struts2-jquery-grid-tags

Upvotes: 2

Related Questions