1.首先需要在原有的Struts2框架为基础。
要在src下创建各个分层
这分别需要在按层级建包。首先创建com包再在com包下建hnpi包再在com.hnpi包下建action,been,dao,interceptor,service,util包,还要在com.hnpi.service,com.hnpi.dao下建impl包,如上图所示。
2.下面创建视图层,注意还是需要按层级,不同的视图需要放在创建不同的文件下:如图;
在这里,我们创建了book和user两个文件夹,用来区分不同的视图,两个文件夹下有booklist.jsp和login.jsp.其中的代码段如下:
login.jsp:
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"utf-8"%>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
密码&#xff1a;
booklist.jsp&#xff1a;
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"UTF-8"%>
<%&#64; taglib prefix&#61;"s" uri&#61;"/struts-tags" %>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
book/toAddBook&#39;>新增图书ID 书名 作者 ISBN 出版社 操作 更新 删除
下面要编写各包下action的代码了
首先action层下的代码如下&#xff1a;
BookAction.java:
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 &#61; book;}/*** 图书列表* &#64;return*/public String bookList() {BookService bookService &#61; new BookServiceImpl();List
LoginAction.java:
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 &#61; name;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd &#61; pwd;}public String login(){System.out.println(name&#43;":"&#43;pwd);if(name !&#61;null &&!"".equals(name) && pwd !&#61;null &&!"".equals(pwd)){//判断if(name.equals("fuxian")&&pwd.equals("123")){Map
been层的代码如下&#xff1a;
Been.java:
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 &#61; id;}public String getBookName() {return bookName;}public void setBookName(String bookName) {this.bookName &#61; bookName;}public String getBookAuthor() {return bookAuthor;}public void setBookAuthor(String bookAuthor) {this.bookAuthor &#61; bookAuthor;}public String getBookIsbn() {return bookIsbn;}public void setBookIsbn(String bookIsbn) {this.bookIsbn &#61; bookIsbn;}public String getBookPublish() {return bookPublish;}public void setBookPublish(String bookPublish) {this.bookPublish &#61; bookPublish;}public Book() {super();}public Book(int id, String bookName, String bookAuthor, String bookIsbn,String bookPublish) {super();this.id &#61; id;this.bookName &#61; bookName;this.bookAuthor &#61; bookAuthor;this.bookIsbn &#61; bookIsbn;this.bookPublish &#61; bookPublish;}public String toString() {return "Book [bookAuthor&#61;" &#43; bookAuthor &#43; ", bookIsbn&#61;" &#43; bookIsbn&#43; ", bookName&#61;" &#43; bookName &#43; ", bookPublish&#61;" &#43; bookPublish&#43; ", id&#61;" &#43; id &#43; "]";}}
dao层的代码如下&#xff1a;
BookDao.java:
package com.hnpi.dao;import java.util.List;import com.hnpi.bean.Book;public interface BookDao {public List
dao下的impl层代码如下&#xff1a;
BookDaoFileImpl.java
package com.hnpi.dao.impl;import java.util.List;import com.hnpi.bean.Book;
import com.hnpi.dao.BookDao;public class BookDaoFileImpl implements BookDao{public List
BookDaoImpl.java&#xff1a;
package com.hnpi.dao.impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;import com.hnpi.bean.Book;
import com.hnpi.dao.BookDao;
import com.hnpi.util.DBUtil;public class BookDaoImpl implements BookDao {public List
BookDaoRedisImpl.java
package com.hnpi.dao.impl;import java.util.List;import com.hnpi.bean.Book;
import com.hnpi.dao.BookDao;public class BookDaoRedisImpl implements BookDao{public List
interceptor层下的代码&#xff1a;
UserInterceptor.java&#xff1a;
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{&#64;Overridepublic String intercept(ActionInvocation invocation) throws Exception {String user&#61;(String) ActionContext.getContext().getSession().get("user");if (user&#61;&#61;null||"".equals(user)) {return "fail";}return invocation.invoke();}}
service层下的代码&#xff1a;
BookService.java&#xff1a;
package com.hnpi.service;import java.util.List;import com.hnpi.bean.Book;public interface BookService {/*** 查询所有图书* &#64;return*/public List
service层下impl的代码&#xff1a;
BookServiceImpl.java
package com.hnpi.service.impl;import java.util.List;import com.hnpi.bean.Book;
import com.hnpi.dao.BookDao;
import com.hnpi.dao.impl.BookDaoFileImpl;
import com.hnpi.dao.impl.BookDaoImpl;
import com.hnpi.service.BookService;public class BookServiceImpl implements BookService {public List
// BookDao bookDao &#61; new BookDaoRedisImpl();
// BookDao bookDao &#61; new BookDaoFileImpl();return bookDao.selectBooks();}}
util层下代码&#xff1a;
DBUtil.java&#xff1a;
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 &#61; "jdbc:sqlserver://localhost:1433;databaseName&#61;MyDB";String user &#61; "sa";String pwd &#61; "1";Connection conn &#61; null;try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");conn &#61; DriverManager.getConnection(url, user, pwd);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;
}public static void closeConn(Connection conn, PreparedStatement ps,ResultSet rs) {try {if (conn !&#61; null) {conn.close();}} catch (SQLException e) {e.printStackTrace();}try {if (ps !&#61; null) {ps.close();}} catch (SQLException e) {e.printStackTrace();}try {if (rs !&#61; null) {rs.close();}} catch (SQLException e) {e.printStackTrace();}}
}
最后需要配置struts.xml
以上就是Struts2框架下的图书管理系统方法和代码&#xff0c;希望可以对学者有所帮助。