热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Struts2框架图书管理系统

Struts2框架图书管理系统1.首先需要在原有的Struts2框架为基础。要在src下创建各个分层这分别需要在按层级建包。首先创建com包再在com包下建hnpi包再在com.

Struts2框架图书管理系统

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;
密码&#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 books &#61; new ArrayList();books &#61; bookService.selectBooks();//问题1:如果不通过request 设置属性的方式传值 可不可以通过传递 字符串(json字符串)的方式放在页面进行显示//问题2:action 传递给页面的json字符串 页面怎么处理?HttpServletRequest request &#61; ServletActionContext.getRequest();request.setAttribute("books", books);return "success";}}

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 session &#61; ActionContext.getContext().getSession();session.put("user", name);return "success";}else{return "error";}}else{return "error";}}}

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 selectBooks();}

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 selectBooks() {// TODO 读取文件的代码return null;}}

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 selectBooks() {Connection conn &#61; DBUtil.getConn();String sql &#61; "select * from book";PreparedStatement ps &#61; null;ResultSet rs &#61; null;List books &#61; new ArrayList();try {ps &#61; conn.prepareStatement(sql);rs &#61; ps.executeQuery();while(rs.next()){Book book &#61; new Book();book.setId(rs.getInt(1));book.setBookName(rs.getString(2));book.setBookAuthor(rs.getString(3));book.setBookIsbn(rs.getString(4));book.setBookPublish(rs.getString(5));books.add(book);}} catch (Exception e) {e.printStackTrace();} finally {DBUtil.closeConn(conn, ps, null);}return books;}}

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 selectBooks() {// TODO 访问缓存的代码return null;}}

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 selectBooks();}

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 selectBooks() {BookDao bookDao &#61; new BookDaoImpl();
// 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


/bookbookListbookList/user/login.jsp/user/login.jsp/book/bookList.jsp

以上就是Struts2框架下的图书管理系统方法和代码&#xff0c;希望可以对学者有所帮助。


推荐阅读
author-avatar
执笔W写下我们的故事
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有