1

I am trying to send AJAX request to Action in which I am sending one parameter from JSP page. So my Action class is receiving the Ajax request but params which I am sending with AJAX class are null in Action class.

This is My Action Class:

 public class AjaxAction{

String name;
private  String welcomeMessage;

public String execute(){

    System.out.println("AJax called "+name);
    welcomeMessage="Hello"+name;
    return "success";
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getWelcomeMessage() {
    return welcomeMessage;
}

public void setWelcomeMessage(String welcomeMessage) {
    this.welcomeMessage = welcomeMessage;
}
 }

This is my struts.xml file:

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     "http://struts.apache.org/dtds/struts-2.0.dtd">
 <struts>

 <constant name="struts.devMode" value="true" />
    <package name="json" namespace="/" extends="json-default">
    <interceptors>
        <interceptor-stack name="defaultStack">
            <interceptor-ref name="json">
                <param name="enableSMD">true</param>
            </interceptor-ref>
        </interceptor-stack>
    </interceptors>
    
            <action name="ajaxAction" class="com.action.AjaxAction">
                <result type="json"/>
            </action>
    </package>

This is my Jsp file:

<title>Struts Ajax Example</title>
<script type="text/javascript">
    function ajaxStruts() {
        data = {
                name:$("#name").val(),
            };

            alert(data);
            $.ajax({
                  type: 'GET',
                  contentType:'application/x-www-form-urlencoded',
                  url:'ajaxAction',
                  data: $("#ajaxform").serialize(),
                  success: function(data){
                    alert(data);
                  }
                
            });
        
    }
</script>

 </head>
 <body>

<fieldset>
    <form method="POST" id="ajaxform">
        Name::<input type="text" id="name" name="name"> 
         <input type="button" name="submit"
            value="submit" onclick="return ajaxStruts();">
    </form>
</fieldset>
<fieldset>
    <div id="ajaxResult"></div>
</fieldset>
 </body>
 </html>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Hitesh
  • 59
  • 4
  • 15

2 Answers2

3

The problem is with your interceptors in struts.xml.

Either remove them or specify them properly. You have override defaultstack Which is not good practice.

You should make your interceptor-stack's name as custom name different from struts stacks.

For Example, name your stack as mystack.

 <interceptors>
    <interceptor-stack name="mystack">
        <interceptor-ref name="defaultStack"/>
        <interceptor-ref name="json">
            <param name="enableSMD">true</param>
        </interceptor-ref>
    </interceptor-stack>
</interceptors>

This means all the interceptors of defaultstack + json interceptor. This will be packed in interceptor stack named as mystack

prem30488
  • 2,828
  • 2
  • 25
  • 57
  • Thanks @parth ...it is working! Can you provide me some tutorial link for reference – Hitesh Jun 30 '14 at 05:33
  • yes sure... for struts2 [Click here](http://www.javatpoint.com/struts-2-tutorial) and For json example [See this example](http://www.mkyong.com/struts2/struts-2-and-json-example/) .. And if you are new user to stack overflow then I will suggest you to learn how to accept answer [Click here](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – prem30488 Jun 30 '14 at 05:39
  • @Hitesh Refer this link for json and struts2. http://tech.learnerandtutor.com/send-json-object-to-struts-2-action-by-jquery-ajax/ http://tech.learnerandtutor.com/read-json-object-from-struts-2-action-by-jquery-ajax/ – Rajeshkumar Jul 02 '14 at 04:21
1

If you want to create a custom interceptor stack and make it as default interceptor stack you can do it in the following way but putting json interceptor in front of your stack, however it gives you nothing unless you send data in json format.

<interceptors>
    <interceptor-stack name="jsonDefaultStack">
        <interceptor-ref name="json">
            <param name="enableSMD">true</param>
        </interceptor-ref>
        <interceptor-ref name="defaultStack"/>
    </interceptor-stack>
</interceptors>

<default-interceptor-ref name="jsonDefaultStack" />

The default stack of interceptors contains a functionality to populate your action bean with parameters that you should send as POST request.

function ajaxStruts() {
    $.ajax({
          type: "POST",
          contentType: "application/x-www-form-urlencoded",
          url: "<s:url action='ajaxAction' namespace='/'/>",
          dataType: "json",
          data: $("#ajaxform").serialize(),
          success: function(data){
            alert(data);
          }
    });
}
Roman C
  • 49,761
  • 33
  • 66
  • 176