有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要实现struts2表单验证,这是已经加载的jsp的一部分

我正在使用struts2和tiles框架。 在默认加载中,我加载的是baselayout。jsp。在tat之后,我不会更改url。它将保持不变,内部部分将使用ajax查询进行更改。 现在我需要实现struts表单。Struts在尝试重新加载整个页面的成功和失败后形成操作。我只需要重新加载表单部分,而不是整个页面

另一个问题:在返回struts操作后,是否可以在javascript中触发函数

提前谢谢 克里希纳


共 (1) 个答案

  1. # 1 楼答案

    我希望下面的答案能对你有所帮助

     $('#formEmp').submit(function(event) {
                  //Validate using jquery validator
                    if (!$('#formEmp').valid()) {
                        return false;
                    }
    
                    // abort any pending request
                    if (request) {
                        request.abort();
                    }
                    // setup some local variables
                    var $form = $(this);
                    // let's select and cache all the fields
                    var $inputs = $form.find("input, select, button, textarea");
                    $inputs.prop("disabled", false);
                    // serialize the data in the form
                    var serializedData = $form.serialize();
                    // let's disable the inputs for the duration of the ajax request
                    // Note: we disable elements AFTER the form data has been serialized.
                    // Disabled form elements will not be serialized.
                    $inputs.prop("disabled", true);
    
                    // fire off the request to /form.php
                    request = $.ajax({
                        url: "saveEmp",
                        type: "post",
                        data: serializedData
                    });
    
                    // callback handler that will be called on success
                    request.done(function(response, textStatus, jqXHR) {
                        // log a message to the console
                        alert("Saved Successfully");
                        initForm();
                    });
    
                    // callback handler that will be called on failure
                    request.fail(function(jqXHR, textStatus, errorThrown) {
                        // log the error to the console
                        alert(textStatus);
                    });
    
                    // callback handler that will be called regardless
                    // if the request failed or succeeded
                    request.always(function() {
                        // reenable the inputs
                        $inputs.prop("disabled", false);
                    });
    
                    // prevent default posting of form
                    event.preventDefault();
    
                });