使用Struts 1.3的Jquery模态表单
我正在为一个类项目使用Struts 1.3构建一个Web应用程序,我在Struts 1.x的AJAX兼容性方面遇到了一些问题(我听说2.x在AJAX和jQuery方面更好)。
谢谢你的回复,这是更新的问题:
我目前在同一个jsp中使用jquery UI模式表单,并且当用户使用AJAX按下“创建新地点”时,想要将表单数据发送到Struts Action。 如何在表单和Struts操作之间发送(和检索)数据?
换句话说,之间的联系:
"Create new venue": function() { $.ajax({ url: "/registered/insertVenue.do", data: });
(这是我的模式forms的sumbit按钮的代码,我不知道如何以一种方式附加数据,以便Struts Action可以读取它)
和Struts Action的’execute’方法(返回ActionForward或null)。
再次感谢! 🙂
有一件事,如果你想在ActionForward
之外返回数据,你必须return null
。 当Struts看到一个null ActionForward
,它不会执行转发。
完成后,我使用以下类型设计在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(); } }
所有与AJAX相关的响应都将实现ResultBasedAction
以执行操作,并将Result
用于将数据发送到客户端。
在你的ajax上,你只需要做一个HTTP GET
,传递URL上的所有参数。 确保参数与Struts ActionForm
匹配所需的Action
类。
支持框架在原始JavaScript / jQuery / Ajax方面确实没有太大区别。
您可以从Struts 1操作中返回任何您想要的内容。 如果你想要一些状态或Flash消息的JSON,你可以直接将它写入响应并返回null
而不是ActionForward
,或者制作一个JSP来获得你想要的内容并设置一个合适的标题。
如何处理Ajax请求的返回值完全取决于客户端代码:Struts 1不关心它是什么类型的请求; 它会吐出任何配置为吐回来的东西。