0

I have the following action, I want to pass name to the ajax response. How could I do it ?

public class DatabaseAction extends ActionSupport{

    private String name;


    public void selectAll() {
        name="aa";

    }
    public void update() {

    }
    public void delete() {

    }
    public void add() {

    }

    public String getName() { return name; }

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

I am using JQuery to write the ajax:

$(document).ready(function(){
    $("#select").click(function() {
        $.ajax({
            url:"/db/database!selectAll.action",
            type:"POST",
            dateType:"JSON",
            success:function(name){
                alert(name);
            }
        });
    });
    $("#delete").click(function() {
        alert("delete");
    });
});

alert is invoked, but empty result is shown.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Xuzheng Wang
  • 531
  • 5
  • 17
  • Possible duplicate of [JQuery Ajax not working in JSP with Struts](http://stackoverflow.com/questions/34939790/jquery-ajax-not-working-in-jsp-with-struts) – Aleksandr M Jan 22 '16 at 09:08

1 Answers1

0

If you invoke an action method, this method should return String value as a result. This result should have a name and should be configured to the action config or configured globally. You can read this answer to learn more how to configure global results. Local results should be configured to the action config. See this answer if you need more information about local results.

The code:

public String selectAll() {
    name="aa";
    return SUCCESS;
}

The configuration:

<action name="database" class="com.example.DatabaseAction" method="selectAll">
   <result type="json"/>
</action>
Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176