`

关于异常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 驱动程序

    MySQL驱动程序是连接Java应用程序与MySQL数据库的关键组件。在Java编程中,我们通常使用JDBC(Java Database Connectivity)API来实现这种连接。JDBC提供了一组接口和类,使得Java程序能够与各种数据库进行交互,...

    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 ...

    关于div+css的用法总结

    【关于div+css的用法总结】 随着网页设计技术的发展,传统的table布局方式逐渐被淘汰,取而代之的是更加灵活和可维护的div+css布局。这种布局方式强调内容与样式的分离,使得网页设计更加模块化,易于适应不同的...

    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 ...

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

    本文将深入探讨Flutter的层叠布局组件——Stack和Positioned,以及它们如何实现类似Web中的绝对定位和Android中的Frame布局。 Stack组件是Flutter中用于创建堆叠布局的一个关键工具,它允许子组件按照声明的顺序堆...

    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 ...

    positioned-relative-to-crx插件

    总结来说,"Positioned Relative To CRX"是一款增强Chrome DevTools功能的实用扩展,它增强了开发者对页面元素相对定位的理解和控制,提高了网页布局和设计的工作效率。对于任何处理CSS定位的开发者而言,这都是一个...

    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 ...

    flutter 总结及遇到的问题集合

    Flutter 总结及遇到的问题集合 在 Flutter 开发中,我们经常会遇到一些问题和难题,本文将总结一些常见的 Flutter 问题和解决方案,帮助开发者快速解决问题,提高开发效率。 一、环境配置 在 Flutter 开发中,...

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

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

    wifi_positioned:wifi本地化演示

    本项目"wifi_positioned:wifi本地化演示"是一个用Java实现的Wi-Fi定位系统演示,旨在帮助用户理解并实践Wi-Fi定位的基本原理和应用。 1. **Wi-Fi定位基础** Wi-Fi定位依赖于Wi-Fi AP的RSSI值,这些值可以反映出...

    IBM MQ错误码大全

    2034: Browse cursor not positioned on message.** - **含义**:浏览光标未定位在消息上。 - **解决方案**:确保浏览光标正确地定位在所需消息上。 **33. 2035: Not authorized for access.** - **含义**:访问未...

    JQuery总结内容

    - `.position()`返回元素相对于其最近的相对定位(positioned)祖先元素的位置。 - `.scrollTop()`和`.scrollLeft()`分别获取元素的垂直和水平滚动位置。 9. **事件处理**: - 示例中的`.bind()`方法用于绑定...

    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 ...

    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...

Global site tag (gtag.js) - Google Analytics