在完成分类管理之后,我们就来编写有关新闻管理的内容,同样我们需要从增删改查四个方面来进行编写。
新闻管理
一、查询新闻
1.现在包swu.xlc.domain下创建新的类new,并为它增加该有的属性(标题、作者、内容、发布时间、分类)。然后在数据库中创建一张表new,也为其添加相应的属性。需要注意的是在新闻表中我们有一个对应的cagtegory 属性,这个属性对应着数据库中的cid属性,在数据库中是要将cid作为一个外键,然后通过外键就将cid与category表的id连接起来。这里@Entity来声明类是一个实体, @Table(name=“new”)映射到数据库的new表,然后@Id @GeneratedValue(strategy =GenerationType.IDENTITY)
@Column(name=“id”)指明id的映射同时指明id是自动增长,@Column映射了其余的属性,最重要的是使用了注解@ManyToOne @JoinColumn(name=“cid”)来实现实体外键的映射。
package swu.xlc.domain;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="new")
public class New {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private Integer id;
private String title;
private String author;
private String content;
private Date time;
@ManyToOne
@JoinColumn(name="cid")
private Category category;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name="author")
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Column(name="content")
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Column(name="time")
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
2.在包swu.xlc.service下创建接口NewService,然后在包swu.xlc.service.impl下创建类NewServiceImpl实现这个接口。在这里的查询,我们增加了分页的查询,在分页查询中,我们要增加一个分页的方法,现在src目录下创建一个swu.xlc.util包,在该包下创建一个类Page,在我们的分页中,默认的是每页显示五个数据。
public class Page {
private int start;
private int count;
private int total;
private String param;
private static final int defaultCount = 5;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Page (){
count = defaultCount;
}
public Page(int start, int count) {
super();
this.start = start;
this.count = count;
}
public boolean isHasPreviouse(){
if(start==0)
return false;
return true;
}
public boolean isHasNext(){
if(start==getLast())
return false;
return true;
}
public int getTotalPage(){
int totalPage;
if (0 == total % count)
totalPage = total /count;
else
totalPage = total / count + 1;
if(0==totalPage)
totalPage = 1;
return totalPage;
}
public int getLast(){
int last;
if (0 == total % count)
last = total - count;
else
last = total - total % count;
last = last<0?0:last;
return last;
}
@Override
public String toString() {
return "Page [start=" + start + ", count=" + count + ", total=" + total + ", getStart()=" + getStart()
+ ", getCount()=" + getCount() + ", isHasPreviouse()=" + isHasPreviouse() + ", isHasNext()="
+ isHasNext() + ", getTotalPage()=" + getTotalPage() + ", getLast()=" + getLast() + "]";
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
}
package swu.xlc.service;
import java.util.List;
import swu.xlc.domain.New;
import swu.xlc.util.Page;
public interface NewService {
int total();
List<New> list(Page page);
}
package swu.xlc.service.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import swu.xlc.dao.DAOImpl;
import swu.xlc.domain.New;
import swu.xlc.service.NewService;
import swu.xlc.util.Page;
@Service
public class NewServiceImpl implements NewService{
@Autowired DAOImpl dao;
@Override
public int total() {
// TODO Auto-generated method stub
String hql = "select count(*) from New ";
List<Long> long= dao.find(hql);
if(long.isEmpty())
return 0;
Long result= long.get(0);
return result.intValue();
}
@Override
public List<New> list(Page page) {
// TODO Auto-generated method stub
DetachedCriteria dc = DetachedCriteria.forClass(New.class);
dc.addOrder(Order.desc("id"));
return dao.findByCriteria(dc, page.getStart(), page.getCount());
}
}
3.在包swu.xlc.action下创建类NewAction,在action中进行数据的处理和视图的跳转,这里我们的查询是分页进行的,所以在action中的处理也有不同,需要将一些数据传递到前端。
package swu.xlc.action;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import swu.xlc.domain.New;
import swu.xlc.service.NewService;
import swu.xlc.util.Page;
@Namespace("/")
@ParentPackage("mystruts")
@Results(
{
/*新闻管理*/
@Result(name="newlist", location="/WEB-INF/jsp/list_new.jsp"),
@Result(name="newlistpage", type = "redirect", location="/listnew"),
}
)
public class NewAction {
@Autowired
private NewService newService;
private Page page;
private New anew;
List<New> news;
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public NewService getNewService() {
return newService;
}
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
public New getAnew() {
return anew;
}
public void setAnew(New anew) {
this.anew = anew;
}
public List<New> getNews() {
return news;
}
public void setNews(List<New> news) {
this.news = news;
}
@Action("listnew")
public String list() {
if(page==null) {
page=new Page();
}
int total = newService.total();
page.setTotal(total);
news=newService.list(page);
return "newlist";
}
}
4.在WEB-INF/jsp目录下创建list_new.jsp,在这个这个也页面中显示我们后端传递过来的数据。同时我们这里有一个分页的标签,因为我们会在很多地方会使用分页显示,所以我们将其做到一个jsp页面中,然后在需要的地方引用这个页面就可以,所以在WEB-INF/jsp目录下创建page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<script>
$(function(){
$("ul.pagination li.disabled a").click(function(){
return false;
});
});
</script>
<nav>
<ul class="pagination">
<li>
<a href="?page.start=0" aria-label="Previous" >
<span aria-hidden="true">首页</span>
</a>
</li>
<li >
<a href="?page.start=${page.start-page.count}" aria-label="Previous" >
<span aria-hidden="true">上一页</span>
</a>
</li>
<c:forEach begin="0" end="${page.totalPage-1}" varStatus="status">
<li>
<a href="?page.start=${status.index*page.count}" class="current">${status.count}</a>
</li>
</c:forEach>
<li >
<a href="?page.start=${page.start+page.count}" aria-label="Next">
<span aria-hidden="true">下一页</span>
</a>
</li>
<li >
<a href="?page.start=${page.last}" aria-label="Next">
<span aria-hidden="true">尾页</span>
</a>
</li>
</ul>
</nav>
<div>
<table>
<thead>
<tr>
<th>ID</th>
<th>标题</th>
<th>作者</th>
<th>分类名称</th>
<th>发布时间</th>
</tr>
</thead>
<tbody>
<c:forEach items="${news}" var="ns">
<tr>
<td>${ns.id}</td>
<td>${ns.title}</td>
<td>${ns.author}</td>
<td>${ns.category.name}</td>
<td><fmt:formatDate value="${ns.time}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div align="center">
<%@include file="/WEB-INF/jsp/page.jsp" %>
</div>
5.在浏览器中访问 http://localhost:8080/ssh_new/listnew 就可以实现新闻的查询,在控制台也会打印相应的sql语句。
二、增加新闻
没时间更新了,以后有机会再更新吧