当前位置 : 主页 > 编程语言 > java >

jsp – 如何在Struts 2中使用ajax请求映射动作类

来源:互联网 收集:自由互联 发布时间:2021-06-25
我是Struts2的新手,并在一个项目上工作.在将前端与后端模块集成时,我没有收到任何错误或异常,但是操作类没有映射到我需要填充部门列表的JSP表单.我从不同的博客及其工作中提到了很
我是Struts2的新手,并在一个项目上工作.在将前端与后端模块集成时,我没有收到任何错误或异常,但是操作类没有映射到我需要填充部门列表的JSP表单.我从不同的博客及其工作中提到了很多例子,但是在我的模块中,我遵循但不起作用,不同之处仅在于我使用Ajax调用映射动作.我是一个分享示例代码:

在JSP页面中:

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

在struts.xml中:

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

行动类:

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

我不知道什么是错的.请建议解决方案. ajax调用有问题吗?

是的,我有一个解决方案
我的ajax电话有问题,

我创建了一个JQuery函数,用于在div-panel中加载JSP页面

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.这很简单,也很有效.

网友评论