Skip to main content

Struts2 Data Grid with Maven

In this document refers how to use Struts2 jQuery grid for view data set.

 This is the project structure using maven of my example. (IDE Intellj Idea 12 )


















Step 01. Start Struts2 project with maven builder.



pom.xml : Need to add struts2 jquery plugin, grid plugin and json pluing as dependencies.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.struts2.gridExample</groupId>
    <artifactId>Struts2GridExample</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>Struts2Example Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>

        <dependency>
            <groupId>com.jgeppert.struts2.jquery</groupId>
            <artifactId>struts2-jquery-grid-plugin</artifactId>
            <version>3.3.1</version>
        </dependency>

        <dependency>
            <groupId>com.jgeppert.struts2.jquery</groupId>
            <artifactId>struts2-jquery-plugin</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.3.15</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>Struts2GridExample</finalName>
    </build>
</project>

struts.xml :

"json-default" , "action-default" package names and following results should be added


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



<?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.action.extension" value="html"/>

    <package name="default" namespace="/" extends="struts-default,json-default,action-default">
        <result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult"/>
        </result-types>

        <default-action-ref name="gridView"/>

        <action name="index" class="grid.example.GridViewAction" method="execute">
            <result name="success">/pages/strutsExample.jsp</result>
        </action>
        <action name="gridView" class="grid.example.GridViewAction" method="viewGrid">
            <result name="success" type="json"> </result>
        </action>
    </package>
</struts>


Step 2 : Create Model/ Bean class for data. StudentBean.java

package grid.example;

public class StudentBean {

    private String name;
    private Integer age;
    private String email;
    private String address;
    private String contact;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Step 3: Create action class GridViewAction.java

package grid.example;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.validation.SkipValidation;

import java.util.ArrayList;
import java.util.List;

public class GridViewAction extends ActionSupport {

    //Your grid result List
    private List<StudentBean> gridModel = new ArrayList<StudentBean>();
    //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;
    // 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;
    // All Record
    private Integer records = 0;

    @SkipValidation
    public String viewGrid() {
        int to = (rows * page);
        int from = to - rows;

        //Count Rows (select count(*) from custumer)
        records = gridModel.size();
        //Your logic to search and select the required data.
        //gridModel = studentService.getStudentList(from, to);
        gridModel = createGridList();

        //calculate the total pages for the query
        total = (int) Math.ceil((double) records / (double) rows);
        return SUCCESS;
    }

    // This is temporary data set for test
    private List<StudentBean> createGridList() {
        List<StudentBean> gridModel = new ArrayList<StudentBean>();
        StudentBean student = new StudentBean();
        student.setName("Malith");
        student.setAge(20);
        student.setEmail("mailtomalith@yahoo.com");
        student.setAddress("Kandy");
        student.setContact("1234");
        gridModel.add(student);
        student = new StudentBean();
        student.setName("Nilantha");
        student.setAge(21);
        student.setEmail("mailthmeee@gmail.com");
        student.setAddress("Kandy");
        student.setContact("56789");
        gridModel.add(student);
        return gridModel;
    }

    public List<StudentBean> getGridModel() {
        return gridModel;
    }

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

    public Integer getPage() {
        return page;
    }

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

    public Integer getRecords() {
        return records;
    }

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

    public Integer getRows() {
        return rows;
    }

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

    public String getSearchField() {
        return searchField;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }

    public String getSearchOper() {
        return searchOper;
    }

    public void setSearchOper(String searchOper) {
        this.searchOper = searchOper;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public String getSord() {
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public Integer getTotal() {
        return total;
    }

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

Step 4 : Create JSP for view grid strutsExample.jsp. Following highlighted lines should be include the page. 

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags" %>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags" %>

<sj:head jqueryui="true" jquerytheme="redmond"/>


<div>
    <s:url var="remoteurl" action="gridView.html"/>
    <sjg:grid
            id="gridtable"
            caption="Student Data"
            dataType="json"
            href="%{remoteurl}"
            pager="true"
            gridModel="gridModel"
            rowList="10,15,20"
            rowNum="15"
            rownumbers="true"
            autowidth="false"
            >
        <sjg:gridColumn name="name" index="name" title="Student Name" sortable="false"/>
        <sjg:gridColumn name="age" index="age" title="Age" sortable="false"/>
        <sjg:gridColumn name="email" index="mail" title="Email" sortable="false"/>
        <sjg:gridColumn name="address" index="address" title="Address" sortable="false"/>
        <sjg:gridColumn name="contact" index="contact" title="Contact" sortable="false"/>
     </sjg:grid>
</div>

Build the project form Maven. (mvn clean install  in command line)


Download the project from github here


Clone example by command line :

git clone https://github.com/malithmeee/struts2-grid.git

Comments

Popular posts from this blog

Prevent hot deployments memory leaks in Tomcat

Since we are mostly using Tomcat as App Container, it's making tough environment when deploying a patch or existing application without restarting tomcat server (Hot deployments) due to reaching the PermGen size due to memory leaks. Hence most of the cases we have to increase PermGen size more than required to deployed applications and it's wasting of memory in the system.  There is a solution for them to release PermGen size and let it clean by GC in JVM at hot deployment. Step 01: Include below dependency to pom.xml <dependency>               <groupId>se.jiderhamn.classloa der-leak-prevention</groupId>         <artifactId>classloader-leak-p revention-servlet</artifactId>   <version>2.2.0</version> </dependency> Step 02: Add listener to web.xml <listener>   <listener-class>se.jiderhamn.c lassloader.leak.prevention.Cla ssLoaderL...

Jboss 5.1.0_GA, Maven, Mysql, EJB3, JSF2 sample

I thought to write this because of no completed sample found with using above technologies. Environment JDK 1.6 Jboss-5.1.0.GA JSF2 Maven 3 EJB3 Note : Please copy the mysql connector jar to Jboss lib e.x. I copied mysql-connector-java-5.1.29.jar to jboss-5.1.0.GA/server/default/lib Clone the sample from github here git clone https://github.com/malithmeee/library.git Build the entire project and copy the library.ear to Jboss server. Try to access the web page using http://localhost:8080/library.web/home.jsf Note : Database needed to be created, and change properties in relevant files.

Birt Dynamic Table Creation from JDBC Mysql Connection with Maven

In this post refers how to create dynamic Birt reports using given customizing fields list using Birt Runtime 4.2.2 Step 01 : First need to download birt runtime. Download from here For view/Edit the rptdesign files, it's better to use Eclipes 4.2.2 Birt integration. Download eclipes here Step 02 : Extract the birt-runtime-4_2_2. and set the birt runtime home environment variable. e.g. in my case i'm set the environment variable in .bash_profile since i'm using Fedora. (we can specify the Birt home in java code as wel) export BIRT_HOME=/home/malith/usr/eclips/birt-runtime-4_2_2 Step 03 : Create Maven project and insert the dependency for Birt Runtime         <dependency>             <groupId>org.eclipse.birt.runtime</groupId>             <artifactId>org.eclipse.birt.runtime</artifactId>  ...