今天项目中遇到了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这个属性 就可以了