Thursday, 17 April 2014

How to use jquery and ajax in struts2 examples

For Struts2 with JQuery Ajax Example :

Ajax is so useful to web programmers for avoiding unnecessary page loding for small task and getting data
from server with send request and get response from server . the following one is Example .



<%--
    Document   : JTableHelloWorld
    Created on : 12 Nov, 2013, 11:00:32 AM
    Author     : Srikanth Ganji.
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="jt" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="../script/jquery-2.0.3.js"></script>


        <script type="text/javascript">
            $(document).ready(function() {
                $("#remote").click(function() {
                    $.ajax({type: 'POST', url: 'jtableAction.action?employeeId=' + 1, success: tableDisplay});
                });
            });
            function tableDisplay(result)
            {
                if (result) {
                    $("#tableSpace").html(result);
                }
            }
        </script>
        <title>JTable Example</title>
    </head>
    <body>
        <input type="button" id="remote" value="Ajax Table"/>
        <h1>JTable Hello WOrld</h1>
        <div id="tableSpace">

        </div>
    </body>
</html>

------------------------------------------------
DummyPage.jsp
<%-- 
    Document   : DummyPage
    Created on : 9 Dec, 2013, 12:42:44 AM
    Author     : Srikanth Ganji
    
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
      
        <table border="1">
            <tr>
                <td>Employee Name</td>
            <td>Employee Job</td>
            <td>Employee Fruid</td>
            </tr>
            <s:iterator value="%{employeeList}">
            <tr>
                <td><s:property value="employeeName"/></td>
                <td><s:property value="employeeJob"/></td>
                <td><s:property value="fruit"/></td>
            </tr>    
            </s:iterator>
            
        </table>
    </body>
</html>

--------------------------------

Struts.xml

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

<!-- Author: Personal -->
 
<struts>
    
    <package name="JTableExample" extends="struts-default" namespace="/JTable" >
   <action name="jtableAction" class="com.gbuds.ems.action.JTableAction" method="jTableData">
       <result>/JTable/DummyPage.jsp</result>
       </action>
    </package>
</struts>

------------------------------------------------------

Action Classes.

JTableAction.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gbuds.ems.action;

import com.gbuds.ems.beans.Employee;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Srikanth Ganji.
 */
public class JTableAction extends ActionSupport {
  private Employee employee;
  private List<Employee> employeeList;
  private int employeeId;

  public JTableAction()
  {
      System.out.println("\n JTableAction Initialized");
  }
  public String jTableData()
  {
      getEmployeeDetails();
      System.out.println("\n\n JTable Aciton Method"+this.getEmployeeId());
      return SUCCESS;
  }
    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    private void getEmployeeDetails() {
        if(this.getEmployeeList()==null) this.setEmployeeList(new ArrayList<Employee>());
        this.getEmployeeList().add(new Employee("Sairam", "Java Developer", "GOva"));
        this.getEmployeeList().add(new Employee("Srikanth", "Stater", "Mango"));
        this.getEmployeeList().add(new Employee("Sahitay", "Drawing", "Banana"));
        this.getEmployeeList().add(new Employee("Bala Gopal", "Deveroper", "Apple"));
        this.getEmployeeList().add(new Employee("Vamis Krishna", "Designer", "PineApple"));
    }

    public List<Employee> getEmployeeList() {
        return employeeList;
    }

    public void setEmployeeList(List<Employee> employeeList) {
        this.employeeList = employeeList;
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }
  
}


-----------------------------------------------
Employee.java




package com.gbuds.ems.beans;

/**
 *
 * @author Srikanth Ganji
 */
public class Employee {

    private String employeeName;
    private String employeeJob;
    private String fruit;

    public Employee(String employeeName, String employeeJob, String fruit) {
        this.employeeName = employeeName;
        this.employeeJob = employeeJob;
        this.fruit = fruit;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public String getEmployeeJob() {
        return employeeJob;
    }

    public void setEmployeeJob(String employeeJob) {
        this.employeeJob = employeeJob;
    }

    public String getFruit() {
        return fruit;
    }

    public void setFruit(String fruit) {
        this.fruit = fruit;
    }

}

--------------------------------
Sample Output:




Share This Post →

No comments:

Post a Comment

Powered By Blogger |   Design By Srikanth Ganji
Join Our Community
×
blogger tipsblogger templatesFOLLOW