偶遇一个问题:org.apache.struts2.json.JSONWriter can not access a member of class
org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
with modifiers "public"。困扰了半天,找到了解决方案,写写自己的一些理解。
action代码:
1 package edu.bjfu.action; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.annotation.Resource; 7 8 import org.apache.struts2.json.annotations.JSON; 9 10 11 import com.opensymphony.xwork2.ActionSupport; 12 13 import edu.bjfu.entity.Authority; 14 import edu.bjfu.service.AuthorityService; 15 16 public class AuthoritiesAction extends ActionSupport{ 17 private AuthorityService authorityService; 18 private List<Authority> authorities; 19 20 public List<Authority> getAuthorities() { 21 return authorities; 22 } 23 24 public void setAuthorities(List<Authority> authorities) { 25 this.authorities = authorities; 26 } 27 public AuthorityService getAuthorityService() { 28 return authorityService; 29 } 30 31 @Resource(name="authorityService") 32 public void setAuthorityService(AuthorityService authorityService) { 33 this.authorityService = authorityService; 34 } 35 36 public String execute() throws Exception { 37 authorities=authorityService.getAllAuthorities(); 38 return SUCCESS; 39 } 40 41 }
配置文件代码:
1 <package name="super_admin" extends="json-default" namespace="/superadmin"> 2 <action name="allAuthorities" class="edu.bjfu.action.AuthoritiesAction"> 3 <result type="json"></result> 4 </action> 5 </package>
ajax交互代码:
1 $(function(){ 2 $.post("superadmin/allAuthorities?dt="+new Date().getTime(), 3 function(data){ 4 $("#authority").empty(); 5 var json = eval(data); 6 for(var i=0;i<json.length;i++){ 7 $("#authority").append("<option value='"+json[i].authorityId+"'>"+json[i].authorityName+"</option>"); 8 } 9 },"json") 10 })
这样每次在交互的时候就会发生以上错误。
主要原因:struts会将action中定义的一些变量序列化转换成json格式,需要调用对象的一系列get方法(例子中调用authorityService和authorities的get方法),并调用以上两个变量的成员变量的get方法将其内容组成json格式。但是在序列化authorityService时,由于其成员变量中含有含有接口所以会报错。
解决方案:
1)修改配置文件:指定序列化的根节点,这样data就是从authorities的根节点以下的数据,不需要用data.authorities
1 <package name="super_admin" extends="json-default" namespace="/superadmin"> 2 <action name="allAuthorities" class="edu.bjfu.action.AuthoritiesAction"> 3 <result type="json"><param name="root">authorities</param></result> 4 </action> 5 </package>
2) 修改java代码:让编译器不对authorityService序列化
1 @JSON(serialize=false) 2 public AuthorityService getAuthorityService() { 3 return authorityService; 4 }
3)此外貌似可以通过配置文件的excude和include来指定需要序列化的对象,我没有自己去试!