jQuery的模态窗体和Struts 1.3窗体、模态、jQuery、Struts

2023-09-10 16:53:04 作者:念卿抚琴

我builing使用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模式窗体,并希望将表单数据发送到Struts的Action当用户presses创建新的场地使用AJAX。我怎么去的形式和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: 
});

(这是$ C $下我透过按钮模式窗体,我不知道如何将数据的方式为它是由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(它返回一个ActionForward或空)的执行方法

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

再次感谢! :)

推荐答案

一件事,如果你要的外部数据返回的ActionForward ,你必须返回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 的行动,以及结果为要发送的数据到客户端。

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上所有的参数。确保参数符合您的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.