JSON object parsing using OGNL in struts 2

*爱你&永不变心* 提交于 2019-12-11 12:16:02

问题


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?


回答1:


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')"/>



回答2:


You can parse JSON using the library function parseJSON like in this example

<s:textfield id="name" name="name"/>
<s:textfield id="mobile" name="mobile"/>    
<script type="text/javascript">
  $(document).ready(function() {
    var obj = $.parseJSON('<s:property value="object"/>');
    $("name").val(obj.name);
    $("mobile").val(obj.mobile);
  });
</script>

This script will replace the values (if any) from the action bean populated when JSP was rendered. The textfields should be mapped to the action bean correspondingly.



来源:https://stackoverflow.com/questions/22110439/json-object-parsing-using-ognl-in-struts-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!