0

Your site had helped me a lot in java web development. My only hindrance is Populating Dropdown inside javascript through struts2 action class (using jtable). I have used your sample code "AJAX based CRUD operation in Struts 2 using jTable plugin".

In below code snippet i want Department to be dropdown which gets value from database using list2 method of com.action.JtableAction class.

userDefinedJtable.js:

 department : {
            title : 'Department',
            width : '30%',
            edit : true,
                            options : '/WebApplication1/list2Action',
                            list: false
        },

list2 method inside JtableAction

public String list2() {
            JSONObject obj = new JSONObject();

      obj.put("name", "foo");
      obj.put("num", new Integer(100));
      obj.put("balance", new Double(1000.21));
      obj.put("is_vip", new Boolean(true));

      return obj.toString();    
    }

Declaration in struts.xml

<action name="getJSONResult" class="com.action.JtableAction" method="list2">
            <result type="json" />
</action>

But when i accessed

 /WebApplication1/list2Action

i am getting this error

HTTP Status 404 - No result defined for action com.action.JtableAction and result {"balance":1000.21,"num":100,"is_vip":true,"name":"foo"}

Kindly help me whether i am doing right thing or not. And what is required.

EDIT

struts.xml

<struts>
    <package name="default" extends="json-default">
        <action name="*Action" class="com.action.JtableAction" method="{1}">
            <result type="json">/jTable.jsp</result>
        </action>
        <action name="getJSONResult" class="com.action.JtableAction" method="list">
            <result type="json" />
        </action>
        <action name="getJSONResult" class="com.action.JtableAction" method="list2">
            <result type="json">
                <param name="root">obj</param>
            </result>
        </action>
    </package>
</struts>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Hirak
  • 9
  • 5
  • possible duplicate of [jQuery Ajax - issue returning JSON value](http://stackoverflow.com/questions/17093862/jquery-ajax-issue-returning-json-value) – Roman C Jun 20 '14 at 09:22

1 Answers1

0

Assuming you are using Struts2-JSON-plugin,

  1. Make your object private at class level, providing a getter for it:

    private JSONObject obj;
    public getJSONObject() { 
        return obj; 
    }
    
  2. Change your return type to SUCCESS:

    public String list2() {
      obj = new JSONObject();
    
      obj.put("name", "foo");
      obj.put("num", new Integer(100));
      obj.put("balance", new Double(1000.21));
      obj.put("is_vip", new Boolean(true));
    
      return SUCCESS;
    }
    
  3. Add a root object parameter to your result definition :

    <action name="getJSONResult" class="com.action.JtableAction" method="list2">
        <result type="json">
            <param name="root">obj</param>
        </result>
    </action>
    

Read more about how it works here.

EDIT

You have at least two big errors in your struts.xml

  1. If you are returning a result of type JSON, you can't return a JSP; remove JSON from your first result.
  2. You can't name two action aliases with the same name in the same package: differentiate the names to make it work.

Fixed code:

<struts>
    <package name="default" extends="json-default">
        <action name="*Action" class="com.action.JtableAction" method="{1}">
            <result>/jTable.jsp</result>
        </action>
        <action name="getJSONResult1" class="com.action.JtableAction" method="list">
            <result type="json" />
        </action>
        <action name="getJSONResult2" class="com.action.JtableAction" method="list2">
            <result type="json">
                <param name="root">obj</param>
            </result>
        </action>
    </package>
</struts>

Obviously you need to change the name of the getJSONResult call adding 1 or 2 at the end.

P.S: Consider using different action classes, to get smaller actions and more granularity.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I have tried the steps you have provided. Thanks for your help. But when I have submitted [link] (http://localhost:9090/WebApplication1/list2Action) I got following {"JSONObject":{},"department":null,"emailId":null,"message":null,"name":null,"record":null,"records":null,"result":"OK","studentId":0} – Hirak Jun 25 '14 at 10:44
  • @user3756876 are you using struts2-json-plugin ? Is your `` extending `json-default` ? Is `JSONOBject` a JavaBean created by yourself ? P.S: It should be List2.action, with the dot – Andrea Ligios Jun 25 '14 at 12:29
  • my struts.xml 'code' /jTable.jsp obj – Hirak Jun 26 '14 at 05:00
  • Next time edit your own question instead of posting code in comments. BTW your struts.xml is completely wrong – Andrea Ligios Jun 26 '14 at 08:16