[jsp] JSTL / EL 문 / 테이블 전체 select 하기

seulki·2022년 10월 25일
0

jsp

목록 보기
51/51
post-custom-banner

🎈 search_view.jsp

<%@page import="com.koit.web.beans.UserBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	
	<form action="search_db.jsp">
		검색하실 회원의 ID <input type="text" name="userid"></br>
		<input type="button" value="전체회원보기" onclick="location.href='list_db.jsp'">
		<input type="submit">
	</form>
	<hr>
	<table border="1">
		<tr>
			<th>아이디</th>
			<th>비밀번호</th>
			<th>이름</th>
			<th>성별</th>
			<th>상세주소</th>
		</tr>
		<c:choose>
			<c:when test="${!empty requestScope.user }">
				<tr>
					<td>${requestScope.user.userid }</td>
					<td>${requestScope.user.userpw }</td>
					<td>${requestScope.user.username }</td>
					<td>${requestScope.user.usergender }</td>
					<td>${requestScope.user.addrdetail }</td>
				</tr>
			</c:when>
			<c:otherwise>
				<c:forEach var="user" items="${requestScope.List }">
					<tr>
						<td>${user.userid }</td>
						<td>${user.userpw }</td>
						<td>${user.username }</td>
						<td>${user.usergender }</td>
						<td>${user.addrdetail }</td>
						
					</tr>
				</c:forEach>
			</c:otherwise>
		
		</c:choose>
		
	</table>
</body>
</html>


🎈 list_db.jsp

<%@page import="java.util.List"%>
<%@page import="com.koit.web.beans.UserBean"%>
<%@page import="com.koit.web.dao.UserDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		UserDAO udao = new UserDAO();
		
		request.setAttribute("List", udao.getList());
		pageContext.forward("search_view.jsp");
	%>
</body>
</html>


🎈 UserDAO - 전체 레코드를 UserBean 객체로 저장하여 List에 담기

package com.koit.web.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.koit.web.beans.UserBean;
import com.koit.web.mybatis.SqlMapConfig;

public class UserDAO {
	SqlSessionFactory factory = SqlMapConfig.getFactory();
	SqlSession sqlsession;

	public UserDAO() { //생성자
		sqlsession = factory.openSession(true);
		// true : auto commit
	}
	
	public List<Object> getList() {
		List<Object> user = sqlsession.selectList("User.getList");
		return user;
	}
}


🎈 user.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="User">	 
	 <select id="getList" resultType="com.koit.web.beans.UserBean">
	 	SELECT * FROM TBL_USER
	 </select>
</mapper>
  • 전체회원보기 클릭

  • DB에 상세주소정보는 아직 넣지 않음.

profile
웹 개발자 공부 중
post-custom-banner

0개의 댓글