ajax - Populating Dropdown inside javascript from database through struts2 action class (using jtable) -
your site had helped me lot in java web development. hindrance populating dropdown within javascript through struts2
action class (using jtable). have used sample code "ajax based crud operation in struts 2 using jtable plugin"
.
in below code snippet want section dropdown gets value database using list2
method of com.action.jtableaction
class.
userdefinedjtable.js
section : { title : 'department', width : '30%', edit : true, options : '/webapplication1/list2action', list: false },
list2
method within 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)); homecoming obj.tostring(); }
declaration in struts.xml
<action name="getjsonresult" class="com.action.jtableaction" method="list2"> <result type="json" /> </action>
but when accessed
/webapplication1/list2action
i getting error
http status 404 - no result defined action com.action.jtableaction , result {"balance":1000.21,"num":100,"is_vip":true,"name":"foo"}
kindly help me whether doing right thing or not. , 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>
assuming using struts2-json-plugin,
make object private @ class level, providing getter it:
class="lang-java prettyprint-override">private jsonobject obj; public getjsonobject() { homecoming obj; }
change homecoming type success:
class="lang-java prettyprint-override">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)); homecoming success; }
add root
object parameter result definition :
<action name="getjsonresult" class="com.action.jtableaction" method="list2"> <result type="json"> <param name="root">obj</param> </result> </action>
read more how works here.
edit
you have @ to the lowest degree 2 big errors in struts.xml
if returning result of type json, can't homecoming jsp; remove json first result. you can't name 2 action aliases same name in same package: differentiate names create 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 need alter name of getjsonresult
phone call adding 1 or 2 @ end.
p.s: consider using different action classes, smaller actions , more granularity.
ajax database drop-down-menu struts2 jtable
No comments:
Post a Comment