How to map action class using ajax request in Struts 2

I am new to Struts2 and working on one project. When integrating the interface with an external module, I do not get any errors or exceptions, but the action class does not map to the JSP form, where I need to fill out the list of departments. I mentioned many examples from different blogs and its work, but in my module I followed, but did not work, the only difference is that I map the action using an Ajax call. Below is a sample code below:


In the JSP Page:

    function getPage(ipage){
    $.ajax({
    type: "GET",
    url: "employeeOnboard",
    success: function (data) {                            
     loadpageinternal(ipage, "employeeOnboard.jsp")
   } //success
});
}

-----
<s:select name="department" id="department"
                        headerValue="Select Department" list="departments" />

In struts.xml:

<action name="employeeOnboard" class="org.tungsten.erp.employee.actions.MB">
        <result name="success">employeeOnboard.jsp</result>
</action>

Action class:

package org.tungsten.erp.employee.actions;

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

import org.hibernate.HibernateException;
import org.tungsten.erp.employee.CommonUtitilty;
import org.tungsten.erp.employee.entities.Employee;
import org.tungsten.erp.employee.services.impl.EmployeeOnboardingServiceImpl;
import org.tungsten.erp.employee.services.intf.EmployeeOnboardingServiceIntf;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

/**
 * This Action Class is used for Mapped with EmployeeOnboarding jsp form and to register new Employee Details.
 * @author MegaBytes1024
 *
 */

public class MB extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 115563261740687363L;

    private Integer Emp_No;
    private String Emp_First_Name;
    private String Emp_Last_Name;
    private String Emp_Gender;
    private String Emp_DateOfJoining;
    private String Emp_PersonalEmail;
    private String Branch_Description;
    private String Department_Description;
    private String ERP_HRMS_Employee_role;
    private String Emp_ProjManager_Id;
    private String Emp_TungstenEmail;

    private EmployeeOnboardingServiceIntf onboardingServiceIntf = new EmployeeOnboardingServiceImpl();

    private List<String> departments;

    public MB() {
        System.out.println("This is demo in constructor");  // TODO Auto-generated constructor stub
    }

    public List<String> getDepartments() {
        return departments;
    }

    public void setDepartments(List<String> departments) {
        this.departments = departments;
    }

    public Integer getEmp_No() {
        return Emp_No;
    }

    public void setEmp_No(Integer emp_No) {
        Emp_No = emp_No;
    }

    public String getEmp_First_Name() {
        return Emp_First_Name;
    }

    public void setEmp_First_Name(String emp_First_Name) {
        Emp_First_Name=emp_First_Name;
    }

    public String getEmp_Last_Name() {
        return Emp_Last_Name;
    }

    public void setEmp_Last_Name(String emp_Last_Name) {
        Emp_Last_Name = emp_Last_Name;
    }

    public String getEmp_Gender() {
        return Emp_Gender;
    }

    public void setEmp_Gender(String emp_Gender) {
        Emp_Gender = emp_Gender;
    }

    public String getEmp_DateOfJoining() {
        return Emp_DateOfJoining;
    }

    public void setEmp_DateOfJoining(String emp_DateOfJoining) {
        Emp_DateOfJoining = emp_DateOfJoining;
    }

    public String getEmp_PersonalEmail() {
        return Emp_PersonalEmail;
    }

    public void setEmp_PersonalEmail(String emp_PersonalEmail) {
        Emp_PersonalEmail = emp_PersonalEmail;
    }

    public String getBranch_Description() {
        return Branch_Description;
    }

    public void setBranch_Description(String branch_Description) {
        Branch_Description = branch_Description;
    }

    public String getDepartment_Description() {
        return Department_Description;
    }

    public void setDepartment_Description(String department_Description) {
        Department_Description = department_Description;
    }

    public String getERP_HRMS_Employee_role() {
        return ERP_HRMS_Employee_role;
    }

    public void setERP_HRMS_Employee_role(String eRP_HRMS_Employee_role) {
        ERP_HRMS_Employee_role = eRP_HRMS_Employee_role;
    }

    public String getEmp_ProjManager_Id() {
        return Emp_ProjManager_Id;
    }

    public void setEmp_ProjManager_Id(String emp_ProjManager_Id) {
        Emp_ProjManager_Id = emp_ProjManager_Id;
    }

    public String getEmp_TungstenEmail() {
        return Emp_TungstenEmail;
    }

    public void setEmp_TungstenEmail(String emp_TungstenEmail) {
        Emp_TungstenEmail = emp_TungstenEmail;
    }

    @Override
    public String toString() {
        return "MB [Emp_No=" + Emp_No + ", Emp_First_Name=" + Emp_First_Name
                + ", Emp_Last_Name=" + Emp_Last_Name + ", Emp_Gender="
                + Emp_Gender + ", Emp_DateOfJoining=" + Emp_DateOfJoining
                + ", Emp_PersonalEmail=" + Emp_PersonalEmail
                + ", Branch_Description=" + Branch_Description
                + ", Department_Description=" + Department_Description
                + ", ERP_HRMS_Employee_role=" + ERP_HRMS_Employee_role
                + ", Emp_ProjManager_Id=" + Emp_ProjManager_Id
                + ", Emp_TungstenEmail=" + Emp_TungstenEmail + "]";
    }

    @Override
    public String execute() throws Exception {
        System.out.println("This is Manoj Bawane");
        departments=new ArrayList<String>();
        departments.add("Manoj");
        departments.add("Bhudevi");
        departments.add("Darshana");
        departments.add("Rasika");
        Emp_First_Name="Manoj";

        System.out.println("Department List : "+departments);
        return Action.SUCCESS;
    }

    public String showMB(){
        System.out.println("This is Demo For MB");
        System.out.println("Akshay");
        System.out.println(this.toString());
        return Action.SUCCESS;
    }

    public String saveEmployeeDetails(){
        try{
            System.out.println("Action : Saving employee Details ");
            Employee employee=convertActionFieldsIntoEmployeeFields();
            System.out.println("converted into employee : "+employee.toString());
            onboardingServiceIntf.saveEmployeeOnBoardingService(employee);
            System.out.println("Action : Saved employee Details ");
            return Action.SUCCESS;
        }catch(HibernateException ex){
            addActionError("Failed to save Details");
            return Action.ERROR;
        }
    }

    /**
     * This method converts the action POJO to Entity
     * @return
     */
    private Employee convertActionFieldsIntoEmployeeFields() {

        Employee employee = new Employee();
        employee.setEmp_No(this.getEmp_No());
        employee.setEmp_First_Name(this.getEmp_First_Name());
        employee.setEmp_Last_Name(this.getEmp_Last_Name());
        employee.setEmp_Gender(this.getEmp_Gender());
        employee.setEmp_PersonalEmail(this.getEmp_PersonalEmail());
        employee.setEmp_DateOfJoining(CommonUtitilty.stringToDateConversion(this.getEmp_DateOfJoining()));
        employee.setEmp_Branch_Id(1);
        employee.setEmp_Dept_Id(1);
        employee.setEmp_Role_Id(1);
        employee.setEmp_TungstenEmail(this.Emp_TungstenEmail);
        employee.setEmp_ProjManager_name(this.Emp_ProjManager_Id);

        return employee;
    }
}

I do not know what is going wrong. Please suggest a solution. Is there an incorrect ajax call?

+4
source share
3 answers

, - ,

JQuery JSP- div-

function loadpageinternal(idi, pagei) {

    $("#internal-panel").css("display", "block");
    $(".first").removeClass("active");
    if (pci != null) {
        $(pci).removeClass("active");
    }
    $("#internal-panel").load(pagei);
    pci = idi;
    $(idi).addClass("active");
}

onclick

<li onclick="loadpageinternal(this,'employeeOnboard.action')">OnBoard</li>

employee.jsp employee.action. , .

+3

struts, , GET , , POST, -, , struts , , . , , .

0

AJAX, . struts.xml( -).

, , , ( , ):

<package name="MBpackage" extends="struts-default" namespace="MBnamespace" >
    <action name="employeeOnboard" class="org.tungsten.erp.employee.actions.MB">
        <result name="success">employeeOnboard.jsp</result>
    </action>

<s:url/> Struts, URL- :

 function getPage(ipage){
    $.ajax({
    type: "GET",
    url: '<s:url action="employeeOnboard" namespace="MBnamespace"/>',

, .

. Struts2-jQuery-plugin Showcase, Form -> AJAX select, .

0

All Articles