专栏实体类:
/*
* 专栏实体类
*/
public class Category {
private Long cate_id;
private String title;//专栏名称
private User user;//专栏作者
private Set<Article> articles = new HashSet<Article>();
public Long getCate_id() {
return cate_id;
}
public Set<Article> getArticles() {
return articles;
}
public void setArticles(Set<Article> articles) {
this.articles = articles;
}
public void setCate_id(Long cate_id) {
this.cate_id = cate_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
映射配置文件: <hibernate-mapping package="com.dengtuzi.dtb.domain">
<!-- -->
<class name="Category" table="dtb_category">
<id name="cate_id">
<generator class="native"></generator>
</id>
<property name="title" ></property>
<many-to-one name="user" column="cate_user_id" class="User"></many-to-one>
<set name="articles" >
<key column="arti_cate_id"></key>
<one-to-many class="Article" />
</set>
</class>
</hibernate-mapping>
1.专栏列表查询
专栏查询的基本流程:
list方法:
/**
* 查询当前用户的所有专栏
*/
public String list(){
//获取当前登录的用户
HttpSession session = ServletActionContext.getRequest().getSession();
User user = (User) session.getAttribute("loginUser");
//查询数据
List<Category> categories = categoryService.findAll(user);
//设置数据
session.setAttribute("categories", categories);
return "list";
}
因为根据用户查询列表在专栏和文章都要用到,所以把方法放在CommonDao中。
CommonDaoImpl中findAll方法实现:
* 查询所有
*/
public List<T> findAll(User user) {
String hql = "FROM " + entityClass.getSimpleName() + " t WHERE t.user = ?";
List<T> list = (List<T>) this.getHibernateTemplate().find(hql,user);
return list;
}
配置action:
<action name="CategoryAction_*" class="com.dengtuzi.dtb.web.action.CategoryAction" method="{1}">
<result name="list" type="dispatcher">/categories_page</result>
</action>
<!-- 配置访问权限页面方法-->
<action name="*_page">
<result type="dispatcher">/WEB-INF/pages/{1}.jsp</result>
</action>
在页面上使用jstl标签遍历集合数据,将id作为隐藏项,后面对指定项进行操作会用到:
<c:forEach var="cate" items="${categories }">
<p>
<a id="cate_id" hidden="true">${cate.cate_id }</a>
<a style="display:inline-block;width:70%">${cate.title }</a>
<a align="center" style="display:inline-block;width:10%">${cate.articles.size() }</a>
<a style="display:inline-block;width:10%">
<a id="edit_cate_btn">编辑</a> | <a id="dele_cate_btn">删除</a>
</a>
</br></br>
</p>
</c:forEach>
页面效果:
2.添加专栏
页面逻辑:点击添加按钮 --> 显示输入窗口 --> 取消,隐藏窗口/提交,隐藏窗口,提交请求
增加按钮:
<button id="add_cate_btn" onclick="add_cate()">+ 新建专栏</button>
点击事件:
<script type="text/javascript">
//显示添加专栏窗口
function add_cate(){
$("#add_cate_window").show();
};
</script>
输入窗口:
<!-- 添加专栏窗口 -->
<div id="add_cate_window" class="opt_window" hidden="true" style="z-index:997;padding: 10px; background: #fff; border: 1px solid #ccc;position:absolute;left:50%;top:30%;">
<form id="add_cate_form" action="CategoryAction_add.action" method="post">
<table>
<tr>
<td>专栏名称:</td>
<td><input name="category.title" type="text" required="true" /></td>
</tr>
</table>
<br>
<div align="center">
<button type="reset" id="add_cate_cancel" onclick="closeOptWindow()">取消</button>
<button id="add_cate_submit" >确认</button>
</div>
</form>
</div>
取消按钮、确认按钮事件:
<script type="text/javascript">
//关闭输入窗口
function closeOptWindow(){
$("div.opt_window").hide();
}
//绑定添加专栏按钮事件
$("add_cate_submit").click(function(){
closeOptWindow();
$("#add_cate_form").submit();
});
</script>
页面效果:
服务器逻辑:
请求 --> 接收参数 --> 检查名字是否可用 --> 持久化操作 --> 返回列表页面
处理添加专栏请求的action方法:
/**
* 添加专栏
*/
public String add(){
//获取当前登录的用户
User user = (User) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
//将专栏和登陆用户绑定
category.setUser(user);
String result = categoryService.add(category);
//返回添加结果
if(result == "0"){
//添加失败提示信息
ServletActionContext.getRequest().getSession().setAttribute("msg", "专栏已存在");
return "toList";
}
//添加成功,刷新列表页面
return "toList";
}
因为返回列表页面会跨action,而actionError是action级别的,所以此处将提示信息设置到session中,在页面自己提取错误信息并显示:
<script type="text/javascript">
//提示信息
$(function(){
var msg = '${msg }';
if(msg != ""){
//信息不为空,显示信息
alert(msg);
//显示一次后清除提示信息
<% request.getSession().removeAttribute("msg"); %>
}
});
</script>
service层add方法实现:
/**
* 添加专栏
*/
public String add(Category cate) {
//查询专栏名是否已经存在
Category category = categoryDao.findByName(cate);
if(category != null){
//专栏已经存在
return "0";
}
//将新专栏保存
categoryDao.save(cate);
return "1";
}
在CommonDaoImpl中实现通用方法:
/**
* 根据名字查询
*/
public T findByName(T entity) {
List<T> list = this.getHibernateTemplate().findByExample(entity);
if(list.size() != 0 && list != null){
return list.get(0);
}
return null;
}
/**
* 添加/保存
*/
public void save(T entity) {
this.getHibernateTemplate().save(entity);
}
添加配置信息:
<result name="toList" type="redirectAction">
<param name="actionName">CategoryAction_list</param>
</result>
测试结果:
3.编辑专栏
页面逻辑:点击添加按钮 --> 显示输入窗口 --> 取消,隐藏窗口/提交,隐藏窗口,提交请求
页面逻辑和添加专栏差不多,但是编辑需要把专栏对应id传给后台。
删除专栏窗口:
<!-- 删除专栏窗口 -->
<div id="delete_cate_window" class="opt_window" hidden="true" style="z-index:997;padding: 10px; background: #fff; border: 1px solid #ccc;position:absolute;left:50%;top:30%;">
<form id="delete_cate_form" action="CategoryAction_delete.action" method="post">
<table>
<tr>
<td> <input id="delete_cate_id" name="category.cate_id" hidden="true"></td>
<td>你确定要删除:</td>
<td><b><a id="delete_cate_title" name="category.title"></a> </b></td>
</tr>
</table>
<br>
<div align="center">
<button type="reset" id="delete_cate_cancel" onclick="closeOptWindow()">取消</button>
<button type="submit" id="delete_cate_submit" >确认</button>
</div>
</form>
</div>
修改页面展示列表代码,点击时将点击项对应id和title作为参数:
<a id="${cate.cate_id }" name="${cate.title }" class="edit_cate_btn" href="#" onclick="edit_cate($(this).attr('id'))">编辑</a>|
<a class="dele_cate_btn" onclick="delete_cate($(this).prev().attr('id'),$(this).prev().attr('name'))">删除</a>
在点击事件中把jquery获取的id和title传给删除窗口对应的表单。
//删除专栏
function delete_cate(id,name){
//设置点击专栏的id
$("#delete_cate_id").val(id);
//提示专栏名
$("#delete_cate_title").html(name);
$("#delete_cate_window").show();
}
服务器逻辑:
请求 --> 接收参数 --> 检查名字是否可用 --> 持久化操作 --> 返回列表页面
action中修改专栏方法实现:
/**
* 修改专栏名
*/
public String edit(){
//获取当前登录的用户
HttpSession session = ServletActionContext.getRequest().getSession();
User user = (User) session.getAttribute("loginUser");
category.setUser(user);
String result = categoryService.edit(category);
//返回添加结果
if("0".equals(result)){
//失败,添加提示信息
ServletActionContext.getRequest().getSession().setAttribute("msg", "专栏已存在");
}
//刷新页面
return "toList";
}
service层实现:
/**
* 修改专栏
*/
public String edit(Category category) {
//查询专栏名是否已经存在
Category cate = categoryDao.findByName(category);
if(cate != null){
//专栏已经存在
return "0";
}
//将新专栏保存
categoryDao.update(category);
return "1";
}
update和findByName方法已经在CommonDaoImpl中实现。
最后,在Category类映射文件中将set集合inverse属性配置为true,放弃主动维护主外键关系,否则每次编辑专栏都会导致专栏下文章丢失主外键关联。
<set name="articles" inverse="true" >
<key column="arti_cate_id"></key>
<one-to-many class="Article" />
</set>
测试效果: