码迷,mamicode.com
首页 > Web开发 > 详细

图书管理系统 Struts+hibernate

时间:2019-07-11 00:22:55      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:pat   cli   返回   cut   sem   下载   webroot   iter   ssi   

下载链接

目录结构如下

com.action>>
                    BookAction.java
                    LoginAction.java
com.bean>>
                    Admin.java
                    Book.java
                    Admin.hbm.xml
                    Book.hbm.xml
com.dao>>
                    AdminDao.java
                    BookDao.java
com.util>>    
                   HibernateSessionFactory.java
hibernate.cfg.xml
struts.xml
WebRoot>>
                  WEB-INF>>
                                    web.xml
                   book_add.jsp
                   borrow.jsp
                   index.jsp
                   login.jsp
                   main.jsp

BookAction.java>>

package com.action;

import java.util.List;

import com.bean.Book;
import com.dao.BookDao;
import com.opensymphony.xwork2.ActionContext;

public class BookAction {
    private Book book;
    private int id;
    private String name;

    //查询图书
    public void query(){
        BookDao bookDao=new BookDao();
        Book book1=new Book();
        if (!name.isEmpty()) {
            book1=bookDao.findBookByName(name);
        }
        ActionContext.getContext().getSession().put("book", book1);
    }
    
    public String addBook(){
        System.out.println("进入了addBook方法");
        BookDao bookDao=new BookDao();
        if(book.getId() == null){
            bookDao.addBook(book);
            System.out.println("新增书名:"+book.getName());
        }else{
            bookDao.updateBook(book);
            System.out.println("修改书id:"+book.getId());
        }
        
        return bookList();
    }
    public String bookList(){
        BookDao bookDao=new BookDao();
        List<Book> books=bookDao.findBookAll();
        ActionContext.getContext().getSession().put("b", books);
        System.out.println("进入了booklist方法");
        return "main";
    } 
    public String bookEdit(){
        BookDao bookDao=new BookDao();
        Book book=bookDao.findBookById(id);
        ActionContext.getContext().getSession().put("bo", book);
        System.out.println("进入了bookEdit方法,id:"+id);
        return "book_edit";
    }
    //get请求 返回user_add页面
    public String bookAdd(){
        if (ActionContext.getContext().getSession().containsKey("bo")) {
            ActionContext.getContext().getSession().remove("bo");
        }
        
        return "book_add";
    }
    public String delete(){
        BookDao bookDao=new BookDao();
        bookDao.deleteBookById(id);
        System.out.println("进入了delet方法,id:"+id);
        return bookList();
    }
    
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }





    public String getName() {
        return name;
    }





    public void setName(String name) {
        this.name = name;
    }
    
}

LoginAction.java>>

package com.action;

import com.bean.Admin;
import com.dao.AdminDao;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction {
    private Admin admin;

    public String login(){
        
        if (!admin.getName().isEmpty()&&!admin.getPassword().isEmpty()) {
            AdminDao adminDao=new AdminDao();
            Admin user=adminDao.findAdminByName(admin.getName());
            if (user.getPassword().equals(admin.getPassword())) {
                ActionContext.getContext().getSession().put("flag", "0");
                ActionContext.getContext().getSession().put("user", user);
                System.out.println("成功登录!用户名:"+user.getName());
                return "main";
            }
        }
        ActionContext.getContext().getSession().put("flag", "1");
        System.out.println("成功失败!用户名:"+admin.getName());
        return "login";
    }
    
    public String loginOut(){
        boolean flag=ActionContext.getContext().getSession().containsKey("user");
        if (flag) {
            ActionContext.getContext().getSession().remove("user");
            return "loginOut";
        }
        System.out.println(flag);
        return "loginOut";
    }
    public String main(){
        boolean flag=ActionContext.getContext().getSession().containsKey("user");
        if (flag) {
            return "main";
        }else {
            return "login";
        }
        
    }
    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }
}

Admin.java>>


package com.bean;


import java.sql.Timestamp;


/**
* Admin entity. @author MyEclipse Persistence Tools
*/


public class Admin implements java.io.Serializable {


// Fields


private Integer id;
private String name;
private String password;
private Short sex;
private Short type;
private Timestamp createDate;


// Constructors


/** default constructor */
public Admin() {
}


/** minimal constructor */
public Admin(String name) {
this.name = name;
}


/** full constructor */
public Admin(String name, String password, Short sex, Short type, Timestamp createDate) {
this.name = name;
this.password = password;
this.sex = sex;
this.type = type;
this.createDate = createDate;
}


// Property accessors


public Integer getId() {
return this.id;
}


public void setId(Integer id) {
this.id = id;
}


public String getName() {
return this.name;
}


public void setName(String name) {
this.name = name;
}


public String getPassword() {
return this.password;
}


public void setPassword(String password) {
this.password = password;
}


public Short getSex() {
return this.sex;
}


public void setSex(Short sex) {
this.sex = sex;
}


public Short getType() {
return this.type;
}


public void setType(Short type) {
this.type = type;
}


public Timestamp getCreateDate() {
return this.createDate;
}


public void setCreateDate(Timestamp createDate) {
this.createDate = createDate;
}


}

Book.java>>

package com.bean;

import java.sql.Timestamp;

/**
 * Book entity. @author MyEclipse Persistence Tools
 */

public class Book implements java.io.Serializable {

    // Fields

    private Integer id;
    private String name;
    private String author;
    private String publisher;
    private Short type;
    private Short state;
    private Integer time;
    private String category;
    private Timestamp createDate;

    // Constructors

    /** default constructor */
    public Book() {
    }

    /** minimal constructor */
    public Book(String name) {
        this.name = name;
    }

    /** full constructor */
    public Book(String name, String author, String publisher, Short type, Short state, Integer time, String category,
            Timestamp createDate) {
        this.name = name;
        this.author = author;
        this.publisher = publisher;
        this.type = type;
        this.state = state;
        this.time = time;
        this.category = category;
        this.createDate = createDate;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return this.author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublisher() {
        return this.publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public Short getType() {
        return this.type;
    }

    public void setType(Short type) {
        this.type = type;
    }

    public Short getState() {
        return this.state;
    }

    public void setState(Short state) {
        this.state = state;
    }

    public Integer getTime() {
        return this.time;
    }

    public void setTime(Integer time) {
        this.time = time;
    }

    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Timestamp getCreateDate() {
        return this.createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

}

Admin.hbm.xml 自动生成

Book.hbm.xml 自动生成

AdminDao.java>>

package com.dao;

import org.hibernate.Query;
import org.hibernate.Session;

import com.bean.Admin;
import com.util.HibernateSessionFactory;

public class AdminDao {
    //查询一条
        public Admin findAdminByName(String name){
            Session session=HibernateSessionFactory.getSession();
            String sql="from Admin ad where ad.name=\‘"+name+"\‘";
            Query query = session.createQuery(sql);
//            query.setString(1, name);
            Admin admin = (Admin) query.list().get(0);
            HibernateSessionFactory.closeSession();
            return admin;
        }
}

BookDao.java>>

package com.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.bean.Admin;
import com.bean.Book;
import com.util.HibernateSessionFactory;

public class BookDao {
    //增加一条
    public boolean addBook(Book book){
        Session session=HibernateSessionFactory.getSession();
        Transaction transaction=session.beginTransaction();
        session.save(book);
        System.out.println("保存成功!!");
        transaction.commit();
        HibernateSessionFactory.closeSession();
        return true;
    } 
    //获取所有
    public List<Book> findBookAll(){
        Session session=HibernateSessionFactory.getSession();
        Transaction transaction=session.beginTransaction();
        String hql="from Book";
        Query query=session.createQuery(hql);
        List<Book> list =query.list();
        transaction.commit();
        HibernateSessionFactory.closeSession();
        return list;
    }
    //查询一条
    public Book findBookById(int id){
        Session session=HibernateSessionFactory.getSession();
        Book book=session.get(Book.class,id);
        HibernateSessionFactory.closeSession();
        return book;
    }
    //查询一条
    public Book findBookByName(String name){
        Session session=HibernateSessionFactory.getSession();
        String sql="from Book where name like \‘"+name+"%\‘";
        Query query = session.createQuery(sql);
//                query.setString(1, name);
        Book book=new Book();
        if (query.list().size()>0) {
            book = (Book) query.list().get(0);
        }
        HibernateSessionFactory.closeSession();
        return book;
    }
    //删除一条
    public boolean deleteBookById(int id){
        Session session=HibernateSessionFactory.getSession();
        Transaction trans=session.beginTransaction();
        Book book=session.get(Book.class,id);
        session.delete(book);
        trans.commit();
        HibernateSessionFactory.closeSession();
        return true;
    }
    //更新
    public boolean updateBook(Book book){
        Session session=HibernateSessionFactory.getSession();
        Transaction trans=session.beginTransaction();
        session.update(book);
        trans.commit();
        HibernateSessionFactory.closeSession();
        return true;
    }
}

HibernateSessionFactory.java 自动生成

hibernate.cfg.xml 自动生成

hibernate.cfg.xml>>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

    <package name="default" extends="struts-default">
        <!--book相关  -->
        <action name="add_book" class="com.action.BookAction" method="addBook">
            <result name="main" type="redirect">/main.jsp</result>
        </action>
        
    
        <action name="delete_book" class="com.action.BookAction" method="delete">
            <result name="book" type="redirect">/book.jsp</result>
        </action>
        
        <action name="edit_book" class="com.action.BookAction" method="bookEdit">
            <result name="book_edit" type="redirect">/book_add.jsp</result>
        </action>
        <action name="book_add" class="com.action.BookAction" method="bookAdd">
            <result name="book_add" type="redirect">/book_add.jsp</result>
        </action>
    
        
        
        <!--登录相关  -->
        <action name="login" class="com.action.LoginAction" method="login">
            <result name="login" type="redirect">/login.jsp</result>
            <!-- 错误可忽略 -->
            <result name="main" type="redirect">/main.action</result>
        </action>
        <action name="loginOut" class="com.action.LoginAction" method="loginOut">
            <result name="loginOut" type="redirect">/login.jsp</result>
            
        </action>
        <action name="main" class="com.action.LoginAction" method="main">
            <result name="main">/main.jsp</result>
            <result name="login">/login.jsp</result>
        </action>
    </package>
</struts>    

book_add.jsp>>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>图书修改新增页面</title>
    <link rel="stylesheet" type="text/css" href="<%=basePath%>static/layui/css/layui.css">
      <script src="<%=basePath%>static/layui/layui.js" charset="utf-8"></script>
  </head>
  <body>
    <h1>书详情页</h1>
    <div class="layui-container">  
      <div class="layui-row">
    <form class="layui-form" action="add_book.action" method="post">
        <input name="book.id" type="hidden" value=<s:property value="#session.bo.id"/>/>
    <div class="layui-form" lay-filter="layuiadmin-form-useradmin" id="layuiadmin-form-useradmin" style="padding: 20px 0 0 0;">
    <div class="layui-form-item">
      <label class="layui-form-label">书名*</label>
      <div class="layui-input-inline">
        <input type="text" name="book.name" lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input" value=<s:property value="#session.bo.name"/>>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">作者*</label>
      <div class="layui-input-inline">
        <input type="text" name="book.author" lay-verify="required" placeholder="请输入作者名" autocomplete="off" class="layui-input" value=<s:property value="#session.bo.author"/>>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">出版社*</label>
      <div class="layui-input-inline">
        <input type="text" name="book.publisher" lay-verify="required" placeholder="请输入出版社" autocomplete="off" class="layui-input" value=<s:property value="#session.bo.publisher"/>>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">所属类别*</label>
      <div class="layui-input-inline">
        <input type="text" name="book.category" lay-verify="required" placeholder="请输入类别" autocomplete="off" class="layui-input" value=<s:property value="#session.bo.category"/>>
      </div>
    </div>

    <div class="layui-form-item">
    <label class="layui-form-label">是否可借</label>
    <div class="layui-input-block">
      <input type="radio" name="book.state" value="1" title="可借" <s:if test="#session.bo==null||#session.bo.state==1">checked</s:if>>
      <input type="radio" name="book.state" value="0" title="不可借" <s:if test="#session.bo.state!=1">checked</s:if>>
    </div>
  </div>
   <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
  </form>
  </div>
  </div>
<script>
    layui.use(form,function(){
       var form = layui.form;
           //刷新界面 所有元素

           form.render()
    });
</script>
  </body>
</html>

borrow.jsp>>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>借书</title>
  </head>
<body class="layui-layout-body">
<h1>借书</h1>
<div class="layui-container">  
    <form action="borrowQuery.action" method="post">
             <div class="layui-form-item">
              <label class="layui-form-label">请输入书名</label>
              <div class="layui-input-block layui-col-md5">
                <input type="text" name="name" required="true" lay-verify="required" placeholder="请输入书名" autocomplete="off" class="layui-input">
              </div>
            </div>
        <button type="submit" class="layui-btn layui-col-md3 layui-col-md-offset3">查询</button>
    </form>
<s:if test="#session.book!=null&&#session.book.id">
    <table class="layui-table" style="margin-top:30px">
      <thead>
        <tr>
          <th>ID</th>
          <th>书名</th>
          <th>作者</th>
          <th>出版社</th>
          <th>类别</th>
          <th>是否可借</th>
        </tr> 
      </thead>
      <tbody>
        <tr>
          <td><s:property value="#session.book.id" /></td>
          <td><s:property value="#session.book.name" /></td>
          <td><s:property value="#session.book.author" /></td>
          <td><s:property value="#session.book.publisher" /></td>
          <td><s:property value="#session.book.category" /></td>
          <td>
              <s:if test="#session.book.state==1">可借</s:if>
              <s:if test="#session.book.state!=1">不可借</s:if>
          </td>
        </tr>
      </tbody>
    </table>
    <a onClick="layer.msg(‘当前书已被借走!‘)" href="<s:if test="#session.book.state!=1">javascript:volid(0);</s:if><s:if test="#session.book.state==1">borrowBook.action?id=<s:property value="#session.book.id"/></s:if>" 
    class="layui-btn layui-btn-normal layui-col-md3 layui-col-md-offset3 <s:if test="#session.book.state!=1">layui-btn-disabled</s:if>">确认借书</a>
    </s:if>    
</div>
<script>
</script>
</body>
</html>

login.jsp>>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登录</title>
  </head>
  
  <body>
     
  <div class="layadmin-user-login-main" style="margin-top:100px">
      <div class="layadmin-user-login-box layadmin-user-login-header">
        <h2>图书馆管理系统</h2>
        <p>图书馆后台管理系统</p>
      </div>
      <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
      <form action="login.action" method="post">
    
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
          <input type="text" name="admin.name" value="admin" id="LAY-user-login-username" lay-verify="required" placeholder="用户名" class="layui-input">
        </div>
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
          <input type="password" name="admin.password" value="123" id="LAY-user-login-password" lay-verify="required" placeholder="密码" class="layui-input">
        </div>
   
        
        <div class="layui-form-item">
          <button class="layui-btn layui-btn-fluid" lay-submit="" lay-filter="LAY-user-login-submit">登 入</button>
        </div>
      
      </form>
      </div>
    </div>
  </body>
</html>

main.jsp>>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>书列表</title>
   
  </head>
  
  <body>
     <h1>书列表</h1>
     
     <div style="margin: 10px;">
          <a href="book_add.action" target="main" class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加图书</a>
        </div>
    <table class="layui-table">
     
      <thead>
    
        <tr>
          <th>ID</th>
          <th>书名</th>
          <th>作者</th>
          <th>出版社</th>
          <th>类别</th>
          <th>是否可借</th>
          <th>操作</th>
        </tr> 
      </thead>
      <tbody>
        <s:iterator value="#session.b" var="i">
        <tr>
          <td><s:property value="#i.id" /></td>
          <td><s:property value="#i.name" /></td>
          <td><s:property value="#i.author" /></td>
          <td><s:property value="#i.publisher" /></td>
          <td><s:property value="#i.category" /></td>
          <td>
              <s:if test="#i.state==1">可借</s:if>
              <s:if test="#i.state!=1">不可借</s:if>
          </td>
          <td>
              <a href="edit_book.action?id=<s:property value=‘#i.id‘/>" class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
              <a href="delete_book.action?id=<s:property value=‘#i.id‘/>" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
          </td>
          
        </tr>
      </s:iterator>
      </tbody>
    </table>
  </body>
</html>

web.xml

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

完!!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

图书管理系统 Struts+hibernate

标签:pat   cli   返回   cut   sem   下载   webroot   iter   ssi   

原文地址:https://www.cnblogs.com/yangchas/p/11167445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!