简体   繁体   English

在struts 2中将java bean转换为json数据

[英]Convert java bean to json data in struts 2

I have a java bean class 'MenuItem' which consist of list of children . 我有一个java bean类'MenuItem',它由子列表组成。 I want to display iterate and render through this object in jsp and display the menu tree. 我想在jsp中显示迭代并通过此对象渲染并显示菜单树。 I tried the json data creation, but it needs ajax call. 我尝试了json数据创建,但它需要ajax调用。 And in my case, I need to submit the page instead of ajax. 在我的情况下,我需要提交页面而不是ajax。

I am using struts 2. Can anyone please suggest how can I render and iterate through my bean object in jsp ? 我正在使用struts 2.任何人都可以建议我如何在jsp中渲染和遍历我的bean对象?

Thanks in Advance. 提前致谢。

Here is my Menu bean object: 这是我的Menu bean对象:

public class MenuItem implements Serializable {    
    private static final long serialVersionUID = 8690828081102943225L;

    private Long id;
    private String name;
    private Collection<MenuItem> children;
    private String url;
    private String actionHoverLabel;
    private String actionLabel;
    private Long displayOrder;
    private Boolean isLeaf;
    private Boolean isVisible;
    private Long menuLevel;
    private Long parentId;      

    public String getActionHoverLabel() {
        return actionHoverLabel;
    }

    public void setActionHoverLabel(String actionHoverLabel) {
        this.actionHoverLabel = actionHoverLabel;
    }

    public String getActionLabel() {
        return actionLabel;
    }

    public void setActionLabel(String actionLabel) {
        this.actionLabel = actionLabel;
    }

    public Long getDisplayOrder() {
        return displayOrder;
    }

    public void setDisplayOrder(Long displayOrder) {
        this.displayOrder = displayOrder;
    }

    public Boolean getIsLeaf() {
        return isLeaf;
    }

    public void setIsLeaf(Boolean isLeaf) {
        this.isLeaf = isLeaf;
    }

    public Boolean getIsVisible() {
        return isVisible;
    }

    public void setIsVisible(Boolean isVisible) {
        this.isVisible = isVisible;
    }

    public Long getMenuLevel() {
        return menuLevel;
    }

    public void setMenuLevel(Long menuLevel) {
        this.menuLevel = menuLevel;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }


    public MenuItem() {
        children = new ArrayList<MenuItem>();
        isVisible = true;
    }

    public MenuItem(Long id, String name) {

        this.id = id;
        this.name = name;
        children = new ArrayList<MenuItem>();
        isVisible = true;
    }

    public Long getId() {    
        return this.id;    
    }

    public void setId(Long id) {
        this.id = id;    
    }

    public String getName() {    
        return this.name;    
    }

    public void setName(String name) {    
        this.name = name;    
    }

    public Collection<MenuItem> getChildren() {

        return this.children;

    }

    public void setChildren(Collection<MenuItem> children) {    
        this.children = children;    
    }


    public boolean isLeaf() {    
        if (children != null && children.size() > 0) {    
            return false;    
        } else {    
            return true;    
        }    
    }

    public void addChild(MenuItem child) {    
        if (child != null && children != null) {    
            children.add(child);
            child.setParentId(this.getId());    
        }    
    }

    public void removeChild(MenuItem child) {    
        if (child != null && children != null) {    
            children.remove(child);
            child.setParentId(null);    
        }    
    }       

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl() {
        if (url == null) {
            return name;
        } else {
            return url;
        }
    }


    @Override
    public boolean equals(Object obj) {
        MenuItem m = null;
        boolean isEqual = false;    
        if (id != null && id >= 0) {
            m = (MenuItem) obj;
            if (m.getId().equals(id) 
                    isEqual = true;                     
        } else {    
            isEqual = super.equals(obj);    
        }    
        return isEqual;    
    }

    @Override
    public int hashCode() {    
        if (id != null && id >= 0) {    
            return id.hashCode();    
        } else {    
            return super.hashCode();    
        }    
    }

    @Override
    public String toString() {    
        return "name: " + name + " id: " + id;    
    }

Go to www.json.org 访问www.json.org

Download Java Library for JSON encoding. 下载用于JSON编码的Java库。

Compile it (build a jar for convenience). 编译它(为方便起见构建一个jar)。

Write a method alike this to JSON Serialize your bean: 写一个类似于JSON的方法序列化你的bean:

 public static String toJson(SomeBean bean) 
 {
        JSONObject jo = new JSONObject(bean);
        return jo.toString();
 }

Deserialization is a little tricky but should work like: 反序列化有点棘手,但应该像:

JSONObject jo = new JSONObject(jsonString);
SomeBean res = new SomeBean();

res.someProperty = jo.getString("someProperty");
res.someIntProperty= jo.getInt("someIntProperty");

Obviously you have to take care of complex properties, but for simple beans it should work out like a charm. 显然你必须要处理复杂的属性,但对于简单的bean来说,它应该像魅力一样。

Straight from sources: 直接来源:

/**
     * Construct a JSONObject from an Object using bean getters.
     * It reflects on all of the public methods of the object.
     * For each of the methods with no parameters and a name starting
     * with <code>"get"</code> or <code>"is"</code> followed by an uppercase letter,
     * the method is invoked, and a key and the value returned from the getter method
     * are put into the new JSONObject.
     *
     * The key is formed by removing the <code>"get"</code> or <code>"is"</code> prefix.
     * If the second remaining character is not upper case, then the first
     * character is converted to lower case.
     *
     * For example, if an object has a method named <code>"getName"</code>, and
     * if the result of calling <code>object.getName()</code> is <code>"Larry Fine"</code>,
     * then the JSONObject will contain <code>"name": "Larry Fine"</code>.
     *
     * @param bean An object that has getter methods that should be used
     * to make a JSONObject.
     */
    public JSONObject(Object bean) {
        this();
        this.populateMap(bean);
    }

Are you sure you've everything set up correctly (Classpath) ? 你确定你已经正确设置了所有东西(Classpath)吗?

If you want to operate on the bean in JSP, you can very well do that with struts tags. 如果你想在JSP中操作bean,你可以使用struts标签来做到这一点。 The bean has to be in the scope. bean必须在范围内。

Why do you need JSON in this case? 在这种情况下,为什么需要JSON? If you still want JSON, the check for GSON library and Jackson mapper library. 如果您仍然需要JSON,请检查GSON库和Jackson映射器库。 On the JSP, you can store the JSON object in a variable and then operate on that. 在JSP上,您可以将JSON对象存储在变量中,然后对其进行操作。 On page submit Struts action would form the JSON and then set it in a page bean variable. 在页面提交Struts操作将形成JSON,然后在页面bean变量中设置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM