MVC

MVC ํŒจํ„ด์ด๋ž€?

Model, View, Controller๋ฅผ ๋œปํ•˜๋Š” ์šฉ์–ด๋กœ ๊ฐœ๋ฐœ ํ˜•ํƒœ์˜ ์ผ์ข…์ด๋‹ค.

  • Model์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ๊ด€๊ณ„๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค. ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ํ•„์š”ํ•œ ์ž๋ฃŒ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ๋ถ€ํ„ฐ ์ถ”์ถœํ•˜๊ฑฐ๋‚˜, ์ˆ˜์ •ํ•˜์—ฌ Contoller๋กœ ์ „๋‹ฌํ•œ๋‹ค.

  • View๋Š” ์‚ฌ์šฉ์žํ•œํ…Œ ๋ณด์—ฌ์ง€๋Š” UIํ™”๋ฉด์ด๋‹ค. ์ฃผ๋กœ .jsp ํŒŒ์ผ๋กœ ์ž‘์„ฑํ•˜๋ฉฐ, Controller์—์„œ ์–ด๋–ค View ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ณด์—ฌ์ค„์ง€ ๊ฒฐ์ •ํ•œ๋‹ค.

  • Controller๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›๊ณ , ์ ์ ˆํ•œ Model์— ์ง€์‹œ๋ฅผ ๋‚ด๋ฆฌ๋ฉฐ, Model์—์„œ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ ์ ˆํ•œ View์— ์ „๋‹ฌํ•œ๋‹ค.

์ž‘์—…์„ ๋ถ„ํ• ํ•˜์—ฌ, ์ถ”ํ›„ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ๋” ์ข‹๊ฒŒ ๋งŒ๋“ ๋‹ค.

Model 1

MVC์—์„œ View์™€ Controller ๊ฐ™์ด ์žˆ๋Š” ํ˜•ํƒœ์ด๋‹ค. ํ•œ ํŒŒ์ผ์— ๊ฐ™์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด๋œ๋‹ค.

ํด๋ผ์ด์–ธํŠธ -> JSP(View + Controller) -> DAO(Model) -> DB

์ด์ „์— ๋งŽ์ด ์“ฐ์ด๋˜ ๋ฐฉ๋ฒ•์ด๋‹ค.

Model 2

์ „ํ†ต์ ์ธ MVC๋ฅผ ๋”ฐ๋ฅด๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. Model, View, Controller๊ฐ€ ๋ชจ๋‘ ๋ชจ๋“ˆํ™” ๋˜์–ด ์žˆ๋Š” ํ˜•ํƒœ์ด๋‹ค.

ํด๋ผ์ด์–ธํŠธ -> Servlet(Controller)<->Command&DAO(Model)<->DB
                             ใ„ด> JSP

์‹ค์Šต

1. ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ

2. FrontController ๋งŒ๋“ค๊ธฐ

ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›๋Š” FrontController๋ฅผ ๋งŒ๋“ค์ž. ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ์ง‘์ค‘์‹œํ‚จ๋‹ค.

package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import command.BCommand;
import command.BContentCommand;
import command.BDeleteCommand;
import command.BListCommand;
import command.BModifyCommand;
import command.BReplyCommand;
import command.BReplyViewCommand;
import command.BWriteCommand;

/**
 * Servlet implementation class BFrontController
 */
@WebServlet("*.do")
public class BFrontController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public BFrontController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("FrontController doGet");
        actionDo(request,response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("FrontController doPost");
        actionDo(request,response);
    }

    private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        System.out.println("Frontcontroller actionDo");

        request.setCharacterEncoding("UTF-8");

        String viewPage = null;
        BCommand command = null;

        String uri = request.getRequestURI();
        String conPath = request.getContextPath();
        String com = uri.substring(conPath.length());

        if(com.equals("/write_view.do")) {
            viewPage = "write_view.jsp";
        }else if(com.equals("/write.do")) {
            command = new BWriteCommand();
            command.execute(request,response);
            viewPage = "list.do";
        }else if(com.equals("/list.do")) {
            command = new BListCommand();
            command.execute(request,response);
            viewPage = "list.jsp";
        }else if(com.equals("/content_view.do")) {
            command = new BContentCommand();
            command.execute(request,response);
            viewPage = "content_view.jsp";
        }else if(com.equals("/modify.do")) {
            command = new BModifyCommand();
            command.execute(request,response);
            viewPage = "list.do";
        }else if(com.equals("/delete.do")) {
            command = new BDeleteCommand();
            command.execute(request,response);
            viewPage = "list.do";
        }else if(com.equals("/reply_view.do")) {
            command = new BReplyViewCommand();
            command.execute(request,response);
            viewPage = "reply_view.jsp";
        }else if(com.equals("/reply.do")) {
            command = new BReplyCommand();
            command.execute(request,response);
            viewPage = "list.do";
        }

        // forwarding
        RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
        dispatcher.forward(request, response);
    }
}

3. Command

Command ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ฐ๊ฐ์˜ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

BCommand ์ธํ„ฐํŽ˜์ด์Šค

package command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface BCommand {
    void execute(HttpServletRequest request, HttpServletResponse response);
}

BWriteCommand

package command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BDao;

public class BWriteCommand implements BCommand{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub

        String bName = request.getParameter("bName");
        String bTitle = request.getParameter("bTitle");
        String bContent = request.getParameter("bContent");

        BDao dao = new BDao();
        dao.write(bName, bTitle, bContent);

    }    
}

BListCommand

package command;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BDao;
import dto.BDto;

public class BListCommand implements BCommand{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
        BDao dao = new BDao();
        ArrayList<BDto> dtos = dao.list();
        request.setAttribute("list", dtos);
    }
}

BContentCommand

package command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BDao;
import dto.BDto;

public class BContentCommand implements BCommand{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
        String bId = request.getParameter("bId");
        BDao dao = new BDao();
        BDto dto = dao.contentView(bId);

        request.setAttribute("content_view", dto);
    }
}

BModifyCommand

package command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BDao;

public class BModifyCommand implements BCommand{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
        String bId = request.getParameter("bId");
        String bName = request.getParameter("bName");
        String bTitle = request.getParameter("bTitle");
        String bContent = request.getParameter("bContent");


        BDao dao = new BDao();
        dao.modify(bId,bName,bTitle,bContent);
    }

}

BDeleteCommand

package command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BDao;

public class BDeleteCommand implements BCommand {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
        String bId = request.getParameter("bId");
        BDao dao = new BDao();
        dao.delete(bId);
    }

}

BReplyViewCommand

package command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BDao;
import dto.BDto;

public class BReplyViewCommand implements BCommand {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
        String bId = request.getParameter("bId");
        BDao dao = new BDao();
        BDto dto = dao.reply_view(bId);

        request.setAttribute("reply_view", dto);
    }
}

BReplyCommand

package command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BDao;

public class BReplyCommand implements BCommand {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
        String bId = request.getParameter("bId");
        String bName = request.getParameter("bName");
        String bTitle = request.getParameter("bTitle");
        String bContent = request.getParameter("bContent");
        String bGroup = request.getParameter("bGroup");
        String bStep = request.getParameter("bStep");
        String bIndent = request.getParameter("bIndent");

        BDao dao = new BDao();
        dao.reply(bId,bName, bTitle, bContent, bGroup, bStep, bIndent);
    }

}

4. DTO(Data Transfer Object) ๋งŒ๋“ค๊ธฐ

๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์˜ ๋ฐ์ดํ„ฐ DTO ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.

package dto;

import java.sql.Timestamp;

public class BDto {
    int bId;
    String bName;
    String bTitle;
    String bContent;
    Timestamp bDate;
    int bHit;
    int bGroup;
    int bStep;
    int bIndent;

    public BDto() {

    }
    public BDto(int bId, String bName,String bTitle,    String bContent,    Timestamp bDate,    int bHit,int bGroup,    int bStep,    int bIndent) {
        this.bId = bId;
        this.bName = bName;
        this.bContent = bContent;
        this.bTitle = bTitle;
        this.bDate = bDate;
        this.bHit = bHit;
        this.bGroup = bGroup;
        this.bStep = bStep;
        this.bIndent = bIndent;
    }
    public int getbId() {
        return bId;
    }
    public void setbId(int bId) {
        this.bId = bId;
    }
    public String getbName() {
        return bName;
    }
    public void setbName(String bName) {
        this.bName = bName;
    }
    public String getbTitle() {
        return bTitle;
    }
    public void setbTitle(String bTitle) {
        this.bTitle = bTitle;
    }
    public String getbContent() {
        return bContent;
    }
    public void setbContent(String bContent) {
        this.bContent = bContent;
    }
    public Timestamp getbDate() {
        return bDate;
    }
    public void setbDate(Timestamp bDate) {
        this.bDate = bDate;
    }
    public int getbHit() {
        return bHit;
    }
    public void setbHit(int bHit) {
        this.bHit = bHit;
    }
    public int getbGroup() {
        return bGroup;
    }
    public void setbGroup(int bGroup) {
        this.bGroup = bGroup;
    }
    public int getbStep() {
        return bStep;
    }
    public void setbStep(int bStep) {
        this.bStep = bStep;
    }
    public int getbIndent() {
        return bIndent;
    }
    public void setbIndent(int bIndent) {
        this.bIndent = bIndent;
    }

}

5. DAO(Data Access Object) ๋งŒ๋“ค๊ธฐ

DAO๋Š” DB๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์กฐ์ž‘ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ „๋‹ดํ•˜๋„๋ก ๋งŒ๋“  Object์ด๋‹ค.

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;

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


import dto.BDto;

public class BDao {

    DataSource dataSource;

    public BDao() {
        try {
            Context context = new InitialContext();
            dataSource = (DataSource)context.lookup("java:comp/env/jdbc/javaproject");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public void write(String bName, String bTitle, String bContent) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = dataSource.getConnection();
            String query = "insert into mvc_board ( bName, bTitle, bContent,bDate, bHit, bGroup, bStep, bIndent) select  ?, ?, ?, NOW(),0, MAX(bId)+1, 0, 0 from mvc_board";
            preparedStatement = (PreparedStatement) connection.prepareStatement(query);
            preparedStatement.setString(1, bName);
            preparedStatement.setString(2, bTitle);
            preparedStatement.setString(3, bContent);
            int rn = preparedStatement.executeUpdate();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                if(preparedStatement!=null)preparedStatement.close();
                if(connection!=null)connection.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
    }

    public ArrayList<BDto> list(){
        ArrayList<BDto> dtos = new ArrayList<BDto>();
        Connection connection=null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = dataSource.getConnection();
            String query = "select bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent from mvc_board order by bGroup desc, bStep asc";
            preparedStatement = (PreparedStatement) connection.prepareStatement(query);
            resultSet = preparedStatement.executeQuery();

            while(resultSet.next()) {
                int bId = resultSet.getInt("bId");
                String bName = resultSet.getString("bName");
                String bTitle = resultSet.getString("bTitle");
                String bContent = resultSet.getString("bContent");
                Timestamp bDate = resultSet.getTimestamp("bDate");
                int bHit = resultSet.getInt("bHit");
                int bGroup = resultSet.getInt("bGroup");
                int bStep = resultSet.getInt("bStep");
                int bIndent = resultSet.getInt("bIndent");

                BDto dto = new BDto(bId,bName, bTitle, bContent, bDate,bHit,bGroup, bStep, bIndent);
                dtos.add(dto);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                if(resultSet!=null)resultSet.close();
                if(preparedStatement!=null)preparedStatement.close();
                if(connection!=null)connection.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
        return dtos;
    }

    public BDto contentView(String strID) {
        upHit(strID);

        BDto dto = null;
        Connection connection=null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = dataSource.getConnection();
            String query = "select * from mvc_board where bId = ?";
            preparedStatement = connection.prepareStatement(query);
            preparedStatement.setInt(1, Integer.parseInt(strID));
            resultSet = preparedStatement.executeQuery();

            if(resultSet.next()) {
                int bId = resultSet.getInt("bId");
                String bName = resultSet.getString("bName");
                String bTitle = resultSet.getString("bTitle");
                String bContent = resultSet.getString("bContent");
                Timestamp bDate = resultSet.getTimestamp("bDate");
                int bHit = resultSet.getInt("bHit");
                int bGroup = resultSet.getInt("bGroup");
                int bStep = resultSet.getInt("bStep");
                int bIndent = resultSet.getInt("bIndent");

                dto = new BDto(bId,bName, bTitle, bContent, bDate,bHit,bGroup, bStep, bIndent);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                if(resultSet!=null)resultSet.close();
                if(preparedStatement!=null)preparedStatement.close();
                if(connection!=null)connection.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
        return dto;
    }

    public void modify(String bId,String bName, String bTitle, String bContent) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = dataSource.getConnection();
            String query = "update mvc_board set bName = ?, bTitle = ?, bContent = ? where bId = ?";
            preparedStatement = (PreparedStatement) connection.prepareStatement(query);
            preparedStatement.setString(1, bName);
            preparedStatement.setString(2, bTitle);
            preparedStatement.setString(3, bContent);
            preparedStatement.setInt(4, Integer.parseInt(bId));
            int rn = preparedStatement.executeUpdate();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                if(preparedStatement!=null)preparedStatement.close();
                if(connection!=null)connection.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
    }

    public void delete(String bId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = dataSource.getConnection();
            String query = "delete from mvc_board where bId = ?";
            preparedStatement = (PreparedStatement) connection.prepareStatement(query);
            preparedStatement.setInt(1, Integer.parseInt(bId));
            int rn = preparedStatement.executeUpdate();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                if(preparedStatement!=null)preparedStatement.close();
                if(connection!=null)connection.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
    }

    public BDto reply_view(String str) {
        BDto dto=null;
        Connection connection=null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = dataSource.getConnection();
            String query = "select * from mvc_board where bId = ?";
            preparedStatement = (PreparedStatement) connection.prepareStatement(query);
            preparedStatement.setInt(1, Integer.parseInt(str));
            resultSet = preparedStatement.executeQuery();

            if(resultSet.next()) {
                int bId = resultSet.getInt("bId");
                String bName = resultSet.getString("bName");
                String bTitle = resultSet.getString("bTitle");
                String bContent = resultSet.getString("bContent");
                Timestamp bDate = resultSet.getTimestamp("bDate");
                int bHit = resultSet.getInt("bHit");
                int bGroup = resultSet.getInt("bGroup");
                int bStep = resultSet.getInt("bStep");
                int bIndent = resultSet.getInt("bIndent");

                dto = new BDto(bId,bName, bTitle, bContent, bDate,bHit,bGroup, bStep, bIndent);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                if(resultSet!=null)resultSet.close();
                if(preparedStatement!=null)preparedStatement.close();
                if(connection!=null)connection.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
        return dto;
    }

    public void reply(String bId, String bName,String bTitle,String bContent,String bGroup,String bStep,String bIndent) {
        replyShape(bGroup,bStep);

        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = dataSource.getConnection();
            String query = "insert into mvc_board ( bName, bTitle, bContent,bDate, bGroup, bStep, bIndent) values ( ?, ?, ?,NOW(), ?, ?, ?)";
            preparedStatement = (PreparedStatement) connection.prepareStatement(query);
            preparedStatement.setString(1, bName);
            preparedStatement.setString(2, bTitle);
            preparedStatement.setString(3, bContent);
            preparedStatement.setInt(4, Integer.parseInt(bGroup));
            preparedStatement.setInt(5, Integer.parseInt(bStep)+1);
            preparedStatement.setInt(6, Integer.parseInt(bIndent)+1);
            int rn = preparedStatement.executeUpdate();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                if(preparedStatement!=null)preparedStatement.close();
                if(connection!=null)connection.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
    }

    private void replyShape(String strGroup, String strStep) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = dataSource.getConnection();
            String query = "update mvc_board set bStep = bStep + 1 where bGroup = ? and bStep > ?";
            preparedStatement = (PreparedStatement) connection.prepareStatement(query);
            preparedStatement.setInt(1, Integer.parseInt(strGroup));
            preparedStatement.setInt(2, Integer.parseInt(strStep));
            int rn = preparedStatement.executeUpdate();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                if(preparedStatement!=null)preparedStatement.close();
                if(connection!=null)connection.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }

    }
    private void upHit(String bId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = dataSource.getConnection();
            String query = "update mvc_board set bHit = bHit + 1 where bId = ?";
            preparedStatement = (PreparedStatement) connection.prepareStatement(query);
            preparedStatement.setString(1, bId);
            int rn = preparedStatement.executeUpdate();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                if(preparedStatement!=null)preparedStatement.close();
                if(connection!=null)connection.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
    }
}

6. View ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ

ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋Œ€ํ•ด์„œ FrontController์—์„œ ์ž‘์—…์„ ๋ถ„๊ธฐํ•˜๊ณ , ํ•ด๋‹น Commandํด๋ž˜์Šค๊ฐ€ ์ž‘๋™ํ•˜์—ฌ DAO๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ž‘์—…์„ ํ•œ๋‹ค.

DAOํด๋ž˜์Šค์˜ ๊ฒฐ๊ณผ๋ฌผ๋กœ DTO๊ฐ์ฒด๊ฐ€ View(.jspํŽ˜์ด์ง€)๋กœ ์ „๋‹ฌ๋˜๋ฉฐ, View์—์„œ๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์œผ๋กœ ํ™”๋ฉด(UI)๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ์ถœ๋ ฅ ํ•œ๋‹ค.

content view

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <table width="500" cellpadding="0" cellspacing="0" border="1">
        <form action="modify.do" method="post">
            <input type="hidden" name="bId" value="${content_view.bId}">
            <tr>
                <td> ๋ฒˆํ˜ธ </td>
                <td> ${content_view.bId} </td>
            </tr>
            <tr>
                <td> ํžˆํŠธ </td>
                <td> ${content_view.bHit} </td>
            </tr>
            <tr>
                <td> ์ด๋ฆ„ </td>
                <td> <input type="text" name="bName" value="${content_view.bName}"></td>
            </tr>
            <tr>
                <td> ์ œ๋ชฉ </td>
                <td> <input type="text" name="bTitle" value="${content_view.bTitle}"></td>
            </tr>
            <tr>
                <td> ๋‚ด์šฉ </td>
                <td> <textarea rows="10" name="bContent" >${content_view.bContent}</textarea></td>
            </tr>
            <tr >
                <td colspan="2"> <input type="submit" value="์ˆ˜์ •"> &nbsp;&nbsp; <a href="list.do">๋ชฉ๋ก๋ณด๊ธฐ</a> &nbsp;&nbsp; <a href="delete.do?bId=${content_view.bId}">์‚ญ์ œ</a> &nbsp;&nbsp; <a href="reply_view.do?bId=${content_view.bId}">๋‹ต๋ณ€</a></td>
            </tr>
        </form>
    </table>

</body>
</html>

list_view

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <table width="500" cellpadding="0" cellspacing="0" border="1">
        <tr>
            <td>๋ฒˆํ˜ธ</td>
            <td>์ด๋ฆ„</td>
            <td>์ œ๋ชฉ</td>
            <td>๋‚ ์งœ</td>
            <td>ํžˆํŠธ</td>
        </tr>
        <c:forEach items="${list}" var="dto">
        <tr>
            <td>${dto.bId}</td>
            <td>${dto.bName}</td>
            <td>
                <c:forEach begin="1" end="${dto.bIndent}">-</c:forEach>
                <a href="content_view.do?bId=${dto.bId}">${dto.bTitle}</a></td>
            <td>${dto.bDate}</td>
            <td>${dto.bHit}</td>
        </tr>
        </c:forEach>
        <tr>
            <td colspan="5"> <a href="write_view.do">๊ธ€์ž‘์„ฑ</a> </td>
        </tr>
    </table>
</body>
</html>

reply_view

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <table width="500" cellpadding="0" cellspacing="0" border="1">
        <form action="reply.do" method="post">
            <input type="hidden" name="bId" value="${reply_view.bId}">
            <input type="hidden" name="bGroup" value="${reply_view.bGroup}">
            <input type="hidden" name="bStep" value="${reply_view.bStep}">
            <input type="hidden" name="bIndent" value="${reply_view.bIndent}">
            <tr>
                <td> ๋ฒˆํ˜ธ </td>
                <td> ${reply_view.bId} </td>
            </tr>
            <tr>
                <td> ํžˆํŠธ </td>
                <td> ${reply_view.bHit} </td>
            </tr>
            <tr>
                <td> ์ด๋ฆ„ </td>
                <td> <input type="text" name="bName" value="${reply_view.bName}"></td>
            </tr>
            <tr>
                <td> ์ œ๋ชฉ </td>
                <td> <input type="text" name="bTitle" value="${reply_view.bTitle}"></td>
            </tr>
            <tr>
                <td> ๋‚ด์šฉ </td>
                <td> <textarea rows="10"  name="bContent">${reply_view.bContent}</textarea></td>
            </tr>
            <tr >
                <td colspan="2"><input type="submit" value="๋‹ต๋ณ€"> <a href="list.do" >๋ชฉ๋ก</a></td>
            </tr>
        </form>
    </table>
</body>
</html>

write_view

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <table width="500" cellpadding="0" cellspacing="0" border="1">
        <form action="write.do" method="post">
            <tr>
                <td> ์ด๋ฆ„ </td>
                <td> <input type="text" name="bName" size = "50"> </td>
            </tr>
            <tr>
                <td> ์ œ๋ชฉ </td>
                <td> <input type="text" name="bTitle" size = "50"> </td>
            </tr>
            <tr>
                <td> ๋‚ด์šฉ </td>
                <td> <textarea name="bContent" rows="10" ></textarea> </td>
            </tr>
            <tr >
                <td colspan="2"> <input type="submit" value="์ œ์ถœ"> &nbsp;&nbsp; <a href="list.do">๋ฆฌ์ŠคํŠธ๋ณด๊ธฐ</a></td>
            </tr>
        </form>
    </table>

</body>
</html>

Last updated

Was this helpful?