【问题标题】:Action class getting null value from jsp page through ajax call动作类通过ajax调用从jsp页面获取空值
【发布时间】:2013-09-18 14:26:53
【问题描述】:

如何通过 Ajax 从动作类中获取 JSP 值?当我输入用户名时,要检查数据库用户名是否存在。为此,我正在使用 Ajax。

这是我的 JSP 代码:

 $(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

【问题讨论】:

    标签: java jquery ajax jsp struts2


    【解决方案1】:

    更改您的data:"username="+username,data: {userName:username} 查询文档中的示例

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

    http://api.jquery.com/jQuery.ajax/

    【讨论】:

    • 谢谢..但我想知道在 java 类中为什么我会得到用户名的空值
    • 改了userName后,你还面临null吗?
    • @user2653831: 它不应该...你可以尝试使用来自 ajax 的硬编码值,即 data: { userName: "test"} ,请检查你的 bean 属性名称是否应该与 data 中的 userName 匹配
    【解决方案2】:

    错字:data:"username="+username 应该是data:"userName="+username

    为避免此类错误,您也可以将 javascript 变量命名为 userName ;)

    【讨论】:

      【解决方案3】:
                 $.ajax({
                          type:"post",
                          url:"CheckUserAction",
                          data:{"username": username },
                          success:function(data){  
      
                          //checking the code for username is present in database or not
                          }
                 });
      

      【讨论】:

        【解决方案4】:

        试试

        $.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
          }
        });
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-03
            • 2015-11-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多