[jsp] JSTL / EL 문 / 조건에 맞는 레코드 select 하기

seulki·2022년 10월 25일
0

jsp

목록 보기
50/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:choose>
	</table>
</body>
</html>


🎈 search_db.jsp

<%@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>

<%
	String userid = request.getParameter("userid");
	UserDAO udao = new UserDAO();
	UserBean user =   udao.searchId(userid);
	
    // 값을 저장하고
	request.setAttribute("user", user);
    // forward방식으로 정보 유지한 채로 페이지 이동
	pageContext.forward("search_view.jsp");
%>
</body>
</html>


🎈 UserDAO -> 제출 클릭 시 ID가 있으면 ID정보 가져오기

package com.koit.web.dao;

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

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 UserBean searchId(String userid) {
		UserBean userbean = sqlsession.selectOne("User.searchId", userid);
		
		return userbean;
	}
}


🎈 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="searchId" parameterType="string" resultType="com.koit.web.beans.UserBean">
	 	SELECT * FROM TBL_USER tu WHERE USERID = #{userid}
	 </select>
</mapper>


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

0개의 댓글