<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="kim" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>메인페이지</title>
</head>
<body>
<kim:header />
<hr>
<form action="main.do">
<select name="searchCondition">
<c:forEach var="v" items="${searchMap}">
<option value="${v.value}">${v.key}</option>
</c:forEach>
</select>
<input type="text" name="searchContent" placeholder="검색어를 입력하세요.">
<input type="submit" value="검색">
</form>
<br>
<table border="1">
<tr>
<th>글 번호</th><th>글 제목</th><th>작성자</th><th>조회수</th>
</tr>
<c:forEach var="v" items="${datas}">
<tr>
<td><a href="board.do?bid=${v.bid}">${v.bid}</a></td><td>${v.title}</td><td>${v.writer}</td><td>${v.cnt}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
a태그로 href를 통해 사용자가 게시글 1개를 클릭 할 수 있도록 함
bid 게시글번호를 controller에 넘겨줌
package com.spring.view.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.spring.biz.board.BoardDAO;
import com.spring.biz.board.BoardVO;
@Controller
public class BoardController {
@RequestMapping(value="/board.do")
public String selectBoard(BoardVO bVO, BoardDAO bDAO, Model model) {
System.out.println("BoardController로그");
//bVO.setBid(Integer.parseInt(request.getParameter("bid"))); bVO가 이미 setter를 이미 불러줌 command객체로
//HttpServletRequest request를 인자로 쓸 필요가 없음
model.addAttribute("data",bDAO.selectOne(bVO));
return "board.jsp";
}
}
command객체로 이미 setter를 불러줌 : request를 인자로 쓸 필요가 없음
"data"로 저장
<%@ 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>Board</title>
</head>
<body>
<table border="1">
<tr>
<th>글 번호</th><th>글 제목</th><th>작성자</th>
</tr>
<tr>
<td>${data.bid}</td><td>${data.title}</td><td>${data.content}</td><td>${data.writer}</td>
</tr>
</table>
</body>
</html>
"data"로 불러오기
package com.spring.biz.board;
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 org.springframework.stereotype.Repository;
import com.spring.biz.common.JDBCUtil;
@Repository("boardDAO") // @Component를 상속받은 @
public class BoardDAO {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private final String insert="INSERT INTO BOARD (BID,TITLE,CONTENT,WRITER) VALUES((SELECT NVL(MAX(BID),0)+1 FROM BOARD),?,?,?)";
private final String selectOne="SELECT * FROM BOARD WHERE BID=?"; // getOne
private final String selectAll="SELECT * FROM BOARD ORDER BY BID DESC"; // getAll
private final String selectAll_Writer = "SELECT * FROM BOARD WHERE WRITER LIKE '%' || ? || '%'";
private final String selectAll_Title = "Select * FROM BOARD WHERE TITLE LIKE '%' || ?|| '%'";
private final String update="UPDATE BOARD SET TITLE=?,CONTENT=? WHERE BID=?";
private final String delete="DELETE FROM BOARD WHERE BID=?";
public BoardVO selectOne(BoardVO bVO) {
BoardVO data=null;
System.out.println("BoardDAO 로그 selectOne() 메서드");
conn=JDBCUtil.getConnection();
try {
pstmt=conn.prepareStatement(selectOne);
pstmt.setInt(1, bVO.getBid());
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
data=new BoardVO();
data.setBid(rs.getInt("BID"));
data.setCnt(rs.getInt("CNT"));
data.setContent(rs.getString("CONTENT"));
data.setTitle(rs.getString("TITLE"));
data.setWriter(rs.getString("WRITER"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.close(conn, pstmt);
}
return data;
}
public List<BoardVO> selectAll(BoardVO bVO) {
List<BoardVO> datas=new ArrayList<BoardVO>();
System.out.println("BoardDAO 로그 selectAll() 메서드");
conn=JDBCUtil.getConnection();
try {
if(bVO.getSearchCondition()==null) {
pstmt = conn.prepareStatement(selectAll);
}
else if (bVO.getSearchCondition().equals("WRITER")) {
pstmt = conn.prepareStatement(selectAll_Writer);
pstmt.setString(1, bVO.getSearchContent());
}
else if(bVO.getSearchCondition().equals("TITLE")) {
pstmt = conn.prepareStatement(selectAll_Title);
pstmt.setString(1, bVO.getSearchContent());
}
rs=pstmt.executeQuery();
//pstmt=conn.prepareStatement(selectAll);
//ResultSet rs=pstmt.executeQuery();
while(rs.next()) {
BoardVO data=new BoardVO();
data.setBid(rs.getInt("BID"));
data.setCnt(rs.getInt("CNT"));
data.setContent(rs.getString("CONTENT"));
data.setTitle(rs.getString("TITLE"));
data.setWriter(rs.getString("WRITER"));
datas.add(data);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.close(conn, pstmt);
}
return datas;
}
public boolean insert(BoardVO bVO) {
System.out.println("BoardDAO 로그 insert() 메서드");
conn=JDBCUtil.getConnection();
try {
pstmt=conn.prepareStatement(insert);
pstmt.setString(1, bVO.getTitle());
pstmt.setString(2, bVO.getContent());
pstmt.setString(3, bVO.getWriter());
int rs=pstmt.executeUpdate();
if(rs<=0) {
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
JDBCUtil.close(conn, pstmt);
}
return true;
}
public boolean update(BoardVO bVO) {
System.out.println("BoardDAO 로그 update() 메서드");
conn=JDBCUtil.getConnection();
try {
pstmt=conn.prepareStatement(update);
pstmt.setString(1, bVO.getTitle());
pstmt.setString(2, bVO.getContent());
pstmt.setInt(3, bVO.getBid());
int rs=pstmt.executeUpdate();
if(rs<=0) {
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
JDBCUtil.close(conn, pstmt);
}
return true;
}
public boolean delete(BoardVO bVO) {
System.out.println("BoardDAO 로그 delete() 메서드");
conn=JDBCUtil.getConnection();
try {
pstmt=conn.prepareStatement(delete);
pstmt.setInt(1, bVO.getBid());
int rs=pstmt.executeUpdate();
if(rs<=0) {
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
JDBCUtil.close(conn, pstmt);
}
return true;
}
}
selectAll 쿼리문이 3개가 필요함
bDAO에
getSearchContent() 인데
getContent() 이렇게 쓰면 당연히 안됨
잘보고 하셈
package com.spring.view.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.spring.biz.board.BoardDAO;
import com.spring.biz.board.BoardVO;
import com.spring.biz.member.MemberVO;
@Controller
public class MainController {
@ModelAttribute("searchMap")
public Map<String,String> searchMap(){
Map<String,String> map=new HashMap<String,String>();
map.put("제목", "TITLE");
map.put("작성자", "WRITER");
return map;
}
@RequestMapping(value="/main.do")
public String main(@ModelAttribute("mem")MemberVO mVO, BoardVO bVO, BoardDAO bDAO, Model model) {
System.out.println("searchCondition: "+bVO.getSearchCondition());
System.out.println("searchContent: "+bVO.getSearchContent());
mVO.setMid("test");
mVO.setMpw("1234");
System.out.println("MainController 로그");
// model.addAttribute("mem", mVO);
model.addAttribute("datas", bDAO.selectAll(bVO));
return "main.jsp";
}
}
[ @ModelAttribute("이름") ]
VIEW에서 사용할 데이터를 세팅하려는 목적으로 활용하는 @
이 어노테이션을 사용함으로써 뷰에서 @ModelAttribute("이름")의 "이름"을 사용할 수 있음
이 어노테이션을 C파트에서 사용을 하면
@Controller 파트에서 @MA를 사용하면, @RequestMapping보다 먼저 수행됨
가장면저 처리된다는 특징이 있음
MainController
@ModelAttribute("searchMap")
public Map<String,String> searchMap(){
Map<String,String> map = new HashMap<String,String>();
map.put("제목", "TITLE");
map.put("작성자", "WRITER");
return map;
}
사용하려면 이런싯으로 메서드 위에 사용함
RequestMapping보다 먼저 수행되는 특징이있어서 /main.do보다 먼저
@ModelAttribute - 커맨드 객체앞에 쓰는것보다 메서드 위에 쓰는 경우가 훨씬 많음
<c:forEach var="v" items="${searchMap}">
<option value = "${v.value}">${v.key}</option>
</c:forEach>
뷰가 어떤 검색이 가능한지 몰라도 됨