Java method never called while using JSON RPC in Struts2
-
03-07-2019 - |
Question
I'm trying to get some code working that a previous developer has written. Yep, he now left the company. :-(
I have a JSON RPC call being made from the JS code. The JS all runs fine and the callback method gets an object back (not an error object).
But the method on the Java class never gets hit. The smd method does get hit though.
public String smd()
{
return SUCCESS; // break point reaches here
}
@SMDMethod
public void updateRowValueForField(String key, String value, String fieldname)
{
// We never get into this method.
}
<package name="EntryBarRPC" namespace="/" extends="star-default">
<action name="ebToggleSelection" class="eboggleSelectionAction" method="smd">
<interceptor-ref name="jsonStack">
<param name="enableSMD">true</param>
</interceptor-ref>
<result type="json">
<param name="enableSMD">true</param>
</result>
</action>
</package>
I'm stumped as to why, or what I'm missing. I've read JSON plugin page over and over.
I think I just need another set of eyes.
Note: no errors in the Tomcat console, no JS errors.
Anyone got any clues? Cheers Jeff Porter
Solution
You forgot to include the javascript code. From the example:
<s:url id="smdUrl" namespace="/nodecorate" action="SMDAction" />
<script type="text/javascript">
//load dojo RPC
dojo.require("dojo.rpc.*");
//create service object(proxy) using SMD (generated by the json result)
var service = new dojo.rpc.JsonService("${smdUrl}");
//function called when remote method returns
var callback = function(bean) {
alert("Price for " + bean.type + " is " + bean.price);
};
//parameter
var bean = {type: "Mocca"};
//execute remote method
var defered = service.doSomething(bean, 5);
//attach callback to defered object
defered.addCallback(callback);
</script>
Are you sure you call service.updateRowValueForField(key, value, fieldname) and not something different?
Further, your method returns a void (e.g. doesn't return anything). What did you expect to get?
OTHER TIPS
New version fixes my problems.
I'm guessing that you need to update the smd()
method to actually call updateRowValueForField()
rather than simply return immediately. Looks like the previous developer never actually hooked up the methods.