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

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>  ...

Create clone of git project from existing code without do new clone

In this article, I will guide you how to create a clone of a existing project by using the already cloned code set which available in another computer. When you going to clone the project from remote repository, it's need to download the project form the remote. Trouble start if the project got more capacity and slow internet connections or temporary disconnection to clone new one. Step 01.  Go to the project location of code existing place by console. (I'm using linux terminal) Step 02. Execute following command at the project place. git archive --format zip --output /full/path/to/myproject.zip master Project code has exported to the myproject.zip, but not included the .git folder. Take both myproject.zip file and .git folder to another computer or another place of your computer Step 3. Extract the zip and copy the .git folder into it. Step 4. Go inside to the project and take git pull (If internet connection available). Now it's working as a newly clone...

EJB3 Stateless Maven example with Jboss Application server

Step 1 : Download jboss 5.1 application server from below (Which supporting for JDK 1.6) http://jbossas.jboss.org/downloads Extract and start the Jboss server. Step 2 : Create sample maven project as below <?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/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <groupId>ejb</groupId>     <artifactId>ejb-stateless</artifactId>     <version>1.0-SNAPSHOT</version>     <dependencies>         <dependency>             <gro...