JavaEE_JSP_完整的JSTL+EL表达式将数据显示出来的流程

来源:互联网 发布:淘宝店铺卖什么比较好 编辑:程序博客网 时间:2024/06/05 20:30

总共分为五个部分,

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.数据库表设计



最终显示结果


0 0