`

关于异常positioned update are not supported总结

阅读更多

今天项目中遇到了positioned update are not supported这个异常

先看源代码吧

UserAction.java

public class UserAction extends ActionSupport{
 private String password;
 private User user;
 private String validateCode;
 private Boolean success;
 private String msg;
 
 public void setUser(User user) {
  this.user = user;
 }
 public User getUser() {
  return user;
 }

 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getValidateCode() {
  return validateCode;
 }
 public void setValidateCode(String validateCode) {
  this.validateCode = validateCode;
 }
 public Boolean getSuccess() {
  return success;
 }
 public void setSuccess(Boolean success) {
  this.success = success;
 }
 public String getMsg() {
  return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg;
 }

 @Resource private UserService userService;
 @SuppressWarnings("static-access")
 public String list(){
  String start =  ServletActionContext.getRequest().getParameter("start");
  String limit =  ServletActionContext.getRequest().getParameter("limit");
  Integer index = Integer.parseInt(start);
  Integer pageSize = Integer.parseInt(limit);
  String json = userService.convertAllUser(index,pageSize);
  //输出json对象
  HttpServletResponse response = ServletActionContext.getResponse();
  response.setContentType("Application/json;charset=GBK");
  response.setHeader("Cache-Control","no-cache");
  PrintWriter out = null;
  try {
   out = response.getWriter();
   out.print(json);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   if(out!=null){
    out.flush();
    out.close();
   }
  }
  return this.SUCCESS;
 }
 @SuppressWarnings("static-access")
 public String update(){
  HttpServletRequest request = ServletActionContext.getRequest();
  String jsonArray = request.getParameter("data");
  userService.saveAllUsers(jsonArray);
  return this.SUCCESS;
 }
 @SuppressWarnings("static-access")
 public String delete(){
  HttpServletRequest request = ServletActionContext.getRequest();
  String jsonArray = request.getParameter("data");
  userService.deleteAllUser(jsonArray);
  return this.SUCCESS;
 }
 @SuppressWarnings("static-access")
 public String checkUser(){
  String name = ServletActionContext.getRequest().getParameter("name");
  System.out.println(name);
  if(name!=null){
   List<User> users = userService.getUserByName(name);
   System.out.println("--------------------");
   System.out.println(users.size());
   System.out.println("--------------------");
   if(users.size()!=0){
    success = true;
    msg = "用户名已存在";
   }else{
    success = false;
    msg = "用户名可以注册";
   }
  }
  System.err.println(msg);
  System.out.println(success);
  return this.SUCCESS;
 }
 @SuppressWarnings("static-access")
 public String save(){
  String roleId = ServletActionContext.getRequest().getParameter("role_id");
  System.out.println(roleId);
  Integer role_id = Integer.parseInt(roleId);
  System.out.println(user);
  System.out.println(password);
  System.out.println(role_id);
  if(user!=null){
   userService.saveUser(user,role_id);
   success = true;
   msg = "保存成功";
  }
  return this.SUCCESS;
 }
 @SuppressWarnings("static-access")
 public String checkLogin(){
  String validate = (String) ActionContext.getContext().getSession().get("validateCode");
  if(validate.equals(validateCode.trim())){
   String userName = user.getUserName().trim();
   String psd = user.getPassword().trim();
   List<User> users = userService.getUserByName(userName);
   if(users.size()!=0){
    User user = users.get(0);
    if(user.getPassword().equals(psd)){
     success = true;
     msg = "登录成功";
     Role role = user.getRole();
     List<ACL> acls = role.getAcls();
     List<UserAllInfo> userInfos = new ArrayList<UserAllInfo>();
     ArrayList<Module> modules = new ArrayList<Module>();
     HttpSession session = ServletActionContext.getRequest().getSession();
     for(ACL acl :acls){
      Module module = acl.getModule();
      UserAllInfo userInfo = new UserAllInfo(module.getId(),acl.getPermission(),user.getUserName(),module.getText());
      userInfos.add(userInfo);
      modules.add(module);
     }
     session.setAttribute("user", user);
     session.setAttribute("acl", acls);
     session.setAttribute("role", role);
     session.setAttribute("modules", modules);
     session.setAttribute("userInfo", userInfos);
    }
   }else{
    success = false;
    msg = "用户名不存在";
   }
  }else{
   success = false;
   msg = "验证码错误";
  }
  return this.SUCCESS;
 }
}

UserService.java

@Service
public class UserService {
 @Resource private UserDAO  userDAO;
 @Resource private RoleDAO roleDAO;
 public List<User> getUsers(){
  return userDAO.getUsers();
 }
 @SuppressWarnings({ "unchecked", "deprecation" })
 public void saveAllUsers(String jsonUser){
  JSONArray jsonArray = JSONArray.fromObject(jsonUser);
  List<User> users = JSONArray.toList(jsonArray, User.class);
  for(User user :users){
    if(user!=null){
     userDAO.update(user);
    }
  }
 }
 public String convertAllUser(Integer index,Integer pageSize){
  List<User> users = userDAO.getUsers();
  List<User> subUsers = null;
  int count = users.size();
  if((count%pageSize)<=0){
   subUsers = users.subList(0,count);
  }else if((count/pageSize)>index){
   subUsers = users.subList(index, pageSize);
  }else{
   subUsers = users.subList(((count/pageSize)*pageSize),count);
  }
  String json="";
  PropertyFilter filter = new PropertyFilter() {
   public boolean apply(Object source, String name, Object value) {
    // TODO Auto-generated method stub
    if(name.equals("role"))return true;
    return false;
   }
  };
  JsonConfig config = new JsonConfig();
  config.setJsonPropertyFilter(filter);
  if(users!=null){
   json+="{root:";
   json += JSONArray.fromObject(subUsers, config).toString();
   json+=",totalProperty:"+count+"}";
  }
  return json;
 }
 @SuppressWarnings({ "unchecked", "deprecation" })
 public void deleteAllUser(String jsonUser){
  JSONArray jsonArray = JSONArray.fromObject(jsonUser);
  List<User> users = JSONArray.toList(jsonArray, User.class);
  for(User user :users){
   int id = user.getId();
   if((user!=null)&&(id!=0)){
    userDAO.delete(id);
   }
  }
 }
 public void saveUser(User user,Integer role_id){
  Role role = roleDAO.getRoleById(role_id);
  user.setRole(role);
  userDAO.save(user);
 }
 public List<User> getUserByName(String userName){
  return userDAO.getUserByName(userName);
 }
}
加红的地方是我下面要讲的地方

先说下出现这个异常的原因吧 由于Role role = roleDAO.getRoleById(role_id);在获取role对象时对象里的个别属性是延迟加载的 当中有一些对象是延迟加载的,这些对象并不是原role类中的属性,而是由cglib这个字节码生成器动态生成的对象。Hibernate在这个子类中添加了hibernateLazyInitializer等等的附加属性。由于jsonplugin并不区分类和动态生成的类,所以也会试图序列化hibernateLazyInitializer属性,从而导致出现上述的异常。

思路如下:

1、判断这个对象是否由cglib生成的。代码如下:

boolean isCreateByCGLIB = clazz.getName().indexOf("$$EnhancerByCGLIB$$") > -1 ? true : false;

如果此对象确实是由cglib生成的,那么我们取此对象的基类,这一步很关键,因为通过取得此对象的基类,我们就忽略了所有由cglib生成的跟hibernate相关的属性。

2、通过反射,取得此对象的所有继续于基类的属性。

3、通过反射,取得此对象的所有get方法器(这一步不能省,不然出来的属性就少了)。

4、输出json。

可是我们需要user的getter 和setter方法 那不能去掉这两个方法怎么办呢  那只有这样 在struts的配置文件中加一下配置

action name="user_*" class="userAction" method="{1}">
    <result name="success" type="json">
     <param name="excludeProperties">user</param>
    </result>
   </action>

在序列化action里属性时不序列化user这个属性 就可以了

分享到:
评论

相关推荐

    My sql 驱动程序

    My sql 驱动程序,打成包了,找了好久才找到的

    Sublime Text Build 3124 x64 Setup.exe

    Fixed Replace not working as expected in conjunction with regex look behinds Fixed build systems being unable to use "file_patterns" with the exec command Corrected tab overlap on HiDPI Windows and ...

    Stack与Align、Stack与Positioned、Flutter AspectRatio、Card卡片、Wrap

    此资源是关于移动端框架Flutter(基于Android)的一些常见组件的介绍,包括PPT讲义及源码示例。 此资源介绍的相关内容有:页面布局 Stack层叠组件、Stack与Align、Stack与Positioned实现定位布局、Flutter Aspect...

    visual assist v 10.4.1632 with crack

    (case=5567, case=7457) 6027, 6021 Listboxes are positioned such that they are not truncated at the right edge of the screen. (case=5178) Listbox remains on the same side of a symbol (either ...

    D3D render pipeline

    is introduced and the supported shading algorithms and light types are de- scribed. Chapter 9 covers programmable vertex shading. Programmable vertex shaders can process the vertex data streams with ...

    指纹图像处理

    指纹检测识别matlabIn this paper the problem of fingerprint verification via the Internet is ...as minutiae, are uniquely positioned. Furthermore, such methods may be subject to attacks by hackers when

    Flutter布局组件之层叠组件Stack和Positioned

    层叠布局 Stack、Positioned 层叠布局和Web中的绝对定位、Android中的Frame布局是相似的,子组件可以根据距父容器四个角的位置来确定自身的位置。绝对定位允许子组件堆叠起来(按照代码中声明的顺序)。Flutter中...

    Beginning iOS Social Games

    Mobile games are social and becoming more integrated into our social lives every day. Game Center and Game Kit are Apple’s answers...those that are a step ahead of the future are positioned for success.

    stbc.rar_The Signal_stbc_ui_start

    Tool tips are given in blue color. The cursor needs to be positioned on these for detailed explanation. The main file is "runsim.m". In these simulations the SISO option is not explicitly given. This...

    futureinternet-10-00020-v2.pdf

    sector, are currently requested to make a hard decision, that is, whether to adopt blockchain or not, and they will only know if they were right in 3–5 years. The objective of this paper is to ...

    wifi_positioned:wifi本地化演示

    wifi_positioned wifi定位Demo RSSI指纹库 Knn算法

    c#primer plus附录

    more than one operator can operate on an operand (because the operators are positioned next to the operand, one on either side), this operand is first processed by the operator of the higher ...

    positioned-io-preview-读取和写入偏移-Rust开发

    positioned-io这个板条箱允许您指定读写偏移量,而无需更改文件中的当前位置。 这类似于preposition-io。此板条箱可让您指定读写偏移量,而无需更改文件中的当前位置。 这类似于C中的pread()和pwrite()。这种...

    Understanding Big Data--IBM

    Big Data represents a new era in data exploration and utilization and IBM is uniquely positioned to help clients navigate this transformation This book reveals how IBM is leveraging open source Big ...

    Compiler Building Tutorial

    The intended audience is those folks who are not computer scientists, but who enjoy computing and have always wanted to know how compilers work. A lot of compiler the- ory has been left out, but the ...

    ATX Specification

    A change to the system form factor is ultimately of little benefit if it does not reduce overall system cost. ATX has achieved cost reduction in a number of ways: • Material cost of cables and add-in...

    NGMG 5G白皮书

    The fifth generation of mobile technology (5G) is positioned to address the demands and business contexts of 2020 and beyond. It is expected to enable a fully mobile and connected society and to ...

    cuteEditor6.0

    ,,,,&lt;th&gt; tags are supported. &lt;br/&gt; 图片插入和自动上传 &lt;br/&gt;Built-in thumbnail generator. Thumbnail images are dynamically created; Supports upload new images. Paging - specify how many...

    Learning Material Design (pdf)

    Chapter 2, Building a Mobile Layout, is where we concentrate on some fundamental processes in designing an Android interface, such as the content hierarchy and how components are positioned and scaled...

    positioned-relative-to-crx插件

    语言:English (UK) Chrome DevTools扩展程序,显示所选元素相对于其定位的父元素。 Chrome DevTools扩展程序,显示所选元素相对于其定位的父元素。

Global site tag (gtag.js) - Google Analytics