标签:conf 技术分享 manage val ase com values title enter
设计实体Forum.java
public class Forum { private Long id; private String name; private String Description; private int position; //排序用的位置号 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return Description; } public void setDescription(String description) { Description = description; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } }
映射文件Forum.hbm.xml
<hibernate-mapping package="cn.itcast.oa.domain"> <class name="Forum" table="itcast_forum"> <id name="id"> <generator class="native" /> </id> <property name="name"></property> <property name="description"></property> <property name="position"></property> </class> </hibernate-mapping>
加到hibernate.hbm.xml中
<mapping resource="cn/itcast/oa/domain/Forum.hbm.xml" />
建表
运行testSessionFactory测试方法
增删改查6个请求,上移下移2个请求
ForumManageAction.java
@Controller @Scope("prototype") public class ForumManageAction extends BaseAction<Forum>{ /** 列表 */ public String list() throws Exception { return "list"; } /** 删除 */ public String delete() throws Exception { return "toList"; } /** 添加页面 */ public String addUI() throws Exception { return "saveUI"; } /** 添加 */ public String add() throws Exception { return "toList"; } /** 修改页面 */ public String editUI() throws Exception { return "saveUI"; } /** 修改 */ public String edit() throws Exception { return "toList"; } /** 上移 */ public String moveUp() throws Exception { return "toList"; } /** 下移 */ public String moveDown() throws Exception { return "toList"; } }
创建所用到的页面
2个,新建,列表
配置
在ForumAction类上标注
struts.xml
<!-- 论坛:版块管理 --> <action name="forumManage_*" class="forumManageAction" method="{1}"> <result name="list">/WEB-INF/jsp/forumManageAction/list.jsp</result> <result name="saveUI">/WEB-INF/jsp/forumManageAction/saveUI.jsp</result> <result name="toList" type="redirectAction">forumManage_list</result> </action>
准备service及实现类
ForumService.java
public interface ForumService extends DaoSupport<Forum>{ }
ForumServiceImpl.java
@Service @Transactional public class ForumServiceImpl extends DaoSupportImpl<Forum> implements ForumService{ }
在BaseAction.java中声明
@Resource protected ForumService forumService;
填空实现ForumManageAction里面的功能
@Controller @Scope("prototype") public class ForumManageAction extends BaseAction<Forum>{ /** 列表 */ public String list() throws Exception { List<Forum> forumList = forumService.findAll(); ActionContext.getContext().put("forumList", forumList); return "list"; } /** 删除 */ public String delete() throws Exception { forumService.delete(model.getId()); return "toList"; } /** 添加页面 */ public String addUI() throws Exception { return "saveUI"; } /** 添加 */ public String add() throws Exception { forumService.save(model); return "toList"; } /** 修改页面 */ public String editUI() throws Exception { //准备回显的页面 Forum forum = forumService.getById(model.getId()); ActionContext.getContext().getValueStack().push(forum);//放在栈顶 return "saveUI"; } /** 修改 */ public String edit() throws Exception { //从数据库中取出原对象 Forum forum = forumService.getById(model.getId()); //设置要修改的属性 forum.setName(model.getName()); forum.setDescription(model.getDescription()); //更新到数据库 forumService.update(forum); return "toList"; } /** 上移 */ public String moveUp() throws Exception { forumService.moveUp(model.getId()); return "toList"; } /** 下移 */ public String moveDown() throws Exception { forumService.moveDown(model.getId()); return "toList"; } }
新增的两个方法在ForumService接口中声明
public interface ForumService extends DaoSupport<Forum>{ //上移,最上面的不能上移了 void moveUp(Long id); //下移,最下面的不能下移了 void moveDown(Long id); }
ForumServileImpl类中对两个方法进行实现,具体怎么实现下面在做
@Service @Transactional public class ForumServiceImpl extends DaoSupportImpl<Forum> implements ForumService{ public void moveUp(Long id) { } public void moveDown(Long id) { } }
写页面
第一拷贝源代码
第二include
第三替换路径
第四改具体内容
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>版块列表</title> <%@ include file="/WEB-INF/jsp/public/commons.jspf" %> </head> <body> <div id="Title_bar"> <div id="Title_bar_Head"> <div id="Title_Head"></div> <div id="Title"><!--页面标题--> <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 版块管理 </div> <div id="Title_End"></div> </div> </div> <div id="MainArea"> <table cellspacing="0" cellpadding="0" class="TableStyle"> <!-- 表头--> <thead> <tr align="CENTER" valign="MIDDLE" id="TableTitle"> <td width="250px">版块名称</td> <td width="300px">版块说明</td> <td>相关操作</td> </tr> </thead> <!--显示数据列表--> <tbody id="TableData" class="dataContainer" datakey="forumList"> <s:iterator value="#forumList"> <tr class="TableDetail1 template"> <td>${name} </td> <td>${description} </td> <td> <s:a action="forumManage_delete?id=%{id}" onclick="return delConfirm()">删除</s:a> <s:a action="forumManage_editUI?id=%{id}" >修改</s:a> <s:a action="forumManage_moveUp?id=%{id}" >上移</s:a> <s:a action="forumManage_moveDown?id=%{id}" >下移</s:a> </td> </tr> </s:iterator> </tbody> </table> <!-- 其他功能超链接 --> <div id="TableTail"> <div id="TableTail_inside"> <s:a action="forumManage_addUI"><img src="${pageContext.request.contextPath}/style/images/createNew.png" /></s:a> </div> </div> </div> <div class="Description"> 说明:<br /> 1,显示的列表按其sortOrder值升序排列。<br /> 2,可以通过上移与下移功能调整顺序。最上面的不能上移,最下面的不能下移。<br /> </div> </body> </html>
saveUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>版块设置</title> <%@ include file="/WEB-INF/jsp/public/commons.jspf" %> </head> <body> <!-- 标题显示 --> <div id="Title_bar"> <div id="Title_bar_Head"> <div id="Title_Head"></div> <div id="Title"><!--页面标题--> <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 版块设置 </div> <div id="Title_End"></div> </div> </div> <!--显示表单内容--> <div id="MainArea"> <s:form action="forumManage_%{id == null ? ‘add‘ : ‘edit‘}"> <s:hidden name="id"></s:hidden> <div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1"> <IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 版块信息 </DIV> --> </div> <!-- 表单内容显示 --> <div class="ItemBlockBorder"> <div class="ItemBlock"> <table cellpadding="0" cellspacing="0" class="mainForm"> <tr> <td width="100">版块名称</td> <td><s:textfield name="name" cssClass="InputStyle" /> *</td> </tr> <tr> <td>版块说明</td> <td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td> </tr> </table> </div> </div> <!-- 表单操作 --> <div id="InputDetailBar"> <input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/> <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a> </div> </s:form> </div> <div class="Description"> 说明:<br /> 1,新添加的版块默认显示在最下面。<br /> </div> </body> </html>
测试
访问http://localhost:8080/ItcastOA/登录
之前设计实体时设计了一个position字段
1,查询时要按position的值排序。
2,添加时要指定position的值,要唯一。
3,上下移动就是与上面或下面的那个Forum交换position的值。
SELECT * FROM itcast_forum order by position;
select * from itcast_forum where position=(//查询位置2的信息
select max(position) from itcast_forum where position<3//找到小于位置3的最大那个位置,也就是2
);
select * from itcast_forum where position<7 order by position desc limit 0,1;
对应写法
Forum other = (Forum) getSession().createQuery(//我上面那个Forum "FROM Forum f WHERE f.position<? ORDER BY f.position DESC")// .setParameter(0, forum.getPosition())// .setFirstResult(0)// .setMaxResults(1)// .uniqueResult();
ForumServiceImpl.java
@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());//转为int型 } //上移和下移都是通过改变position的值实现的 public void moveUp(Long id) { //找出相关的Forum Forum forum = getById(id);//当前要移动的Forum Forum other = (Forum) getSession().createQuery(//我上面那个Forum "FROM Forum f WHERE f.position<? ORDER BY f.position DESC")// .setParameter(0, forum.getPosition())// .setFirstResult(0)// .setMaxResults(1)// .uniqueResult(); //最上面的不能上移 if(other == null) { return; } //交换posution的值 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);//当前要移动的Forum Forum other = (Forum) getSession().createQuery(//我下面那个Forum "FROM Forum f WHERE f.position>? ORDER BY f.position ASC")// .setParameter(0, forum.getPosition())// .setFirstResult(0)// .setMaxResults(1)// .uniqueResult(); //最下面的不能上移 if(other == null) { return; } //交换posution的值 int temp = forum.getPosition(); forum.setPosition(other.getPosition()); other.setPosition(temp); //更新到数据库中,可以不写,因为对象现在是持久化状态 getSession().update(forum); getSession().update(other); } }
最上面上移和最下面下移变灰色不能点
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>版块列表</title> <%@ include file="/WEB-INF/jsp/public/commons.jspf" %> <style type="text/css"> .disabled{ color:gray; cursor:pointer; } </style> </head> <body> <div id="Title_bar"> <div id="Title_bar_Head"> <div id="Title_Head"></div> <div id="Title"><!--页面标题--> <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 版块管理 </div> <div id="Title_End"></div> </div> </div> <div id="MainArea"> <table cellspacing="0" cellpadding="0" class="TableStyle"> <!-- 表头--> <thead> <tr align="CENTER" valign="MIDDLE" id="TableTitle"> <td width="250px">版块名称</td> <td width="300px">版块说明</td> <td>相关操作</td> </tr> </thead> <!--显示数据列表--> <tbody id="TableData" class="dataContainer" datakey="forumList"> <s:iterator value="#forumList" status="status"> <tr class="TableDetail1 template"> <td>${name} </td> <td>${description} </td> <td> <s:a action="forumManage_delete?id=%{id}" onclick="return delConfirm()">删除</s:a> <s:a action="forumManage_editUI?id=%{id}" >修改</s:a> <!-- 最上面的不能上移 --> <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="disable"></span> </s:if> <s:else> <s:a action="forumManage_moveDown?id=%{id}" >下移</s:a> </s:else> </td> </tr> </s:iterator> </tbody> </table> <!-- 其他功能超链接 --> <div id="TableTail"> <div id="TableTail_inside"> <s:a action="forumManage_addUI"><img src="${pageContext.request.contextPath}/style/images/createNew.png" /></s:a> </div> </div> </div> <div class="Description"> 说明:<br /> 1,显示的列表按其sortOrder值升序排列。<br /> 2,可以通过上移与下移功能调整顺序。最上面的不能上移,最下面的不能下移。<br /> </div> </body> </html>
标签:conf 技术分享 manage val ase com values title enter
原文地址:http://www.cnblogs.com/justdoitba/p/7821021.html