带有 Struts 1.3 的 Jquery 模态表单 [英] Jquery Modal Forms with Struts 1.3

查看:9
本文介绍了带有 Struts 1.3 的 Jquery 模态表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个类项目使用 Struts 1.3 构建一个 Web 应用程序,但我在 Struts 1.x 的 AJAX 兼容性方面遇到了一些问题(我听说 2.x 使用 AJAX 和 jQuery 更好).

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).

感谢您的回复,这是更新后的问题:

Thank you for the reply, this is the updated problem:

我目前在同一个 jsp 中使用 jquery UI 模态表单,并希望在用户使用 AJAX 按下创建新场所"时将表单数据发送到 Struts Action.如何在表单和 Struts 操作之间发送(和检索)数据?

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?

换句话说,之间的联系:

In other words, the connection between:

"Create new venue": function() {
$.ajax({
    url: "/registered/insertVenue.do",
    data: 
});

(这是我的模态表单的 sumbit 按钮的代码,我不知道如何以某种方式附加数据以便 Struts Action 可以读取它)

(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)

以及 Struts Action 的 'execute' 方法(返回 ActionForward 或 null).

and the 'execute' method of the Struts Action (which returns an ActionForward or null).

再次感谢!:)

推荐答案

有一点,如果你想在ActionForward之外返回数据,你必须return null.当 Struts 看到一个空的 ActionForward 时,它不会执行转发.

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.

完成后,我使用以下类型设计在 Struts 中创建 JSON 响应:

Once done, the following type design is what I used to create a JSON Response in Struts:

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 用于发送到客户端的数据.

All your AJAX related responses will implement the ResultBasedAction for action, and a Result for the data to be sent to the client.

在您的 ajax 上,您只需要执行一个 HTTP GET,并在 URL 上传递所有参数.确保参数与所需的 Action 类的 Struts ActionForm 匹配.

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.

这篇关于带有 Struts 1.3 的 Jquery 模态表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆