一。導包
二。構建hibernate框架 ,配置靜態單例模式
三。配置web.xml文件
四。配置增刪改查方法
package com.itnba.maya.DAO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import com.itnba.maya.model.News;
public class NewsDao {
private Session session;
public NewsDao(){
session = HibernateUtil.getSession();
}
public List<News> getList(){
List<News> list = new ArrayList<News>();
try{
list = session.createQuery("from News").getResultList();
}
catch(Exception e){
e.printStackTrace();
}
finally {
HibernateUtil.closeSession();
}
return list;
}
public News get(int ids){
News data = new News();
try{
data = (News)session.createQuery("from News where ids=?")
.setParameter(0, ids)
.getSingleResult();
}
catch(Exception e){
e.printStackTrace();
}
finally {
HibernateUtil.closeSession();
}
return data;
}
public void add(News news){
try{
session.beginTransaction();
session.save(news);
session.getTransaction().commit();
}
catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}
finally {
HibernateUtil.closeSession();
}
}
public void update(News news){
try{
session.beginTransaction();
News data = session.get(News.class, news.getIds());
data.setTitle(news.getTitle());
data.setMemo(news.getMemo());
data.setTime(news.getTime());
data.setType(news.getType());
session.update(data);
session.getTransaction().commit();
}
catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}
finally {
HibernateUtil.closeSession();
}
}
public void delete(int ids){
try{
session.beginTransaction();
News data = session.get(News.class, ids);
session.delete(data);
session.getTransaction().commit();
}
catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}
finally {
HibernateUtil.closeSession();
}
}
}
五。配置Action
package com.itnba.maya.controller;
import java.util.List;
import com.itnba.maya.DAO.NewsDao;
import com.itnba.maya.model.News;
import com.opensymphony.xwork2.ActionSupport;
public class NewsAction extends ActionSupport {
private List<News> list; //這里直接定義集合、對象、主鍵的成員變量,更加方便
private News news;
private int ids;
public List<News> getList() {
return list;
}
public void setList(List<News> list) {
this.list = list;
}
public News getNews() {
return news;
}
public void setNews(News news) {
this.news = news;
}
public int getIds() {
return ids;
}
public void setIds(int ids) {
this.ids = ids;
}
public String showAll(){
list = new NewsDao().getList();
return SUCCESS;
}
public String show(){
news = new NewsDao().get(ids);
return SUCCESS;
}
public String add(){
return SUCCESS;
}
public String insert(){
new NewsDao().add(news);
return SUCCESS;
}
public String edit(){
news = new NewsDao().get(ids);
return SUCCESS;
}
public String update(){
new NewsDao().update(news);
return SUCCESS;
}
public String delete(){
new NewsDao().delete(ids);
return SUCCESS;
}
}
六。根據Action可以知道我們的jsp頁面名,創建jsp頁面
1.顯示所有新聞
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>新聞顯示</h1>
<c:forEach items="${list }" var="data">
<a href="News_show?ids=${data.ids}">${data.ids} ${data.title }</a><br>
</c:forEach>
<a href="News_add">添加新新聞</a>
</body>
</html>
2.顯示一個新聞
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>新聞詳情</h1>
${news.title }<br>
${news.time }<br>
${news.memo }<br>
<a href="News_edit?ids=${news.ids }">修改</a> <a href="News_delete?ids=${news.ids }">刪除</a>
</body>
</html>
3.顯示修改界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>修改新聞</h1>
<form action="News_update" method="post">
新聞代號:<input type="hidden" name="news.ids" value="${news.ids }"><input type="text" name="news.ids" disabled="disabled" value="${news.ids }"><br>
新聞標題:<input type="text" name="news.title" value="${news.title }"><br>
發布時間:<input type="text" name="news.time" value="${news.time }"><br>
新聞內容:<textarea rows="10" cols="50" name="news.memo" >${news.memo }</textarea><br>
新聞級別:<input type="text" name="news.type" value="${news.type }"><br>
<input type="submit" value="修改">
</form>
</body>
</html>
4.顯示修改成功界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
修改成功<br>
<a href="News_showAll">返回主界面</a>
</body>
</html>
5.顯示添加界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>添加新聞</h1>
<form action="News_insert" method="post">
標題:<input type="text" name="news.title"><br>
時間:<input type="text" name="news.time"><br>
內容:<textarea rows="10" cols="50" name="news.memo"></textarea><br>
<input type="submit" value="go">
</form>
</body>
</html>
6.顯示添加成功界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
添加成功<br>
<a href="News_showAll">返回主界面</a>
</body>
</html>
7.顯示刪除成功界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
刪除成功<br>
<a href="News_showAll">返回主界面</a>
</body>
</html>
七。配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="*_*" class="com.itnba.maya.controller.{1}Action" method="{2}">
<result>
{1}_{2}.jsp
</result>
</action>
</package>
</struts>
這樣就完成了對新聞簡單的增刪改查。
結果如下:







