用Struts2框架做一个简单的图书管理的项目
1.新建一个web项目
2.开始搭建Struts2框架
3.在src下三级菜单分类(这一步很重要,基本就是这个模式)
4.在action下新建两个class文件
package com.hnpi.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.hnpi.bean.Book;
import com.hnpi.service.BookService;
import com.hnpi.service.impl.BookServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport {
private Book book;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
/**
* 图书列表
* @return
*/
public String listBook() {
BookService bookService = new BookServiceImpl();
List<Book> books = new ArrayList<Book>();
books = bookService.selectAllBook();
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";
}
}
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(){
System.out.println(name+":"+pwd);
if(name !=null &&!"".equals(name) && pwd !=null &&!"".equals(pwd)){
//判断
if(name.equals("fuxian")&&pwd.equals("123")){
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("user", name);
return "success";
}else{
return "error";
}
}else{
return "error";
}
}
}
5.在bean下新建
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 + "]";
}
}
6.在interceptor新建
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();
}
}
7.在service下新建
package com.hnpi.service;
import java.util.List;
import com.hnpi.bean.Book;
public interface BookService {
/**
* 增加图书
* @param book
* @return
*/
public boolean addBook(Book book);
/**
* 根据图书ID删除图书
* @param bookId
* @return
*/
public boolean deleteBook(int bookId);
/**
* 更新图书信息
* @param book
* @return
*/
public boolean updateBook(Book book);
/**
* 根据图书编号查询图书
* @param BookId
* @return
*/
public Book selectBookById(int bookId);
/**
* 查询所有图书
* @return
*/
public List<Book> selectAllBook();
}
8.在util下新建(util里是最直接关联Sql的)
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 = "sa";
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();
}
}
}
9.每一步都要在struts.xml里配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="user" extends="struts-default" namespace="/user">
<action name="login" class="com.fuxian.action.LoginAction"
method="login">
<result name="success" type="chain">
<param name="actionName">listBookAction</param>
<param name="namespace">/book</param>
<param name="method">listBook</param>
</result>
<result name="error" type="redirect">/user/login.jsp</result>
</action>
</package>
<package name="default" extends="struts-default" namespace="/book">
<interceptors>
<interceptor name="userInterceptor" class="com.fuxian.interceptor.UserInterceptor"></interceptor>
<interceptor-stack name="selfStack">
<interceptor-ref name="userInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="selfStack"></default-interceptor-ref>
<global-results>
<result name="fail">/user/login.jsp</result>
</global-results>
<action name="listBookAction" class="com.fuxian.action.BookAction"
method="listBook">
<result name="success">/book/listBook.jsp</result>
</action>
<action name="toAddBook" class="com.fuxian.action.BookAction"
method="toAddBook">
<result name="success">/book/addBook.jsp</result>
</action>
<action name="addBook" class="com.fuxian.action.BookAction"
method="addBook">
<result name="success" type="chain">listBookAction</result>
</action>
<action name="toModifyBook" class="com.fuxian.action.BookAction"
method="toModifyBook">
<result name="success">/book/modifuBook.jsp</result>
</action>
<action name="modifyBook" class="com.fuxian.action.BookAction"
method="modifyBook">
<result name="success" type="chain">listBookAction</result>
</action>
<action name="delBook" class="com.fuxian.action.BookAction"
method="delBook">
<result name="success" type="chain">listBookAction</result>
</action>
</package>
</struts>
10.在WebRoot下新建几个文件夹