论坛模块_版块管理1_增删改查

设计实体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}&nbsp;</td>
<td>${description}&nbsp;</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/登录

论坛模块_版块管理2_实现上下移动1

之前设计实体时设计了一个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}&nbsp;</td>
<td>${description}&nbsp;</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>

论坛模块_版块管理_增删改查&实现上下移动的更多相关文章

  1. 数据库开发基础-SQl Server 控制数据库的服务+数据库的创建与管理(增删改查)

    控制数据库的服务: 方法一: 1.Windows+R 打开运行  打开cmd 2.输入net start MSSQLserver 启动数据库服务 输入net stop MSSQLserver 关闭数据 ...

  2. 数据库开发基础-★SQl Server 控制数据库的服务+数据库的创建与管理(增删改查)★

    控制数据库的服务: 方法一: 1.Windows+R 打开运行  打开cmd 2.输入net start MSSQLserver 启动数据库服务 输入net stop MSSQLserver 关闭数据 ...

  3. 1.SSM整合_单表的增删改查

    目标:增删改查 环境:Maven+Eclipse+Tomcat7+JDK7 思维导图: 表结构 目录结构 依赖 <dependencies> <dependency> < ...

  4. python连接MySQL pymysql模块,游标,SQL注入问题,增删改查操作

    pymysql模块 pymysql是用python控制终端对MySQL数据库进行操作的第三方模块 import pymysql # 1.连接数据库 client = pymysql.connect( ...

  5. 洗礼灵魂,修炼python(91)-- 知识拾遗篇 —— pymysql模块之python操作mysql增删改查

    首先你得学会基本的mysql操作语句:mysql学习 其次,python要想操作mysql,靠python的内置模块是不行的,而如果通过os模块调用cmd命令虽然原理上是可以的,但是还是不太方便,那么 ...

  6. SQLite数据库_实现简单的增删改查

    1.SQLite是一款轻量型的数据库是遵守ACID(原子性.一致性.隔离性.持久性)的关联式数据库管理系统,多用于嵌入式开发中. 2.Android平台中嵌入了一个关系型数据库SQLite,和其他数据 ...

  7. 4.SSM整合_多表_多对多的增删改查

    多对多关系,课程和学生 接口 public interface CourseMapper { /** * 获取所有课程 * @return * @throws Exception */ public ...

  8. 3.SSM整合_多表_一对多的增删改查

    1.配置文件跟上一章一样,这里就不多写了,主要是Mapper映射文件,一对多反过来就是多对一 一 接口 public interface CategoryMapper { public void ad ...

  9. jQuery 第四章 实例方法 DOM操作_基于jQuery对象增删改查相关方法

    .next() .prev() .nextAll() .prevAll() .prevUntil() .nextUntli() .siblings() .children() .parent() .p ...

随机推荐

  1. android中文字中间有超链接的实现方法

      1.XML里写: <resources> <string name="ACCOUNT_REGISTER_PROMPT_AGREEMENT">点击注册,表 ...

  2. NS3网络仿真(6): 总线型网络

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载.但请保留作者信息 在NS3提供的第一个演示样例first.py中,模拟了一个点对点的网络,接下来的一个演示样例代码模 ...

  3. elipse快捷键大全 elipse快捷键详解

    1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如applic*.xm ...

  4. Atitit. Attilax企业框架 AEF的发展里程总结

    Atitit. Attilax企业框架 AEF的发展里程总结 1. Attilax企业框架and框架发展思想 1 2. AEF框架 2 2.1. 多语言支持,涉及的语言 java ,c# php py ...

  5. 关于release后retainCount还是1的问题

    转自:http://www.cocoachina.com/bbs/read.php?tid=175523 realse之后再调用还能调用的的问题,我做了这么多年也是经常遇到,也曾经试图寻找原因, 就像 ...

  6. CGROUP相关知识

    安装 CentOS 6 yum install libcgroup CentOS 7 yum install libcgroup-tools 使用 默认情况下有几个控制器可以进行限制,分别是 cpus ...

  7. vue2.0 实现click点击当前li,并动态添加class(这种方法不太喜欢)

    1,文件内容 ---- 使用v-for遍历数据 ---- @click="selectSort(item)"添加点击事件,并把每个obj=item传入 ---- v-show=&q ...

  8. error: no matching function for call to 'Ui::GoToCellDialog::setupUi(QDialog*&)' ui.setupUi(dialog); ^

    环境:Qt5.3 参考书是:C++ GUI Qt4编程 问题描述: 按照书中的例子2-2做,编译时遇到的问题,从字面意思看是没有匹配的函数可用,UI::GotoCellDialog类是自动生成的,所以 ...

  9. std::string与output-operator"<<"的兼容问题

    经查阅资料得知,“在某些编译器下std::string,需要使用c_str()才能作为output-operator "<<" 的参数” std::string tit ...

  10. js 開始时间,当前时间,结束时间的比較

    //開始时间不能小于当前时间 function startTimeIsBigThanTotay(startTime){ var startdate = new Date((startTime).rep ...