Action类通过AASPACK调用从JSP页面获取空值

83qze16e  于 2023-10-14  发布在  其他
关注(0)|答案(5)|浏览(125)

如何通过Ajax从action类中获取JSP值?当我输入用户名要检查数据库的用户名是否存在。为此,我使用Ajax。
下面是我的JSP代码:

<script>    
      $(document).ready(function(){ 
      $("#username").change(function(){
      $("#message").html("checking...");    
      $("#message").css('color', 'red');
                            
                    var username=$("#username").val();
                    alert(username);
            
                    $.ajax({
                        type:"post",
                        url:"CheckUserAction",
                        data:"username="+username,
                        success:function(data){                        

//checking the code for username is present in database or not
                           }
        });
    });
    </script>
</head>

<body>
<%@include file="index.html" %>
 <div class="box2">
    <center>
    <!-- <div class="header"></div> -->
    <center>
        <div class="outer"><center>
        <br /><br /><br />
        <div class="inner"><br /><br /><br />
        <center> 
            <s:form action="RegisterationProcess" id="loginForm"  name="loginForm" method="post" >
                <table width="auto" border="0" align="center" cellpadding="2" cellspacing="0" class="tab">

                <tr>
                    <td colspan="2"><center><b>Registration Form</b></center></td>
                </tr>
                
                <tr>
                    <td colspan="2"><hr></td>
                </tr> 
                
                <input type="hidden" name="register" value="register"/>      
                <tr>     
                    <td><s:textfield name="user.userName"class="textfield" id="login" label="Name"/></td>
                </tr>
    
                <tr>  
                    <td><s:textfield name="user.userId" id="username"  class="textfield" label="User Name" /> </td> 
                    <div id="message"></div>        
                </tr>`enter code here`
   
                <tr>  
                    <td><s:password name="user.password"  class="textfield" id="password" label="Password" /></td>
                </tr>
            
                <tr>
                    <td><s:textfield name="user.emailId" class="textfield" id="emailAddress" label="Email" /></td>
                </tr>
                
                <tr>
                <td></td>
                <td><input id="sbmt" type="submit" value="Register"></td>
                <!-- <td><s:submit value="Register"  id="sbmt"/></td> -->
                </tr>

这是我的CheckUserAction类:

private String userId;
    private String userName;
   
  public UserImpl getUser()
    {
        return user;
    }

public void setUser(UserImpl user) 
{
    this.user = user;
}

public String getUserName() 
{
    return userName;
}

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

public String getUserId()
{
    return userId;
}

public void setUserId(String userId) 
{
    this.userId = userId;
}

public String execute()
{
  System.out.println("username"+userName);
}
}

我的用户名是null

57hvy0tb

57hvy0tb1#

data:"username="+username,更改为data: {userName:username}
参见example from the documentation of jQuery

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
qcbq4gxm

qcbq4gxm2#

排印错误:data:"username="+username应为data:"userName="+username
为了避免这类错误,你也可以将JavaScript变量命名为userName;)

3yhwsihp

3yhwsihp3#

$.ajax({
                    type:"post",
                    url:"CheckUserAction",
                    data:{"username": username },
                    success:function(data){  

                    //checking the code for username is present in database or not
                    }
           });
cclgggtu

cclgggtu4#

尝试

$.ajax({
  type:"post",
  url:"<s:url action='CheckUserAction'/>",
  data: { userName: username }
  success:function(data){                            
     //checking the code for username is present in database or not
  }
});
8fsztsew

8fsztsew5#

使用data: {"username":username},代替data:"username="+username,

相关问题