I'm builing a web application using Struts 1.3 for a class project, and I'm having some problems with the AJAX compatibility of Struts 1.x (I hear 2.x is way better with AJAX and jQuery).
我正在为一个类项目使用Struts 1.3构建一个Web应用程序,我在Struts 1.x的AJAX兼容性方面遇到了一些问题(我听说2.x在AJAX和jQuery方面更好)。
Thank you for the reply, this is the updated problem:
谢谢你的回复,这是更新的问题:
I'm currently using a jquery UI modal form in the same jsp, and want to send the form data to a Struts Action when the user presses "create new venue" using AJAX. How do I go about sending (and retrieving) the data between the form and the Struts action?
我目前在同一个jsp中使用jquery UI模式表单,并且当用户使用AJAX按下“创建新地点”时,想要将表单数据发送到Struts Action。如何在表单和Struts操作之间发送(和检索)数据?
In other words, the connection between:
换句话说,之间的联系:
"Create new venue": function() {
$.ajax({
url: "/registered/insertVenue.do",
data:
});
(this is the code for my sumbit button for the modal form, I don't know how to attach the data in a way for it to be readable by the Struts Action)
(这是我的模式形式的sumbit按钮的代码,我不知道如何以一种方式附加数据,以便Struts Action可以读取它)
and the 'execute' method of the Struts Action (which returns an ActionForward or null).
和Struts Action的'execute'方法(返回ActionForward或null)。
Thanks again! :)
再次感谢! :)
2 个解决方案
#1
4
One thing, if you want to return data outside of an ActionForward
, you must return null
. When Struts sees a null ActionForward
, it doesn't execute the forward.
有一件事,如果你想在ActionForward之外返回数据,你必须返回null。当Struts看到一个null ActionForward时,它不会执行转发。
Once done, the following type design is what I used to create a JSON Response in Struts:
完成后,我使用以下类型设计在Struts中创建JSON响应:
public interface Result {
public void applyResult(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
public abstract class ResultBasedAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
Result result = execute(mapping, form, request);
if (result == null) {
throw new Exception("Result expected.");
}
result.applyResult(request, response);
//Finally, we don't want Struts to execute the forward
return null;
}
public abstract Result execute(ActionMapping mapping, ActionForm form, HttpServletRequest request) throws Exception;
}
public class JsonResult implements Result {
private JSONObject json;
public JsonResult(JSONObject json) {
this.json = json;
}
public void applyResult(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.addHeader("Content-Type", "application/json");
response.getOutputStream().write(json.toString().getBytes("UTF-8"));
response.getOutputStream().flush();
}
}
All your AJAX related responses will implement the ResultBasedAction
for action, and a Result
for the data to be sent to the client.
所有与AJAX相关的响应都将实现ResultBasedAction以执行操作,并将结果用于将数据发送到客户端。
On your ajax, you will just have to do an HTTP GET
, passing all parameters on the URL. Make sure that the parameters matches your Struts ActionForm
for the required Action
class.
在你的ajax上,你只需要做一个HTTP GET,传递URL上的所有参数。确保参数与Struts ActionForm匹配所需的Action类。
#2
3
The backing framework really doesn't make much of a difference in terms of raw JavaScript/jQuery/Ajax.
支持框架在原始JavaScript / jQuery / Ajax方面确实没有太大区别。
You can return whatever you want from your Struts 1 action. If you want some JSON back like with a status or Flash message you can either write it directly to the response and return null
instead of an ActionForward
, or craft a JSP to have the content you want and set an appropriate header.
您可以从Struts 1操作中返回任何您想要的内容。如果你想要一些状态或Flash消息的JSON,你可以直接将它写入响应并返回null而不是ActionForward,或者制作一个JSP来获得你想要的内容并设置一个合适的标题。
How the return value of the Ajax request is handled is all up to the client-side code: Struts 1 doesn't care what type of request it is; it will just spit back whatever it's configured to spit back.
如何处理Ajax请求的返回值完全取决于客户端代码:Struts 1不关心它是什么类型的请求;它会吐出任何配置为吐回来的东西。
#1
4
One thing, if you want to return data outside of an ActionForward
, you must return null
. When Struts sees a null ActionForward
, it doesn't execute the forward.
有一件事,如果你想在ActionForward之外返回数据,你必须返回null。当Struts看到一个null ActionForward时,它不会执行转发。
Once done, the following type design is what I used to create a JSON Response in Struts:
完成后,我使用以下类型设计在Struts中创建JSON响应:
public interface Result {
public void applyResult(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
public abstract class ResultBasedAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
Result result = execute(mapping, form, request);
if (result == null) {
throw new Exception("Result expected.");
}
result.applyResult(request, response);
//Finally, we don't want Struts to execute the forward
return null;
}
public abstract Result execute(ActionMapping mapping, ActionForm form, HttpServletRequest request) throws Exception;
}
public class JsonResult implements Result {
private JSONObject json;
public JsonResult(JSONObject json) {
this.json = json;
}
public void applyResult(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.addHeader("Content-Type", "application/json");
response.getOutputStream().write(json.toString().getBytes("UTF-8"));
response.getOutputStream().flush();
}
}
All your AJAX related responses will implement the ResultBasedAction
for action, and a Result
for the data to be sent to the client.
所有与AJAX相关的响应都将实现ResultBasedAction以执行操作,并将结果用于将数据发送到客户端。
On your ajax, you will just have to do an HTTP GET
, passing all parameters on the URL. Make sure that the parameters matches your Struts ActionForm
for the required Action
class.
在你的ajax上,你只需要做一个HTTP GET,传递URL上的所有参数。确保参数与Struts ActionForm匹配所需的Action类。
#2
3
The backing framework really doesn't make much of a difference in terms of raw JavaScript/jQuery/Ajax.
支持框架在原始JavaScript / jQuery / Ajax方面确实没有太大区别。
You can return whatever you want from your Struts 1 action. If you want some JSON back like with a status or Flash message you can either write it directly to the response and return null
instead of an ActionForward
, or craft a JSP to have the content you want and set an appropriate header.
您可以从Struts 1操作中返回任何您想要的内容。如果你想要一些状态或Flash消息的JSON,你可以直接将它写入响应并返回null而不是ActionForward,或者制作一个JSP来获得你想要的内容并设置一个合适的标题。
How the return value of the Ajax request is handled is all up to the client-side code: Struts 1 doesn't care what type of request it is; it will just spit back whatever it's configured to spit back.
如何处理Ajax请求的返回值完全取决于客户端代码:Struts 1不关心它是什么类型的请求;它会吐出任何配置为吐回来的东西。