It's been a month since I used Ofbiz.
I want to use JSON Array sent from Ajax. How does JAVA handle this? 1. controller.xml <request-map uri="CRUDList"> <security https="true" auth="true"/> <event type="service" invoke="CRUDList"/> <response name="success" type="request" value="json"/> <response name="error" type="request" value="json"/> </request-map> 2. service.xml <service name="CRUDList" default-entity-name="TestEntity" engine="java" auth="true" location="com.test.services.TestServices" invoke="CRUDList"> <attribute name="crudMode" mode="IN" type="String" optional="false"/> <attribute name="data" mode="INOUT" type="java.util.List" optional="true"/> </service> 3. crudList.ftl 4. service.java public static Map<String, Object> CRUDList(DispatchContext dctx, Map<String, ?> context) { Delegator delegator = dctx.getDelegator(); Map<String, Object> result = ServiceUtil.returnSuccess(); GenericValue userLogin = (GenericValue) context.get("userLogin"); List<String> reqData = UtilGenerics.checkList(context.get("data")); String str = reqData.get(0); JSONArray data = new JSONArray(); data = new JSONArray(str); ........... } **result = data.length ==> 1 When I take a JSONARray and size it, it's 1. I don't know why. I'd appreciate it if you could explain why. And then, Please let me know if there is an easier way to use Java by sending an arraylist to Ajax. -- Sent from: http://ofbiz.135035.n4.nabble.com/OFBiz-User-f135036.html |
Hello,
lsy <[hidden email]> writes: > It's been a month since I used Ofbiz. > I want to use JSON Array sent from Ajax. > How does JAVA handle this? > 1. controller.xml > <request-map uri="CRUDList"> > <security https="true" auth="true"/> > <event type="service" invoke="CRUDList"/> > <response name="success" type="request" value="json"/> > <response name="error" type="request" value="json"/> > </request-map> > > 2. service.xml > <service name="CRUDList" default-entity-name="TestEntity" engine="java" > auth="true" > location="com.test.services.TestServices" invoke="CRUDList"> > <attribute name="crudMode" mode="IN" type="String" > optional="false"/> > <attribute name="data" mode="INOUT" type="java.util.List" > optional="true"/> > </service> > > 3. crudList.ftl > > > 4. service.java > public static Map<String, Object> CRUDList(DispatchContext dctx, Map<String, > ?> context) { > Delegator delegator = dctx.getDelegator(); > Map<String, Object> result = ServiceUtil.returnSuccess(); > GenericValue userLogin = (GenericValue) context.get("userLogin"); > > List<String> reqData = UtilGenerics.checkList(context.get("data")); > String str = reqData.get(0); > JSONArray data = new JSONArray(); > data = new JSONArray(str); > > ........... > } > > **result = data.length ==> 1 > When I take a JSONARray and size it, it's 1. > I don't know why. > I'd appreciate it if you could explain why. > And then, Please let me know if there is an easier way to use Java by > sending an arraylist to Ajax. Unless I am overlooking something, you simply need to associate the key “data” with the Java list you want to be converted to a JSON list like this: result.put("data", *the java list you want to be converted in JSON*); return result; The <response name="success" type="request" value="json"/> is supposed to convert the service result map (plus other things) to JSON auto-magically. -- Mathieu Lirzin GPG: F2A3 8D7E EB2B 6640 5761 070D 0ADE E100 9460 4D37 |
Administrator
|
In reply to this post by lsy
Hi lsy,
Your message has been moderated. Please subscribe to the user ML for such questions and then use your email client See also why here http://ofbiz.apache.org/mailing-lists.html You will get a better support , it's more fair to share with everybody and people can answer you on the ML rather than directly to you The wider the audience the better the answers you might get Also it's more work for moderators who have to accept your messages as long as you have not subscribed. I'll personally no longer accept them (other moderators still could) Thanks Jacques Le 17/06/2019 à 19:58, lsy a écrit : > It's been a month since I used Ofbiz. > I want to use JSON Array sent from Ajax. > How does JAVA handle this? > 1. controller.xml > <request-map uri="CRUDList"> > <security https="true" auth="true"/> > <event type="service" invoke="CRUDList"/> > <response name="success" type="request" value="json"/> > <response name="error" type="request" value="json"/> > </request-map> > > 2. service.xml > <service name="CRUDList" default-entity-name="TestEntity" engine="java" > auth="true" > location="com.test.services.TestServices" invoke="CRUDList"> > <attribute name="crudMode" mode="IN" type="String" > optional="false"/> > <attribute name="data" mode="INOUT" type="java.util.List" > optional="true"/> > </service> > > 3. crudList.ftl > > > 4. service.java > public static Map<String, Object> CRUDList(DispatchContext dctx, Map<String, > ?> context) { > Delegator delegator = dctx.getDelegator(); > Map<String, Object> result = ServiceUtil.returnSuccess(); > GenericValue userLogin = (GenericValue) context.get("userLogin"); > > List<String> reqData = UtilGenerics.checkList(context.get("data")); > String str = reqData.get(0); > JSONArray data = new JSONArray(); > data = new JSONArray(str); > ........... > } > > **result = data.length ==> 1 > When I take a JSONARray and size it, it's 1. > I don't know why. > I'd appreciate it if you could explain why. > And then, Please let me know if there is an easier way to use Java by > sending an arraylist to Ajax. > > > > -- > Sent from: http://ofbiz.135035.n4.nabble.com/OFBiz-User-f135036.html > |
Free forum by Nabble | Edit this page |