Hello,
lsy <[email protected]> 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