带有 Struts 1.3 的 Jquery 模态表单

2023-06-21前端开发问题
1

本文介绍了带有 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 模态表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

Rails 3.1 ajax:成功处理
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)...
2024-04-20 前端开发问题
11

ExecJS::ProgramError: SyntaxError: 保留字“function"
ExecJS::ProgramError: SyntaxError: Reserved word quot;functionquot;(ExecJS::ProgramError: SyntaxError: 保留字“function)...
2024-04-20 前端开发问题
13