------------注意的包:
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
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;
}