4

I am new in Struts2 and and working on one project. While integrating front end with back-end module I am not getting any error or exception but the action class is not mapping with the JSP form where I need to populate the department list. I referred many examples from different blogs and its working, but in my module same I followed but not worked, the difference is only I am mapping action using Ajax call. I am a sharing sample code bellow:


In 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 don't know whats going wrong. Please suggest solution. Is there wrong with ajax call?

Roman C
  • 49,761
  • 33
  • 66
  • 176
MegaBytes
  • 6,355
  • 2
  • 19
  • 36

3 Answers3

3

Yes, I got a Solution There was a wrong with my ajax call,

I create one JQuery function as given to load JSP page inside a div-panel

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");
}

and onclick event I just call this method

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

there was only a simple change instead of employee.jsp I used employee.action. That was very simple and it's working also.

Roman C
  • 49,761
  • 33
  • 66
  • 176
MegaBytes
  • 6,355
  • 2
  • 19
  • 36
0

i am not that good at struts but i can suggest a few problems, GET method as you know cant take much data so use POST method instead, and secondly when the page returns success the struts will redirect the page to the same page and reload and most of your passed data will be lost so nothing will happen. i think this is the main problem you have

Mumin Asaad
  • 180
  • 9
0

You don't map actions with AJAX, you call them. The action mapping is statically done in struts.xml (or with annotations when using the Convention Plugin).

You should ensure you are using a namespace defined by you instead of the default one (not mandatory but definitely a good practice):

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

Then use the <s:url/> Struts tag to correctly mount the action URL:

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

That should be enough to make it work.

Note: take a look at the Struts2-jQuery-plugin Showcase, especially the Form -> AJAX select example, it simplifies this thing a lot.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Now, It gives me an exception : [/web-module] threw exception [tag 'select', field 'list', name 'departments': The requested list key 'departments' could not be resolved as a collection/array/map/enumeration/iterator type – MegaBytes Sep 16 '14 at 10:31
  • I googled for this exception, but couldn't resolved yet – MegaBytes Sep 16 '14 at 10:33
  • This exception means your `public List getDepartments() {` can't be reached. You should pay attention to what exactly you are doing, there is a lot of things going on here. You use Action MB to render the page, then you call it again through AJAX and then you manipulate a JSP through javascript. It's not clear at all, but usually you should have one Action rendering the page, and another action (another class, or another method of the same class) to call through AJAX. Then you should manipulate the DOM / HTML through javascript, without touching any JSP. – Andrea Ligios Sep 16 '14 at 10:36
  • Consider [reading this](http://stackoverflow.com/a/25762261/1654265) and the answers linked in there, especially how to return a JSP snippet, how to return a JSON object, and how to use struts2-jquery plugin to do this kind of things a lot easier than you are doing now. Hope that helps – Andrea Ligios Sep 16 '14 at 10:37
  • Thanks, Clearly, in my case I am trying to populate list of departments on jsp page using '', the jsp page I am including inside another jsp page. but It gives me exception. I tried few example it works, but same I am doing in my module. it doesn't work – MegaBytes Sep 16 '14 at 10:51
  • This `loadpageinternal(ipage, "employeeOnboard.jsp")` is suggesting you are referencing the .jsp page without having it rendered by an action. You should return a JSP page as a result of an action, as described in the mentioned link... and use different actions, like said. – Andrea Ligios Sep 16 '14 at 11:00
  • 1
    Now, I am not using `loadpageinternal(ipage,"employee.jsp")` instead I did one ajax call to render the page which rendered by action class (it also prints some syso's of action class constructor and execute methods). after doing this it showing same exception for `` tag. – MegaBytes Sep 16 '14 at 13:12