Sending request to action using Ajax, the result type is json. Use a json plugin for struts2. The result is correct, but takes a very long time.
Below is the content of struts.xml:
<package name="FoodCompositionSearchajax" extends="json-default">
<action name="addSearchCondition" class ="com.fmOnline.action.FmzxMenuCompositionSearchAction" enter code here
method="addSearchCondition">
<result name="success" type="json">
<param name="excludeProperties">
showList\[\d+\]\.foodStr,showList\[\d+\]\.nourishStr,
showList\[\d+\]\.searchFunction,showList\[\d+\]\.num1,
showList\[\d+\]\.num2,showList\[\d+\]\.danwei,
showList\[\d+\]\.nourishname,showList\[\d+\]\.resultmap,
dataList, foodList, foodStr, map, nourishList, nourishStr,
num1, num2, paramList, searchDes, searchFun
</param>
</result>
</action>
</package>
The <param name="includeProperties">
does not work, so use <param name="excludeProperties">
instead.
The result json contains a list (showList, contains 2 String attributes, the number of elements varies from 1 to 6) and a String. I have alerted the result content from the javascript in the client page, and the <param name="excludeProperties">
works.
The json result is : {"msg":null, "showList":[{"id":1, "searchDes":"淀粉中的蛋白质含量>=11"}, {"id":2,"searchDes":"豆类中的蛋白质含量>=11"}, {"id":3, "searchDes":"禽类中的蛋白质含量>=11"}]}
At first, I used <result type="json"/>
. I though the cause of the long time may be a too large object. Then I tried to @JSON(serialize=false) to deserialize the get methods, so that these methods will not execute at the conversion process from java to json. Finally I used excludeProperties. They all succeed in returning smaller object but failed to shorten the time. The response time of a normal jsp page is good
Problem:
When running the project on the local host, the response time is short.
When running in the remote server, it takes above 10 seconds to response on the client side. Operating Database is really quick, it seems that it takes a long time to send the result from server to browser.
When the result is null, the response time is short, even from remote server.
How can I shorten the response time?
Based on the information you provided, I would first look at the JSON response size. You mentioned that when the response is null the response time is short. Either make the AJAX request directly in the browser or use a debugging tool like Firebug (for Firefox) to see the response. It is possible that the response is really large and that the slowdown is in marshalling the Java object into a JSON response.
If the JSON response is really large and you don't need all of the information coming back, you may find it easier to create an object that contains only the fields you need and then marshall that.
Update
Okay, we can rule out the response being too large. Is there a reason that you have such a large list of excludeProperties? You might want to try commenting that out and see if it helps. Otherwise, perhaps its your server that is slow?
I replace jsonplugin with json-lib-2.3-jdk15.jar, and the problem has settled. Really appreciate your help~ So happy now O(∩_∩)O~
来源:https://stackoverflow.com/questions/4478012/struts2jsonplugin-response-takes-a-long-time-to-display-in-browser-from-remote