一。导包

二。构建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>

这样就完成了对新闻简单的增删改查。

结果如下:

posted on 2017-03-14 15:11  云破月丶  阅读(2103)  评论(0编辑  收藏  举报