论坛模块_版块管理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. cxf 生成客户端代码调用服务

    cxf是另一种发布webservice的方式,与jdk提供的相比 jdk提供的是wsimport cxf 提供的是 wsdl2java- d 地址 根据http://www.cnblogs.com/f ...

  2. ubuntu 命令行下查看网页 w3m

    w3m localhost/index.php

  3. C++的引用与const指针的关系以及各种传递方式

    首先我们知道 const int *p 与 int const *p 是一样的,即 *p 是常量:而 int * const p 跟上面是不一样的,即 p 是常量:我们知道引用只是一个别名,与变量共享 ...

  4. python 中 numpy array 中的维度

    简介 numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数.有时候我们可能需要知道某一维的特定维数. 二维情况 >>> import numpy as np ...

  5. WCF实现RESTFul Web Service

    共同学习了前面一些概念,终于开始正题了哈.RESTful的Web Service调用直观,返回的内容容易解析.这里先会描述一个简单的场景--Web Service提供一个方法来搜索个人信息,传入人名, ...

  6. Ubuntu 14.04主机上部署k8s集群

    部署结构 3台虚拟机,其中1台作为master,2台作为minion,都安装了最新版本的docker engine(目前是1.11.2) k8s版本是1.3.0 主要问题 部署步骤基本按照官方文档:h ...

  7. atitit,it人怎么样才容易事业成功?? 有以下五种性格的人容易成功

    atitit,it人怎么样才容易事业成功?? 有以下五种性格的人容易成功 有以下五种性格的人容易成功 一.不撞南墙不回头的人:由于这种人有脚踏实地,做事拼命.不撞南墙不回头的性格特点, 势有不到黄河心 ...

  8. 揭开Altera公司支持OpenCL的设计工具的神秘面纱

    将程序中处理负荷较大的工作分配给加速器LSI的“异构计算(Heterogeneous Computing)”将踏出崭新的一步.美国Altera公司将于2013年内开始面向普通用户提供可自动由按照异构计 ...

  9. JS自定义去除字符串左右两边的指定字符

    function ltrim(str,char){ var pos = str.indexOf(char); var sonstr = str.substr(pos+1); return sonstr ...

  10. 将多个文件夹内的txt合并

    import os import re def text_create(name): """ 创建txt文件夹 """ desktop_pa ...