写篇经验贴睡觉折腾了我4个小时,说多了都是泪。为了看到的人以后不流泪,熬夜到1点我也要写出来
net.sf.json.JSONException: There is a cycle in the hierarchy!
net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsArray(CycleDetectionStrategy.java:)
net.sf.json.JSONArray._fromCollection(JSONArray.java:)
net.sf.json.JSONArray.fromObject(JSONArray.java:)
net.sf.json.JSONObject._processValue(JSONObject.java:)
net.sf.json.JSONObject._setInternal(JSONObject.java:)
net.sf.json.JSONObject.setValue(JSONObject.java:)
net.sf.json.JSONObject._fromBean(JSONObject.java:)
net.sf.json.JSONObject.fromObject(JSONObject.java:)
net.sf.json.JSONObject._processValue(JSONObject.java:)
net.sf.json.JSONObject._setInternal(JSONObject.java:)
net.sf.json.JSONObject.setValue(JSONObject.java:)
net.sf.json.JSONObject._fromBean(JSONObject.java:)
net.sf.json.JSONObject.fromObject(JSONObject.java:)
net.sf.json.JSONObject._processValue(JSONObject.java:)
net.sf.json.JSONObject._setInternal(JSONObject.java:)
net.sf.json.JSONObject.setValue(JSONObject.java:)
net.sf.json.JSONObject._fromBean(JSONObject.java:)
net.sf.json.JSONObject.fromObject(JSONObject.java:)
net.sf.json.JSONObject._processValue(JSONObject.java:)
net.sf.json.JSONObject._setInternal(JSONObject.java:)
net.sf.json.JSONObject.setValue(JSONObject.java:)
net.sf.json.JSONObject._fromBean(JSONObject.java:)
net.sf.json.JSONObject.fromObject(JSONObject.java:)
net.sf.json.JSONArray._processValue(JSONArray.java:)
net.sf.json.JSONArray.processValue(JSONArray.java:)
net.sf.json.JSONArray.addValue(JSONArray.java:)
net.sf.json.JSONArray._fromCollection(JSONArray.java:)
net.sf.json.JSONArray.fromObject(JSONArray.java:)
net.sf.json.JSONObject._processValue(JSONObject.java:)
net.sf.json.JSONObject._setInternal(JSONObject.java:)
net.sf.json.JSONObject.setValue(JSONObject.java:)
net.sf.json.JSONObject._fromBean(JSONObject.java:)
net.sf.json.JSONObject.fromObject(JSONObject.java:)
net.sf.json.JSONArray._processValue(JSONArray.java:)
net.sf.json.JSONArray.processValue(JSONArray.java:)
net.sf.json.JSONArray.addValue(JSONArray.java:)
net.sf.json.JSONArray._fromCollection(JSONArray.java:)
net.sf.json.JSONArray.fromObject(JSONArray.java:)
com.sun.action.UserInfoAction.findUserinfoAll(UserInfoAction.java:)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:)
java.lang.reflect.Method.invoke(Method.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:)
这是使用hibernate+Spring+Struts2时报的错,具体说的是我在把List对象集合转成JSon时报错,明眼人一看就知道,死循环了……原因就是我取值的这个对象很复杂关联了N个对象(表),N个对象(表)又关联它
4个小时的成果分享,我想说才开始时报的不是这个错/(ㄒoㄒ)/~~,
第一次报的是Sesson关闭,具体原因是session没等我会话结束就关闭了,取值你时……掰着脚趾头都能想到O(∩_∩)O~,我就把session关闭时间延长具体代码
1.在加入这个FILETER的时候,一定一定,千万千万记得放在STRUTS2的核心控制器的位置的上面!切记!
2.如果你的SPRING的配置文件中SessionFactory的BEAN ID默认不是sessionFactory的话,你的openSessionInView要改成这样.
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value><!--//这里的值是取决于你在bean.xml中的配置的名称的
--></init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
加了之后呢,继续错,苦逼了吧
当你把OPENSESSIONINVIEW配置上后,你就可以让SESSION晚点关闭的.但是又一个新的问题产生了.什么问题呢?自己可以试试,如果这个时候你的SPRING
没有配置事务的话,那么你所有的更新操作都是不允许的.
所以要在SPRING里把需要更新的SERVICE操作都配置事务.
ok加事务
配置如下:
首先在SPRING配置文件中加入XML命名空间:
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
方案地址:
xsi:schemaLocation里加上
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
前提是你配置了AOP的命名空间和方案地址
<!-- 事务配置 -->
<!--Step1.事务管理器配置-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--Step2.交给SPRING事务管理的一些方法.-->
<aop:config>
<aop:pointcut id="productServiceMethods"
<!-- 指定切面是哪个范围类,比如你项目的SERVICE的所有方法-->
expression="execution(* com.公司名.项目名.service..*.*(..))" />
<!-- 一个通知的集合,这个集合都用上的POINTCUT-->
<aop:advisor advice-ref="txAdvice"
pointcut-ref="productServiceMethods" />
</aop:config>
<!--定义通知集合,以TX开头,是有事务的 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--所有的以ADD开头的方法名的方法都用事务 REQUIRED 表示,比如方法A有更新操作,A中调用了B方法,那么B到底是重新起一个事务还是用A方法的事务,
REQUIRED标识不起就用当前事务,如果有就用A的,如果没有就起一个-->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<!--加入只读,说明以GET或者LOAD开头的方法名的方法都是只读事务,说明这个方法内没有UPDATE操作,这样声明下可以提高该查询方法的效率 -->
<tx:method name="get*" propagation="REQUIRED"
read-only="true" />
<tx:method name="load*" propagation="REQUIRED"
read-only="true" />
</tx:attributes>
</tx:advice>
此时我以为问题会到此终结,结果证明我异想天开。
好了,不扯了回归正题,怎么解决复杂的集合对象转成json呢。
//根据页数和行数查出数据
userinfoList=userInfoServices.findUserInfoAll(page, rows);
//总行数
int count=userInfoServices.findUserinfoCount();
//转成json
JsonConfig cfg = new JsonConfig();
//这里是把关联对象剔除掉
cfg.setJsonPropertyFilter(new PropertyFilter()
{
public boolean apply(Object source, String name, Object value) {
if(name.equals("tbDepartment")||name.equals("tbOrders")||name.equals("tbUserinfoRoles")||name.equals("tbCommisions")) {
return true;
} else {
return false;
}
}
});
JSONArray jsonArray = JSONArray.fromObject(userinfoList,cfg); //加载配置文件
System.out.print("[{total:"+count+",rows:"+jsonArray.toString()+"}]");
希望能帮到你晚安。。。。
最后附上我的漂亮界面