1

I have created an application in Struts 2 for displaying by iterating a JSONObject object taken from Struts2 session by using Struts2 iterator like as shown below

<s:iterator var="menu" value="userMenu.get('permissions')">
   <s:property value="menu.name"/>
</s:iterator>

but I am getting nothing printed

Is there any other alternatives if it is not possible by Struts2 iterator?

my JSON is shown below

{
  "id": 10,
  "groupName": "manager",
  "permissions": [
    {
      "id": 18,
      "name": "Group 1",
      "modules": [
        {
          "id": 6,
          "name": "Company 1",
          "groupId": 18,
          "stat": true,
          "moduleGrpName": "Group 1"
        },
        {
          "id": 8,
          "name": "Company 2",
          "groupId": 18,
          "stat": true,
          "moduleGrpName": "Group 1"
        }
      ],
      "stat": "false"
    },
    {
      "id": 19,
      "name": "Group 2",
      "modules": [
        {
          "id": 17,
          "name": "Company 3",
          "groupId": 19,
          "stat": true,
          "moduleGrpName": "Group 2"
        },
        {
          "id": 15,
          "name": "Company 4",
          "groupId": 19,
          "stat": true,
          "moduleGrpName": "Group 2"
        }
      ],
      "stat": "false"
    }
  ]
}

EDIT:

 public String validateLogin() throws Exception 
    {
        TableUser flag=hobjLoginauth.checkUsercredentials(userName, password);
        System.out.println("Flag :"+flag);
        if(flag == null!=true)
        {
            sessionmap.remove("logincheck");
            setSession(sessionmap);
            sessionmap.put("userId", flag.getId());
            sessionmap.put("user",flag.getUsername());
            sessionmap.put("userMenu", new JSONObject(userModulesServicesImpl.getDeSerialized(flag.getGroupId().getPermissions())));
            sessionmap.put("menuGroups",userModulesServicesImpl.getUserModules());
            return SUCCESS;
        }else{
            sessionmap.put("logincheck","1");
            return ERROR;
        }

    }
Roman C
  • 49,761
  • 33
  • 66
  • 176
Alex Man
  • 4,746
  • 17
  • 93
  • 178
  • If it's a *string* then iterating it is (essentially) meaningless. Iterate over a *data structure*, like an array. You'll need to provide more information. – Dave Newton Sep 21 '14 at 16:54
  • i made it as a JSONObject, now can we do iteration using – Alex Man Sep 21 '14 at 16:58

1 Answers1

1

You can use the following getter for JSON

public Map getUserMenu(){     
  return JSONObject.fromObject(json);
}

also you can do the same using Struts2 utility

public Map getUserMenu(){
  Map res = null;
  try {
    Object o = JSONUtil.deserialize(json);
    res = (Map)o;
  } catch (JSONException e) {
    e.printStackTrace();
  }
  return res;
}

and iterate a map via

<s:iterator var="menu" value="userMenu.get('permissions')">
  <s:property value="#menu.name"/>
</s:iterator>

Note that using variable of the iterator is not necessary but if used then it accessed from the value stack context using #.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • which json library is this – Alex Man Sep 21 '14 at 17:50
  • json-lib-2.3-jdk15.jar – Roman C Sep 21 '14 at 17:52
  • This library comes with struts2-json-plugin, if you are using this plugin, then it uses that library as a dependency. – Roman C Sep 21 '14 at 17:56
  • i have upadted my answer , there i am already calling a method `validateLogin` upon which my view is shown, so how can i use the above said method – Alex Man Sep 21 '14 at 18:10
  • If you want to know how to work with session see [this](http://stackoverflow.com/a/24790578/573032) or [that](http://stackoverflow.com/a/17094765/573032) answer and upvote it if helped you. – Roman C Sep 21 '14 at 19:28
  • can you suggest some solution for this http://stackoverflow.com/questions/25975679/struts2-inject-not-working-in-jersey-rest-webservices – Alex Man Sep 22 '14 at 14:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61687/discussion-between-alex-man-and-roman-c). – Alex Man Sep 22 '14 at 14:15