ssh 框架写接口注意事项

本文介绍如何在 Struts2 中使用 JSON 插件实现数据的 JSON 格式返回,包括所需 JAR 包、Struts 配置、Action 实现等关键步骤,并给出了解决延迟加载问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

------------注意的包:                

struts2-json-plugin-2.1.8.1.jar 

json-lib-2.1.jar

commons-collections-3.2.jar

commons-beanutils-1.7.0.jar

commons-lang-2.3.jar

commons-logging-1.0.4.jar

ezmorph-1.0.3.jar

7个包是返回json形式的数据必须的。因为json大量引用了Apache commons的包,所以要加入4个,commons包,除了commons的包外,还需要引入一个 ezmorph的包。最后加入struts2必须的6个包: 

struts2-core-2.1.8.1.jar 

xwork-core-2.1.6.jar

ognl-2.7.3.jar

freemarker-2.3.15.jar

commons-fileupload-1.2.1.jar

commons-io-1.3.2.jar


--------------struts配置

<struts>
    <constant name="struts.objectFactory" value="spring"></constant>
    <constant name="struts.multipart.maxSize" value="10485760"/>
    <package name="hlshMobileInterface"  namespace="/" extends="json-default" >
        <action name="mobile" class="mobileInterfaceAction">
            <!-- <result type="json" name="success">
                <param name="root">communityMap</param>
            </result> -->
            <result name="success" type="json">
                <param name="root">communityMap</param>
            </result>
        </action>
    </package>    
</struts>


----------action


public class MobileInterfaceAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
       //注入service
        private MobileInterfaceService mobileInterfaceService;
        private CommunityService communityService;
        private InformationService informationService;
        private AdvertismentService advertismentService;
        private CommunityServiceService communityServiceService;
        private AroundBusinessService aroundBusinessService;
        //请求
        private HttpServletRequest request;
        //响应
        private HttpServletResponse response;
        private Map<String,Object> communityMap;    
        //key值
        private String keyCom="com";
        /**
         *
         * @return communityMap
         */
        public Map<String, Object> getCommunityMap() {
            return communityMap;
        }

        public void setMobileInterfaceService(
                MobileInterfaceService mobileInterfaceService) {
            this.mobileInterfaceService = mobileInterfaceService;
        }

        public void setCommunityService(CommunityService communityService) {
            this.communityService = communityService;
        }

        public void setInformationService(InformationService informationService) {
            this.informationService = informationService;
        }

        public void setAdvertismentService(AdvertismentService advertismentService) {
            this.advertismentService = advertismentService;
        }

        public void setCommunityServiceService(
                CommunityServiceService communityServiceService) {
            this.communityServiceService = communityServiceService;
        }

        public void setAroundBusinessService(AroundBusinessService aroundBusinessService) {
            this.aroundBusinessService = aroundBusinessService;
        }
        
        
        public void setServletResponse(HttpServletResponse arg0) {
            // TODO Auto-generated method stub
            
        }
    
        public void setServletRequest(HttpServletRequest arg0) {
            // TODO Auto-generated method stub
            
        }
        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            communityMap=new HashMap<String, Object>();
            List<Community> lstCom=null;
            /*List<Hlshadvertisement> lstAdvtesment=null;*/
            try {
                lstCom=communityService.getAllCountCommunity(new Community());
                /*lstAdvtesment=informationService.getAllAdvertisment(new Hlshadvertisement());*/
                communityMap.put(keyCom, lstCom);
                communityMap.put("result",1);
            } catch (Exception e) {
                // TODO: handle exception
                communityMap.put(keyCom, null);
                communityMap.put("result",0);
            }
            return "success";
        }
        /**
         * 移动接口返回所有的下去数据
         * @return
         *//*
        public String getAllComunity(){
            List<Community> lstCom=null;
            try {
                lstCom=communityService.getAllCountCommunity(new Community());
                communityMap.put(keyCom, lstCom);
                communityMap.put("result",1);
            } catch (Exception e) {
                // TODO: handle exception
                communityMap.put(keyCom, null);
                communityMap.put("result",0);
            }
            return "success";
        } */

}
-----------------解决延迟加载的问题(值得注意)

struts2  在 xml的配置已经不管用(如下)

<!-- 解决延迟加载问题lazy -->
  <filter>
      <filter-name>openSessionInViewFilter</filter-name>
      <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
 
  <filter-mapping>
      <filter-name>openSessionInViewFilter</filter-name>
      <url-pattern>*.action</url-pattern>
  </filter-mapping>
 
 <filter-mapping>
      <filter-name>openSessionInViewFilter</filter-name>
      <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

-------------------解决方案(可能存在延迟加载的地方)

添加:@JSON(serialize=false)

如:

@JSON(serialize=false)
    public Set getAroundbusinesses() {
        return this.aroundbusinesses;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值