EL을 활용한 방법
먼저 EL에 대해 알아보면 Expression Language 로 jsp표준기술로많이 사용되면 간단한 계산이나, attribute에 직접적으로 접근할 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <%@ page contentType="text/html; charset=utf-8" pageEncoding="EUC-KR" import="java.util.*"%><% String[] l = new String[]{"apple","banana","orange","kiwi"}; request.setAttribute("abcd",l); List<String> l2 = new ArrayList<String>(); l2.add("apple"); l2.add("banana"); l2.add("orange"); l2.add("kiwi"); request.setAttribute("xyzz",l2); %> <!DOCTYPE html> <html> <head> <body> ${abcd[0]} ${abcd[1]} ${abcd[2]} ${abcd[3]} ${xyzz[0]} ${xyzz[1]} ${xyzz[2]} ${xyzz[3]} ${xyzz.get(0)} </body> </html> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <%@ taglib prefix="q" uri="http://java.sun.com/jsp/jstl/core"%><% String[] l = new String[]{"apple","banana","orange","kiwi"}; request.setAttribute("abcd",l); List<String> l2 = new ArrayList<String>(); l2.add("apple"); l2.add("banana"); l2.add("orange"); l2.add("kiwi"); request.setAttribute("xyzz",l2); %> <!DOCTYPE html> <html> <head> <body> <q:forEach begin="0" end="5" varStatus="vs"> ${vs.count-1} </q:forEach> <q:forEach items="${abcd}" var="t"> ${t} </q:forEach> <q:forEach items="${xyzz}" var="x"> ${x} </q:forEach> </body> </html> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <%@ page contentType="text/html; charset=utf-8" pageEncoding="EUC-KR" import="java.util.*, banana.*"%> <%@ taglib prefix="q" uri="http://java.sun.com/jsp/jstl/core"%><% Bang06VO vo = new Bang06VO(); vo.setContent("apple"); vo.setAuthor("banana"); List<Bang06VO> l = new ArrayList<Bang06VO>(); l.add( vo ); l.add( vo ); l.add( vo ); request.setAttribute("abcd" , l ); %> <!DOCTYPE html> <html> <head> <body> <q:forEach items="${abcd}" var="t"> ${t.content} ${t.author} <br/> </q:forEach> <table border="1" cellspacing="0" cellpadding="8"> <q:forEach items="${abcd}" var="t"> <tr> <td>${t.content}</td> <td>${t.author}</td> <q:if test="${true}"> <td><img src="down.svg"></td> </q:if> <q:choose> <q:when test="${empty t.fsn}"> <td><img src="down.svg"></td> </q:when> <q:otherwise> <td>[다운]</td> </q:otherwise> </q:choose> </tr> </q:forEach> </table> </body> </html> | cs |
Jdbc Template
Spring에서 jdbc 를 이용하는 방식으로 다시 이를 이용해 console에 간단하게 게시판을 나타내는 코드를 짜봤다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class JdbcTemplate { public <R> List<R> query( String sql , RowMapper<R> rowMapper) throws Exception { List<R> rl = new ArrayList<R>(); Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { Class.forName("org.mariadb.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mariadb://183.111.242.21:3306/pukyung05", "pukyung05","pukyung00!!1"); stmt = conn.prepareStatement("SELECT * FROM bang_06_T WHERE no > ?"); stmt.setInt( 1, 10 ); rs = stmt.executeQuery(); while( rs.next() ) { R vo = rowMapper.mapRow(rs); rl.add( vo ); } } catch( Exception e ) { throw e; } finally { if( rs != null ) rs.close(); if( stmt != null ) stmt.close(); if( conn != null ) conn.close(); } return rl; } | cs |
1 2 3 4 5 | public interface RowMapper<X> { public X mapRow( ResultSet rs ) throws SQLException; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class RowMapper_Bang06 implements RowMapper<Bang06VO>{ @Override public Bang06VO mapRow(ResultSet rs) throws SQLException { Bang06VO vo = new Bang06VO(); vo.setNo( rs.getInt("no") ); vo.setContent( rs.getString("content") ); vo.setAuthor( rs.getString("author") ); vo.setFsn( rs.getString("fsn") ); vo.setOfn( rs.getString("ofn") ); return vo; } } | cs |
1 2 3 4 5 6 7 8 | public class Test545 { public static void main( String[] args ) throws Exception { JdbcTemplate jtpl = new JdbcTemplate(); List<Bang06VO> rl = jtpl.query("SELECT * FROM bang_06_T" ,new RowMapper_Bang06() ); for( Bang06VO t : rl ) { System.out.println( t.getNo() + "," + t.getContent() ); } | cs |