Struts2是一个基于MVC设计模式的Web应用框架,而Ajax(Asynchronous JavaScript and XML)是一种无需重新加载整个网页的情况下,能够更新部分网页的技术。
这是最常见的方式,需要确保项目中已经引入jQuery库。
$.ajax({
type: "POST", // 或 "GET"
url: "yourActionName.action", // Struts2 Action的URL
data: {
param1: "value1",
param2: "value2"
},
dataType: "json", // 期望返回的数据类型
success: function(response) {
// 处理成功响应
console.log(response);
},
error: function(xhr, status, error) {
// 处理错误
console.error(error);
}
});
确保你的Action类有对应的getter和setter方法,并且返回类型设置为"json"(如果你使用JSON插件):
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;
@Namespace("/")
@Result(name="success", type="json")
public class YourAction extends ActionSupport {
private String result;
private String param1;
private String param2;
@Action(value="yourActionName")
public String execute() {
// 处理业务逻辑
this.result = "Processed: " + param1 + " and " + param2;
return SUCCESS;
}
// Getter和Setter方法
public String getResult() { return result; }
public void setResult(String result) { this.result = result; }
public String getParam1() { return param1; }
public void setParam1(String param1) { this.param1 = param1; }
public String getParam2() { return param2; }
public void setParam2(String param2) { this.param2 = param2; }
}
确保struts.xml中配置了json插件:
<struts>
<package name="default" extends="json-default">
<!-- 你的action配置 -->
</package>
</struts>
原因:可能没有正确配置json插件或返回类型 解决:
原因:可能参数名不匹配或缺少setter方法 解决:
原因:前端和后端不在同一域名下 解决:
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Access-Control-Allow-Origin", "*");
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function callStrutsAction() {
$.ajax({
type: "POST",
url: "testAction.action",
data: {
name: $("#name").val(),
age: $("#age").val()
},
dataType: "json",
success: function(response) {
$("#result").html(response.result);
},
error: function(xhr, status, error) {
console.error(error);
}
});
}
</script>
</head>
<body>
<input type="text" id="name" placeholder="Name">
<input type="text" id="age" placeholder="Age">
<button onclick="callStrutsAction()">Submit</button>
<div id="result"></div>
</body>
</html>
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;
@Namespace("/")
@Result(name="success", type="json")
public class TestAction extends ActionSupport {
private String name;
private int age;
private String result;
@Action(value="testAction")
public String execute() {
this.result = "Hello " + name + ", you are " + age + " years old.";
return SUCCESS;
}
// Getters and Setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getResult() { return result; }
public void setResult(String result) { this.result = result; }
}
通过以上方法,你可以轻松地在Ajax中调用Struts2 Action方法,实现前后端的异步通信。
没有搜到相关的文章