1. 程式人生 > >JavaEE_JSP_完整的JSTL+EL表示式將資料顯示出來的流程

JavaEE_JSP_完整的JSTL+EL表示式將資料顯示出來的流程

總共分為五個部分,

1.EL+JSTL前端JSP頁面

2.servlet(傳遞資料)

3.dao(操作資料庫)

4.javaBean(ORM)

5.資料庫表設計

1.EL+JSTL前端JSP頁面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%-- page import="com.user.bean.VimUserInformationObject" --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>使用者許可權管理</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <div>
 <span style="color:red;font-weight:bold">
 	<%
 	 if(session.getAttribute("login_user")!=null){
 	 out.println("尊敬的"+session.getAttribute("login_user")+"<br/>");
 	 }
     %>
  </span>
  </div>
  <div>
  	<a href="user/normal/servlet/loginOutUser">-登出-</a>
  	<a href="jsp/user/modify_password.jsp">-修改密碼-</a>
  </div>
  <div>
  	  <div>
  	  	<span style="color:red;font-weight:bold">
 			<%
 	 			if(request.getAttribute("tips")!=null){
 	 				out.println(request.getAttribute("tips")+"<br/>");
 	 			}
    	 	%>
    	 </span>
  	  </div>
      <form method="post" action="">
          <table>
              <tr>
                  <td>序號</td>
                  <td>使用者名稱</td>
                  <td>許可權</td>
              </tr>
              <c:forEach items="${allUserAuthority}" var="item" varStatus="current">
              	<tr>
              		<td>${current.index}</td>
              		<td>${item.user_name}</td>
              		<td>${item.authority}</td>
              	</tr>
              </c:forEach>
              <!-- 
              	${item['user_name']}
              	${item['authority']}
              	 -->
              <!--
                <tr>
                  <td>1</td>
                  <td>szh</td>
                  <td><input name="username" type="checkbox"/></td>
                  <td><input type="checkbox"/></td>
              </tr> 
               -->
           
          </table>
          <!-- 
          <input type="submit" value="確認刪除" onclick="return confirm('確認刪除?');"/>
          <input type="reset" value="重選"> 
           -->
      </form>
  </div>
  </body>
</html>


2.servlet

package com.user.manage.servlet;

import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.user.bean.VimUserInformationObject;
import com.user.dao.MultUserInformationDao;

public class ManagerUserAuthorityServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public ManagerUserAuthorityServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	// 響應客戶端請求的方法
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, java.io.IOException {
		String tips = "";
		// Servlet本身並不輸出響應到客戶端,因此必須將請求轉發到檢視頁面
		RequestDispatcher rd;

		try {
			MultUserInformationDao userInformationDao = new MultUserInformationDao();

			// 暫時將所有使用者的資料放到登入Servlet處理,之後進行優化
			List<VimUserInformationObject> allUserAuthorityObjects = userInformationDao
					.query();
			request.setAttribute("allUserAuthority", allUserAuthorityObjects);
			//
			//

			// 獲取轉發物件
			rd = request
					.getRequestDispatcher("/WEB-INF/jsp/user/manager_authority.jsp");
			rd.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

3.dao(操作資料庫)
package com.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.base.dao.BaseDao;
import com.user.bean.VimUserInformationObject;
import com.util.DBconnect;

//字首mult 表示多表聯查
public class MultUserInformationDao extends BaseDao {
	public MultUserInformationDao() {
		super();
	}

	public List<VimUserInformationObject> query() throws Exception {
		List<VimUserInformationObject> userInformationList = new ArrayList<>();
		Connection conn = null;
		PreparedStatement ps = null; // 建立PreparedStatement 物件
		ResultSet rs = null;
		String sql = new String(
				"SELECT u.user_id, u.user_name, a.authority FROM user AS u, user_authority as A"
						+ " WHERE a.user_id = u.user_id"
						+ " ORDER BY u.user_id");

		try {
			conn = DBconnect.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				VimUserInformationObject userInformationObject = new VimUserInformationObject(
						rs.getInt("user_id"), rs.getString("user_name"),
						rs.getInt("authority"));
				userInformationList.add(userInformationObject);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBconnect.closeAllConnection(rs, ps, conn);
		}
		return userInformationList;
	}

	public static void main(String[] args) {
		MultUserInformationDao dao = new MultUserInformationDao();
		try {
			List<VimUserInformationObject> userInformationList = (ArrayList<VimUserInformationObject>) dao
					.query();
			if (!userInformationList.isEmpty()) {
				for (VimUserInformationObject tmp : userInformationList) {
					System.out.println("使用者ID:" + tmp.getUser_id() + "  使用者name:"
							+ tmp.getUser_name() + "  使用者許可權:"
							+ tmp.getAuthority());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

4.javaBean(ORM)
package com.user.bean;


//對應User表 和 User_authority 聯查出來的資料, 字首vim表示虛擬化
public class VimUserInformationObject {

	private Integer user_id;
	private String user_name;
	private Integer authority;

	{
		user_id = 0;
		user_name = "";
		authority = 0;
	}

	public Integer getUser_id() {
		return user_id;
	}

	public void setUser_id(Integer user_id) {
		this.user_id = user_id;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public Integer getAuthority() {
		return authority;
	}

	public void setAuthority(Integer authority) {
		this.authority = authority;
	}

	public VimUserInformationObject(Integer user_id, String user_name,
			Integer authority) {
		super();
		this.user_id = user_id;
		this.user_name = user_name;
		this.authority = authority;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
5.資料庫表設計


最終顯示結果