对象中多一个属性 position 表示排序用的位置号,将所有对象按着升序存入数据库中
@Service @Transactional @SuppressWarnings("unchecked") public class ForumServiceImpl extends DaoSupportImpl<Forum> implements ForumService { @Override public List<Forum> findAll() { return getSession().createQuery(// "FROM Forum f ORDER BY f.position")// .list(); } @Override public void save(Forum forum) { //保存 super.save(forum); //设置position的值 forum.setPosition(forum.getId().intValue()); } public void moveUp(Long id) { //找出相关的forum Forum forum = getById(id); //当前要移动的forun Forum other = (Forum) getSession().createQuery(// "FROM Forum f WHERE f.position<? ORDER BY f.position DESC ")// .setParameter(0, forum.getPosition())// .setFirstResult(0)// .setMaxResults(1)// .uniqueResult() ;//我上面那个forum //最上面的不能够上移 if(other == null){ return ; } //交换position int temp = forum.getPosition() ; forum.setPosition(other.getPosition()); other.setPosition(temp); //更新到数据库中,可以自动更新到数据库中,因为对象现在是持久化状态 getSession().update(forum); getSession().update(other); } public void moveDown(Long id) { //找出相关的forum Forum forum = getById(id); //当前要移动的forun Forum other = (Forum) getSession().createQuery(// "FROM Forum f WHERE f.position>? ORDER BY f.position ASC ")// .setParameter(0, forum.getPosition())// .setFirstResult(0)// .setMaxResults(1)// .uniqueResult() ;//我上面那个forum //最下面的不能够下移 if(other == null){ return ; } //交换position int temp = forum.getPosition() ; forum.setPosition(other.getPosition()); other.setPosition(temp); //更新到数据库中,可以自动更新到数据库中,因为对象现在是持久化状态 getSession().update(forum); getSession().update(other); } }
变灰显示,不可移动
<!-- 最上面的不能上移 --> <s:if test="#status.first"> <span class="disabled">上移</span> </s:if> <s:else> <s:a action="forumManage_moveUp?id=%{id}">上移</s:a> </s:else> <!-- 最下面的不能下移 --> <s:if test="#status.last"> <span class="disabled">下移</span> </s:if> <s:else> <s:a action="forumManage_moveDown?id=%{id}">下移</s:a> </s:else>