用servlet实现ztree重新排序问题

@WebServlet("/ReSortServlet")
public class ReSortServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final Logger logger = Logger.getLogger(ReSortServlet.class);
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReSortServlet() {
        super();
    }


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
//response.getWriter().append("Served at: ").append(request.getContextPath());
// 设置接收的编码格式
req.setCharacterEncoding("utf-8");
String whichType = req.getParameter("type");//判断是何模块
int nowSort = Integer.parseInt(req.getParameter("nowsort"));//移动最终序号
String moveId = req.getParameter("moveid");//要移动的id
        String type = req.getParameter("type");
Transaction t = null;
System.out.println("type"+type);
try {
t = (Transaction) BaseDao.getCurSession().beginTransaction();
Type move_c = (Type)BaseDao.get(Type.class, moveId);
if(move_c.getLevel()!=1){
   String hql = "update Type t set t.sort = t.sort-1 where t.state = ? and t.sort > ? and t.sort <= ? and t.id != ? and t.parentType.id = ?";
   if(nowSort < move_c.getSort())
hql = "update Type t set t.sort = t.sort+1 where t.state = ? and t.sort < ? and t.sort >= ? and t.id != ? and t.parentType.id = ?";
   BaseDao.executeHql(hql, new Object[]{Constants.STATE_OK, move_c.getSort(), nowSort, move_c.getId(), move_c.getParentType().getId()});
   move_c.setSort(nowSort);
}else{
String hql = "update Type t set t.sort = t.sort-1 where t.state = ? and t.sort > ? and t.sort <= ? and t.id != ? and t.type = ? and t.level = 1";
if(nowSort < move_c.getSort())
hql = "update Type t set t.sort = t.sort+1 where t.state = ? and t.sort < ? and t.sort >= ? and t.id != ? and t.type = ? and t.level = 1";
BaseDao.executeHql(hql, new Object[]{Constants.STATE_OK, move_c.getSort(), nowSort, move_c.getId(), type});
move_c.setSort(nowSort);
}
BaseDao.update(move_c);
t.commit();
} catch (Exception e) {
logger.error(e.toString());
if(t != null)
t.rollback();
response.getWriter().write("error");
}

response.getWriter().write("success");
}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}


}

猜你喜欢

转载自blog.csdn.net/yy_lemon/article/details/79655235