该系统有增删改查四个功能
创建Struts2框架不会的可以参考本人第一篇文章
写jsp在WebRoot下的book文件夹里写
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
添加图书
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
图书列表
增加图书
编号
图书名称
作者
出版社
ISBN
操作
更新 删除
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
更新页面
更新内容:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
登录页面
写控制层action
在用户名密码在这里
用户名:admin
密码:123
BookAction.java页面
package com.hnpi.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.hnpi.bean.Book;
import com.hnpi.service.BookService;
import com.hnpi.service.impl.BookServiceImpl;
public class BookAction extends ActionSupport{
private Book book;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
/**
* 查询图书
* @return
*/
public String bookList(){
//从数据库获取数据 进行数据准备 然后将数据传至bookList.jsp
BookService bookService = new BookServiceImpl();
List books = bookService.selectBooks();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("books", books);
return "success";
}
/**
* 准备新增图书
* @return
*/
public String toAddBook() {
return "success";
}
/**
* 新增图书
* @return
*/
public String addBook() {
BookService bookService = new BookServiceImpl();
bookService.addBook(book);
return "success";
}
/**
* 删除图书
* @return
*/
public String delBook(){
BookService bookService = new BookServiceImpl();
bookService.deleteBook(book.getId());
return "success";
}
/**
* 根据id查询图书 并跳转到更新图书页面
* @return
*/
public String selectBookId(){
BookService bookService = new BookServiceImpl();
Book booksId = bookService.selectBookId(book.getId());
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("booksId", booksId);
return "success";
}
/**
* 更新图书
* @return
*/
public String updateBook(){
BookService bookService = new BookServiceImpl();
bookService.updateBook(book);
return "success";
}
// public String updateBook(){
// BookService bookService = new BookServiceImpl();
// bookService.updateBook(book.getId());
// return "success";
// }
}
package com.hnpi.action;
import java.util.Map;
import javax.servlet.http.HttpSession;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String login(){
if(name !=null &&!"".equals(name) && pwd !=null &&!"".equals(pwd)){
//判断
if(name.equals("admin")&&pwd.equals("123")){
Map session = ActionContext.getContext().getSession();
session.put("user", name);
return "success";
}else{
return "fail";
}
}else{
return "fail";
}
}
}
package com.hnpi.bean;
public class Book {
private int id;
private String bookName;
private String bookAuthor;
private String bookIsbn;
private String bookPublish;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookIsbn() {
return bookIsbn;
}
public void setBookIsbn(String bookIsbn) {
this.bookIsbn = bookIsbn;
}
public String getBookPublish() {
return bookPublish;
}
public void setBookPublish(String bookPublish) {
this.bookPublish = bookPublish;
}
public Book() {
super();
}
public Book(int id, String bookName, String bookAuthor, String bookIsbn,
String bookPublish) {
super();
this.id = id;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookIsbn = bookIsbn;
this.bookPublish = bookPublish;
}
public String toString() {
return "Book [bookAuthor=" + bookAuthor + ", bookIsbn=" + bookIsbn
+ ", bookName=" + bookName + ", bookPublish=" + bookPublish
+ ", id=" + id + "]";
}
}
package com.hnpi.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class UserInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String user=(String) ActionContext.getContext().getSession().get("user");
if (user==null||"".equals(user)) {
return "fail";
}
return invocation.invoke();
}
}
package com.hnpi.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
public static Connection getConn() {
String url = "jdbc:sqlserver://localhost:1433;databaseName=MyDB";
String user = "sa";
String pwd = "1";
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void closeConn(Connection conn, PreparedStatement ps,
ResultSet rs) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.hnpi.service;
import java.util.List;
import com.hnpi.bean.Book;
public interface BookService {
/**
* 查询所有图书
* @return
*/
public List selectBooks();
/**
* 增加图书
* @param book
* @return
*/
public boolean addBook(Book book);
/**
* 根据图书ID删除图书
* @param bookId
* @return
*/
public boolean deleteBook(int bookId);
/**
* 根据图书ID查询图书
* @param bookId
* @return
*/
public Book selectBookId(int bookId);
/**
* 更新图书信息
* @param book
* @return
*/
public boolean updateBook(Book book);
//public List updateBook(Book book);
}
在service包的impl包实现该接口
创建一个BookServiceImpl.java的class类
package com.hnpi.service.impl;
import java.util.List;
import com.hnpi.bean.Book;
import com.hnpi.dao.BookDao;
import com.hnpi.dao.impl.BookDaoImpl;
import com.hnpi.service.BookService;
/*
* 作用1 数据的清洗
* 作用2 可以调用多个dao组合成页面需要显示的数据
*/
public class BookServiceImpl implements BookService{
/**
* 查询所有图书
* @return
*/
public List selectBooks() {
// TODO 从数据库获取内容进行数据组织
BookDao bookDao = new BookDaoImpl();
List books = bookDao.selectBooks();
return books;
}
/**
* 增加图书
* @param book
* @return
*/
public boolean addBook(Book book) {
BookDao bookDao = new BookDaoImpl();
return bookDao.addBook(book);
}
/**
* 根据图书ID删除图书
* @param bookId
* @return
*/
public boolean deleteBook(int bookId) {
BookDao bookDao = new BookDaoImpl();
return bookDao.deleteBook(bookId);
}
/**
* 根据图书ID查询图书
* @param bookId
* @return
*/
public Book selectBookId(int bookId) {
BookDao bookDao = new BookDaoImpl();
Book book = bookDao.selectBookId(bookId);
return book;
}
/**
* 更新图书信息
* @param book
* @return
*/
public boolean updateBook(Book book) {
BookDao bookDao = new BookDaoImpl();
return bookDao.updateBook(book);
}
//public List updateBook(Book book) {
//
// BookDao bookDao = new BookDaoImpl();
// return bookDao.updateBook(book);
//
// }
}
bookList
/book
bookList
/user/login.jsp
/user/login.jsp
/book/bookList.jsp
/book/addBook.jsp
bookList
/book/modifyBook.jsp
bookList
bookList
最后还有数据库