user3541321 user3541321 - 2 months ago 6
Ajax Question

Not able to access ajax post request parameters in struts2 controller class

I am implementing simple login logic in my application. I have created a login.html and login.js files. In logins.js i am making a ajax post request and passing userId and password to struts controller class but both the values are null in my controller.

Below are my classes:

index.html

<form id="login-form" name="login-form" method="post" action="#">

<table class="table_signin" >
<!--email address table begins-->
<tr>
<th><label for="email" >Email Address</label></th>
<td><input type="text" id="email" name="email" class="input_width98 text" style="text-transform: lowercase"/></td>
</tr>

<tr>
<th><label for="password" >Password</label></th>
<td><input type="password" id="password" name="password" class="input_width98 text" autocomplete="off"/></td>
</tr>
<tr>
<td><input type="submit" name="login" id="login" value="Submit"/></td>
</tr>
</table>
</form>


MyLogin.js is:

$().ready(function() {
$("#login").click(function(){
var email = $("#email").val();
var password = $("#password").val();
if( email =='' || password =='')
{
$('input[type="text"],input[type="password"]')
$('input[type="text"],input[type="password"]')
alert("Please fill all fields...!!!!!!");
}
else{
var userInfo= {"userEmail":email , "userPassword":password};

alert("else part"+userInfo)
var login = $.ajax({
async: false,
cache: false,
url: "/nts-web/login/login.do",
data: userInfo,
dataType: 'json',
type: 'POST',
crossDomain: true,

success: function (msg) {
var res = msg;
alert("res"+res);

}

});
}
});
});


Now my struts.xml contains below configuration:

<package namespace="/login" name="login" extends="json-default">

<action name="login" class="com.warnerbros.nts.controller.impl.AdminLoginControllerImpl" method="login">
<interceptor-ref name="json" />
<interceptor-ref name="defaultStack" />

<result type="json">
<param name="root">result</param>
</result>
</action>
</package>


And finally my LoginController is :

public class AdminLoginControllerImpl extends AbstractControllerImpl implements AdminLoginController {

private static final long serialVersionUID = 1L;

@Autowired
private LoginService loginService;

private User userInfo;
public String login() {
System.out.println(userInfo.getUserEmailId());//Here it is throwing null pointer.
String res= "";
return res;
}
}


and I have a pojo User.java like this:

public class User {

private String userEmail;
private String userPassword;

public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}


How to get the values useremail and password in controller?

Answer

Fixed the issue by sending the proper json object like

var obj = {"userInfo":{"userEmail":email , "userPassword":password}};

and in struts.xml below is the configuration.

<package namespace="/login" name="login" extends="json-default">

  <interceptors>
        <interceptor-stack name="defaultStack">
            <interceptor-ref name="json">
                <param name="enableSMD">true</param>
            </interceptor-ref>
        </interceptor-stack>
    </interceptors>
    <default-interceptor-ref name="defaultStack" />
    <action name="login"
        class="com.warnerbros.nts.controller.impl.AdminLoginControllerImpl"
        method="login">
        <interceptor-ref name="defaultStack" />
            <result type="json">
            <param name="root">result</param>
        </result>
    </action>
</package>