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

时间:2014-09-16 06:12:54

标签: jsp struts2

我是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调用有问题吗?

3 个答案:

答案 0 :(得分:3)

是的,我有一个解决方案 我的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。这很简单,也很有用。

答案 1 :(得分:0)

我对Struts不是很擅长,但我可以提出一些问题,GET方法,因为你知道不能采用大量数据,所以使用POST方法代替,其次当页面返回成功时struts将页面重定向到同一页面并重新加载,大部分传递的数据将丢失,因此不会发生任何事情。 我认为这是你遇到的主要问题

答案 2 :(得分:0)

您没有使用AJAX 映射操作,您调用它们。动作映射在struts.xml中静态完成(或在使用Convention插件时使用注释)。

您应确保使用由您定义的命名空间而不是默认命名空间(不是强制性的,但绝对是一种好的做法):

<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 tag正确安装操作网址:

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

这应该足以让它发挥作用。

注意:看一下Struts2-jQuery-plugin Showcase,尤其是Form -> AJAX select示例,它会简化这个