Jimmy小站

小明也有大梦想 — 蒋明/铭
当前位置:网站首页 / 未分类 / 正文

Struts2 中 Action返回Json字符串

2016-07-24 / 未分类 / 3955 次围观 / 0 次吐槽

1. 首先在SSH(Struts2 Spring Hibernate)项目中 引入struts2-json-plugin.jar(可以下载jar包然后放入项目的lib目录下,如果是maven项目可以直接在pom.xml文件中写入dependency)

<dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-json-plugin</artifactId>
  <version>2.3.28.1</version>
</dependency>

2. 在struts.xml的package中extends第一步加入的jar包中的json-default。同时配置好相应的action,这里我们配置的action中的result-》type要指明为json,在URL上访问http://context/ajax_test    就会发访问对应bean id = ajax 的action类中的test方法。最终返回需要的字符串。

<package name="ajax_conf" namespace="/" extends="json-default" >
   <action name="ajax_*" class="ajax" method="{1}" >
      <result name="success" type="json"/>
   </action>
</package>

同时附上applicationContext.xml 以及 AjaxAction.java

<?xml version="1.0" encoding="UTF-8"?>
<beans
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">
   <bean id="test" class="shu.lab.action.TestAction" />
   <bean id="ajax" class="shu.lab.action.AjaxAction" />
</beans>
package shu.lab.action;

import com.opensymphony.xwork2.ActionSupport;
import shu.lab.entity.User;


/**
 * Created by Jimmy on 2016/7/17.
 */
public class AjaxAction extends ActionSupport {
    //Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
    User user = new User();

    public User getUser() {
        return user;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("AjaxAction.execute");
        user.setUserId(12);
        user.setUsername("jimmy");
        user.setPassword("123456");
        return super.execute();
    }
    public String test() throws Exception {
        System.out.println("AjaxAction.test");
        user.setUserId(12);
        user.setUsername("jimmy");
        user.setPassword("123456");
        return super.execute();
    }
}

最终测试结果图如下

blob.png

最后需要注意一点就是,如果两个对象之间存在相互引用的话(比如说User里面有一个Gruop,同时Group里面有一个Set<User> users),这样会导致相互循环引用直至内存溢出报错。所以需要在Set(一对多)变量的get方法前加上@JSON(serialize = false)忽略这个成员,从而切断循环。

package shu.lab.entity;

import org.apache.struts2.json.annotations.JSON;

import java.util.HashSet;
import java.util.Set;

/**
 * Group entity. @author Jimmy J
 */

public class Group implements java.io.Serializable {

   // Fields

   private Integer groupId;
   private String groupName;
   private Set groupAuths = new HashSet(0);

   // Constructors

   /** default constructor */
   public Group() {
   }

   /** full constructor */
   public Group(String groupName, Set groupAuths) {
      this.groupName = groupName;
      this.groupAuths = groupAuths;
   }

   // Property accessors

   public Integer getGroupId() {
      return this.groupId;
   }

   public void setGroupId(Integer groupId) {
      this.groupId = groupId;
   }

   public String getGroupName() {
      return this.groupName;
   }

   public void setGroupName(String groupName) {
      this.groupName = groupName;
   }

   @JSON(serialize = false)
   public Set getGroupAuths() {
      return this.groupAuths;
   }

   public void setGroupAuths(Set groupAuths) {
      this.groupAuths = groupAuths;
   }

   @Override
   public String toString() {
      return "Group{" +
            "groupId=" + groupId +
            ", groupName='" + groupName + '\'' +
            '}';
   }
}


推荐您阅读更多有关于“struts2json,”的文章

[一个Java程序猿的转型之路,读研深造,专注机器学习推荐算法]
本站所有文章如无特别注明均为原创。作者:吉米酱 ,复制或转载请以超链接形式注明转自 Jimmy小站
原文地址《Struts2 中 Action返回Json字符串
额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Copyright © Jimmy小站 Allrights Reserved.备案号:桂ICP备 15005996