user3541321 user3541321 - 1 year ago 67
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:


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

<table class="table_signin" >
<!--email address table begins-->
<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>

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

MyLogin.js is:

$().ready(function() {
var email = $("#email").val();
var password = $("#password").val();
if( email =='' || password =='')
alert("Please fill all fields...!!!!!!");
var userInfo= {"userEmail":email , "userPassword":password};

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

success: function (msg) {
var res = msg;



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>

And finally my LoginController is :

public class AdminLoginControllerImpl extends AbstractControllerImpl implements AdminLoginController {

private static final long serialVersionUID = 1L;

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 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 Source

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">

        <interceptor-stack name="defaultStack">
            <interceptor-ref name="json">
                <param name="enableSMD">true</param>
    <default-interceptor-ref name="defaultStack" />
    <action name="login"
        <interceptor-ref name="defaultStack" />
            <result type="json">
            <param name="root">result</param>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download