jquery 在Struts 2中使用Ajax时如何传递属性

iq3niunx  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(102)

使用Struts 2与Ajax时,我试图提交表单的属性没有传递给动作类,因此我得到null

我的JSP:

<head>
    <title>Login</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="publicHtml/css/bootstrap.min.css">
    <script src="publicHtml/js/jquery.min.js"></script>
    <script src="publicHtml/js/bootstrap.min.js"></script>
    <script>
        var contextName = "<%=request.getContextPath()%>";
    </script>
    <script src="publicHtml/js/automation.js"></script>
</head>
<body>
    <form action="loginAction" id="myForm" method="post">
        <div class="container-fluid">
            <div class="jumbotron">
                LOG CREATION
            </div>
        </div>

        <div class="row" style="height: 50px">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="text-align: center">
                <mark style="color: red;display: none;" id="errorMessage"></mark>
            </div>
        </div>
        <div class="row"> 
            <div class="col-lg-7 col-md-7 col-sm-5 col-xs-5">
            </div>
            <div class="col-lg-3 col-md-3 col-sm-6 col-xs-6">
                <div id="loginJSP">
                    <div class="form-group">
                        <input type="text"  class="form-control" placeholder="User Name" id="userName" name="userName">
                    </div>
                    <div class="form-group">
                        <input type="password"  class="form-control" placeholder="Password" id="password" name="password">
                    </div>
                    <input type="button" onclick="validLogin()"  value="Login">
                </div>
            </div>
            <div class="col-lg-2 col-md-2 col-sm-1 col-xs-1">

            </div>
        </div>
    </form>
</body>

我的Action

public class LoginAction extends ActionSupport {
String forward = ERROR;
public LoginAction() {
}

/**
 *
 * @return @throws Exception
 */
@Override
public String execute() throws Exception {
    
    System.err.println("Assigned Property is  "+userName);
    forward = null;
    return forward;
}


private String userName=null,password=null;
public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}}

我的JS是:

function submitForm(actionName, commandName) {

var frm = document.forms[0];
var url = contextName + "/" + actionName + "?command=" + commandName;
frm.action = url;
frm.submit();
}
function validLogin() {
alert('hi');
var userName = document.getElementById('userName').value;
var password = document.getElementById('password').value;
document.getElementById('errorMessage').innerHTML = "";
$('#errorMessage').hide();
 alert('hi');
if (userName == '' && password == '') {
    $('#errorMessage').append("USERNAME AND PASSWORD CANT BE BLANK");
    $('#errorMessage').show();
} else {
    $.ajax({
        type: "POST",
        url: "./loginAction",
        data: "command=loginValidation",
        beforeSend: function () {
        },
        success: function (response) {
  },
        error: function (e) {
            alert('hi error');
        }
    });
}

我给出的输入是AAA

预期输出应为:

Assigned Property is AAA

我的输出是:

Assigned Property is null
oxcyiej7

oxcyiej71#

在验证后,您没有在aps调用中发送参数。如果你使用下面的代码,这个问题可以解决:

$.ajax({
    type: "POST",
    url: "./loginAction",
    data: {command: "loginValidation", userName: userName, password: password},
    beforeSend: function () {
    },
    success: function (response) {
    },
    error: function (e) {
        alert('hi error');
    }
});

相关问题