Struts2 simple library management system

First, the realization of functions:

  • Logged-in user (login information will be stored in the session)
  • Display a list of books (remove all book information from the database, List deposited into the collection)
  • Click here for details, you can view specific information about the book.

Second, the project structure:

Write pictures described here

com.hlk.action-Controller-controlling layer
com.hlk.dao- data persistence layer
com.hlk.pojo- Common bean object
com.hlk.util- tools

Third, run shot:

Extracted from the book database information into a set of List, then stored in the session set.
Username and books are acquired display information from the session.

home page:

Write pictures described here

Click for details:

Write pictures described here

Four: code implementation:

1, database tools: JdbcUtil.java

public class JdbcUtil {
	
	private static final String driver="com.mysql.jdbc.Driver";
	private static final String url="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";
	private static final String username="root";
	private static final String password="";
	
	public static Connection getConnection() {
		Connection conn=null;
		try {
			Class.forName(driver);				//注册驱动
			conn = DriverManager.getConnection(url,username,password);//获得连接对象
		} catch (ClassNotFoundException e) {	//捕获驱动类无法找到异常
			e.printStackTrace();										
		} catch (SQLException e) {					//捕获SQL异常
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void close(Connection conn,Statement st,ResultSet rs){
		try {
			if(rs!=null)
				rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(st!=null)
				st.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(conn!=null)
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

2, Dao layer (data persistence layer)

UserDao.java:

public class UserDao {
	
	public User findUser(String username,String password) {
		Connection conn = JdbcUtil.getConnection();
		PreparedStatement stmt = null;	
		ResultSet rs = null;
		User user = null;
		String sql = "SELECT username,password FROM user WHERE";
		sql+=" username = ? AND password = ?";
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, username);
	        stmt.setString(2, password);
	        rs = stmt.executeQuery();
	        while (rs.next()) {
	        	user = new User();
	        	user.setUsername(rs.getString(1));
	        }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtil.close(conn, stmt, rs);
		}
        return user;
	}
}

BookDao.java:

public class BookDao {
	
	public List<Book> list() {
		List<Book> books = new ArrayList<Book>();
		Connection conn = JdbcUtil.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			String sql = "select * from book";
			rs = st.executeQuery(sql);
			while(rs.next()) {
				Book book = new Book();
				book.setId(rs.getInt("id"));
				book.setName(rs.getString("name"));
				book.setInfo(rs.getString("info"));
				book.setDate(rs.getDate("date"));
				book.setAuthor(rs.getString("author"));
				books.add(book);
			}
			return books;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			JdbcUtil.close(conn, st, rs);
		}
		return null;
	}
}

3, Action categories: (Controller Control Layer)

UserAction.java:

public class UserAction extends ActionSupport implements ModelDriven<User>,SessionAware {
	
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	User user = new User();
	public User getModel() {
		return user;
	}
	
 	public String login() throws Exception {
 		
 		UserDao userDao = new UserDao();
 		User session_user = userDao.findUser(user.getUsername(),user.getPassword());
 		if(session_user == null) {
 			return "error";
 		} else {
 			session.put("session_user", user.getUsername());
 			return "success";
 		}
	}
 
}

BookAction.java:

public class BookAction extends ActionSupport implements SessionAware {
	
	private static final long serialVersionUID = 1L;
	
	private Map<String, Object> session;
	
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	List<Book> books = new ArrayList<Book>();
	public String list() throws Exception {
		// TODO Auto-generated method stub
		BookDao bookDao = new BookDao();
		books = bookDao.list();
		session.put("books", books);
		return "success";
	}

}

4, View layer:

book.jsp:

<body>
    <div class="container">
        <h1>图书管理系统</h1>
		系统提示:您好,<s:property value="#session.session_user"/>,登录成功!
		<hr/>
        <h3>所有图书 <a href="" type="button" class="btn btn-primary btn-sm">添加</a></h3>

           <table class="table table-bordered table-striped" id="table">
               <tr>
                   <th>ID</th>
                   <th>书名</th>
                   <th>照片</th>
                   <th>信息</th>
                   <th>发布日期</th>
                   <th>作者</th>
                   <th>操作</th>
               </tr>
               <s:iterator id="book" value="#session.books" status="vs">
                 <tr>
                     <td><s:property value="#book.id"/></td>
                     <td>《<s:property value="#book.name"/>》</td>
                     <td><img src="images/<s:property value="#book.name"/>.jpg" /></td>
                     <td><s:property value="#book.info"/></td>
                     <td><s:property value="#book.date"/></td>
                     <td><s:property value="#book.author"/></td>
                     <td>
                         <a id="<s:property value="#vs.index+1"/>" onclick="editInfo(this)" type="button" class="btn btn-sm btn-success" data-toggle="modal" data-target="#my_modal">详情</a>
                         <a type="button" class="btn btn-sm btn-warning">修改</a>
                         <a type="button" class="btn btn-sm btn-danger">删除</a>
                     </td>
                 </tr>
               </s:iterator>
           </table>
    </div>
	
	<div class="modal" id="my_modal" tabindex="-1" role="dialog" aria-labelleby="myModallabel">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button  type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title" id="myModallabel">详细信息</h4>
				</div>
				<div class="modal-body" id="b">
					书名:	<scan id="b_name"></scan><hr/>
					信息:	<scan id="b_info"></scan><hr/>
					作者:	<scan id="b_author"></scan><hr/>
					日期: 	<scan id="b_date"></scan>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
					<button type="button" class="btn btn-primary">Save</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script type="text/javascript">
	function editInfo(obj) {
		var id = $(obj).attr("id");
	    //获取表格中的一行数据  
	    var t = $('#table')[0];
	    var name = t.rows[id].cells[1].innerText;  
	    var info = t.rows[id].cells[3].innerText;  
	    var date = t.rows[id].cells[4].innerText;  
	    var author = t.rows[id].cells[5].innerText;  
	    //向模态框中传值  
	    $('#b_name').text(name);
	    $('#b_info').text(info);  
	    $('#b_date').text(date); 
	    $('#b_author').text(author);
	}
</script>
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>

As used herein, the <s: iterator> </ s: iterator> tag iterations, index element traversing through stutus.index. Here note, index of the element is zero-based.

<a id="<s:property value="#vs.index+1"/>" onclick="editInfo(this)" type="button" class="btn btn-sm btn-success" data-toggle="modal" data-target="#my_modal">详情</a>

Click on details, activate modal box at the same time, it calls editInfo methods, written information to the modal box.

Published 128 original articles · won praise 239 · views 330 000 +

Guess you like

Origin blog.csdn.net/HLK_1135/article/details/61191361