gpt4 book ai didi

jsp - 如何在 Struts 2 中使用 ajax 请求映射 Action 类

转载 作者:行者123 更新时间:2023-12-01 02:14:56 26 4
gpt4 key购买 nike

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

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 面板中加载 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 .这很简单,它也可以工作。

关于jsp - 如何在 Struts 2 中使用 ajax 请求映射 Action 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25861757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com