Search code examples
jsonjspstruts2ognl

JSON object parsing using OGNL in struts 2


I have an action class like

public class DataProcessor extends ActionSupport{

    private JSONObject object;

    public JSONObject getObject() {
        return object;
    }

    public void setObject(JSONObject object) {
        this.object = object;
    }

    @Override
    public String execute() throws Exception {
        .......
        return SUCCESS;
    }
} 

My XML mapping is like

<package name="default" extends="struts-default" namespace="/">
   <action name="process" class="com.demo.DataProcessor">
      <result type="success">home.jsp</result>
   </action>
</package>

on jsp page if i write <s:property value="object"/> it prints json data. bt if i write

<s:property value="object.name"/>

or

<s:property value="#object.name"/>

or

<s:property value="${#object.name}"/>  it is printing nothing.

How can i parse json object in jsp page?


Solution

  • You do not need to parse JSON you need to retrieve value from it. Assuming that your object is org.json.JSONObject you can get value from it by calling get method and passing key as string.

    <s:property value="object.get('name')"/>