Spring MVC๊ฐ ์ผ๋ง๋ ํธํ ๊ธฐ๋ฅ์ ์ ๊ณตํด์ฃผ๊ณ ๋ ๊ทธ ๊ธฐ๋ฅ์ ๋ด๋ถ๋ ์ด๋ค ์์ผ๋ก ์ด๋ฃจ์ด์ก๋์ง ์์๋ณด๊ธฐ ์ํด ์์ฃผ ๋ณ๋ก์ธ ์์ ๋ฐฉ์์ Servlet๊ณผ JSP๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค์ด๋ณด์.
Spring web
๊ณผ Lombok
์ dependency๋ก ์ถ๊ฐํด์ค๋๋ค.@ServletComponentScan
์ด๋
ธํ
์ด์
์ ์ถ๊ฐํด์ค๋๋ค.@SpringBootApplication
@ServletComponentScan
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
username
๊ณผ age
๋ฅผ ์
๋ ฅ๋ฐ์ ๊ฒ์ด๊ธฐ์ ์๋์ ๊ฐ์ด ์์ฑํด์ค๋๋ค.@Getter @Setter
public class Member {
private Long id;
private String username;
private int age;
public Member() {
}
public Member(String username, int age) {
this.username = username;
this.age = age;
}
}
public class MemberRepository {
private static Map<Long, Member> store = new HashMap<>();
private static Long sequence = 0L;
private static final MemberRepository instance = new MemberRepository();
private MemberRepository() {
}
public static MemberRepository getInstance() {
return instance;
}
public Member save(Member member){
member.setId(++sequence);
store.put(member.getId(), member);
return member;
}
public Member findById(Long id) {
return store.get(id);
}
public List<Member> findAll() {
return new ArrayList<>(store.values());
}
public void clearAll() {
store.clear();
}
}
๐๐ป ์ฐธ๊ณ
static
ํด๋์ค๋ฅผ ์ฌ์ฉํ๊ณ ๊ธฐ๋ณธ ์์ฑ์๋ฅผprivate
๋ก ํ์ฌ ์ธ๋ถ์ ํธ์ถ์ ๋ง๋ ์ด์
- Repository ํด๋์ค๋ฅผ
์ฑ๊ธํค
์ผ๋ก ๊ด๋ฆฌํ๊ธฐ ์ํจ ์ ๋๋ค.์ฑ๊ธํค
ํด๋์ค์ ์ธ์คํด์ค๊ฐ ๋จ ํ ๊ฐ๋ง ์์ฑ๋์ด ์์ฑ๋ ์ธ์คํด์ค๋ฅผ ์ฌ๋ฌ ๊ณณ์์ ๊ณต์ ํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก, ๋จ ํ ๊ฐ์ ๊ฐ์ฒด๋ง ์์ฑ๋๋ ๊ฒ์ ๋ณด์ฅํ๋ ๋์์ธ ํจํด์ ๋๋ค.- ํด๋์ค ๋ด๋ถ์์ static์ผ๋ก ํด๋์ค๋ฅผ ์ปดํ์ผ ํ์์ ๋ฏธ๋ฆฌ ์์ฑํด๋๊ณ ์ฌ์ฉํ๋ ์ชฝ์์๋
getInstance
๋ฉ์๋๋ก ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๋ฐ์ ์ฌ์ฉํฉ๋๋ค.- ๊ธฐ๋ณธ ์์ฑ์๊น์ง ๋ง์๋์์ผ๋ฏ๋ก ์ธ๋ถ์์
new
๋ก ํด๋์ค ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์๊ฒ ๋ฉ๋๋ค.
HttpServlet
์ ์์๋ฐ๊ณ service
๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํฉ๋๋ค.@WebServlet
url๋งคํ ์ ๋ณด๋ฅผ ์ค์ ํฉ๋๋ค.PrintWriter
๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ html์ ์์ฑํ๋ ๊ฒ์ด ๋ณด์ด์ญ๋๊น?@WebServlet(name = "memberFormServlet", urlPatterns = "/servlet/members/new-form")
public class MemberFormServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
res.setCharacterEncoding("utf-8");
PrintWriter w = res.getWriter();
w.write("<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
"</head>\n" +
"<body>\n" +
"<form action=\"/servlet/members/save\" method=\"post\">\n" +
" username: <input type=\"text\" name=\"username\" />\n" +
" age: <input type=\"text\" name=\"age\" />\n" +
" <button type=\"submit\">์ ์ก</button>\n" + "</form>\n" +
"</body>\n" +
"</html>\n");
}
}
MemberRepository
๊ฐ์ฒด๋ฅผ ๋ฐ์์ต๋๋ค.HttpServlet
์ ์์๋ฐ๊ณ service
๋ฉ์๋๋ฅผ ๊ตฌํํ๊ณ @WebServlet
์ ๋งคํ์ ๋ณด๋ฅผ ์ค์ ํฉ๋๋ค.Request
๊ฐ์ฒด์ getParameter
๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ์ ์ก๋ ๋ฐ์ดํฐ๋ฅผ ํ์ฑํฉ๋๋ค.Member
๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ Repository์ save
๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ์ ์ฅํฉ๋๋ค.@WebServlet(name = "memberSaveServlet", urlPatterns = "/servlet/members/save")
public class MemberSaveServlet extends HttpServlet {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String username = req.getParameter("username");
int age = Integer.parseInt(req.getParameter("age"));
Member member = new Member(username, age);
memberRepository.save(member);
res.setContentType("text/html");
res.setCharacterEncoding("utf-8");
PrintWriter w = res.getWriter();
w.write("<html>\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" + "</head>\n" +
"<body>\n" +
"์ฑ๊ณต\n" +
"<ul>\n" +
" <li>id="+member.getId()+"</li>\n" +
" <li>username="+member.getUsername()+"</li>\n" +
" <li>age="+member.getAge()+"</li>\n" + "</ul>\n" +
"<a href=\"/index.html\">๋ฉ์ธ</a>\n" + "</body>\n" +
"</html>");
}
}
MemberRepository
์ findAll
์ ์ฅ์์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ต๋๋ค.foreach
๋ฌธ์ ์ด์ฉํ์ฌ html์ ๊ตฌ์ฑํด์ค๋๋ค.@WebServlet(name = "memberListServlet", urlPatterns = "/servlet/members")
public class MemberListServlet extends HttpServlet {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
List<Member> members = memberRepository.findAll();
res.setContentType("text/html");
res.setCharacterEncoding("utf-8");
PrintWriter w = res.getWriter();
w.write("<html>");
w.write("<body>");
for (Member member : members) {
w.write("<ul>");
w.write("<li>");
w.write("<span> username: "+member.getUsername()+" age: "+ member.getAge()+"</span>");
w.write("</li>");
w.write("</ul>");
}
w.write("</body>");
w.write("</html>");
}
}
Servlet
์ JAVA์ฝ๋๋ก HTML์ ์์ฑํด์ผ ํ๊ธฐ ๋๋ฌธ์ ๋งค์ฐ ๋ถํธํฉ๋๋ค.- ๋ํ ์ด๋ ๊ฒ JAVA์ฝ๋์ HTML์ฝ๋๊ฐ ํผ์ฌ๋์ด ์๋ ๊ฒ์ ๊ฐ๋ฐ ์์ฐ์ฑ, ์ ์ง๋ณด์์ฑ ๋ฑ ๋ชจ๋ ๋ฉด์์ ์ข์ง ์์ต๋๋ค.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/jsp/members/save.jsp" method="post">
username: <input type="text" name="username"/>
age: <input type="text" name="age"/>
<button type="submit">์ ์ก</button>
</form>
</body>
</html>
<% %>
๋ด์ Java์ฝ๋ ์์ฑ<%= %>
๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅrequest
, resoponse
๊ฐ์ฒด๋ ๋ณ๋ ์ ์ธ ์์ด ์์ฝ์ด์ฒ๋ผ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค. <%@ page import="basic.servlet.domain.member.MemberRepository" %>
<%@ page import="basic.servlet.domain.member.Member" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
MemberRepository memberRepository = MemberRepository.getInstance();
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));
Member member = new Member(username, age);
memberRepository.save(member);
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<ul>
<li>id=<%=member.getId()%></li>
<li>username=<%=member.getUsername()%></li>
<li>age=<%=member.getAge()%></li>
</ul>
<a href="/index.html">๋ฉ์ธ</a>
</body>
</html>
out
๋ํ request
, response
์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ ์ธ ์์ด ์์ฝ์ด์ฒ๋ผ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค.<%@ page import="basic.servlet.domain.member.MemberRepository" %>
<%@ page import="basic.servlet.domain.member.Member" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
MemberRepository memberRepository = MemberRepository.getInstance();
List<Member> members = memberRepository.findAll();
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/index.html">๋ฉ์ธ</a>
<table>
<thead>
<th>id</th>
<th>username</th>
<th>age</th>
</thead>
<tobdy>
<%
for (Member member : members) {
out.write("<tr>");
out.write(" <td>"+member.getId()+"</td>");
out.write(" <td>"+member.getUsername()+"</td>");
out.write(" <td>"+member.getAge()+"</td>");
out.write("</tr>");
}
%>
</tobdy>
</table>
</body>
</html>
JSP๊ฐ ์์ servlet๋ณด๋ค๋ HTML์ ๊ตฌ์ฑํ๋๋ฐ๋ ํจ์ฌ ํธํ ๊ฒ ๊ฐ๋ค์.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<!-- ์๋๊ฒฝ๋ก ์ฌ์ฉ => localhost:8080/test/page.jsp ์์ ์ด jspํ์ผ ํธ์ถ์
action url์ localhost:8080/test/save๊ฐ ๋จ-->
<form action="save" method="post">
username: <input type="text" name="username"/>
age: <input type="text" name="age"/>
<button type="submit">์ ์ก</button>
</form>
</body>
</html>
urlPattenrs
์ ์ง์ ํ ๊ฒฝ๋ก๋ก ์์ฒญ์ด ์ค๋ฉด Form์ด ์๋ .jspํ์ผ์ ์๋ตํ๊ธฐ ์ํ Servlet์ ์์ฑํฉ๋๋ค.
์ด ๊ฒฝ๋ก๋ก ์์ฒญ์ด ์์ ๋ jspํ์ผ์ ์๋ตํ๋ 2๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. (jspํ์ผ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ฆฌ์์ค์ ๋ํด์๋ ๋ง์ฐฌ๊ฐ์ง ์ ๋๋ค.)
test.jsp
๋ฅผ ์๋ตํ๋ ค๋ฉด ํด๋ผ์ด์ธํธ์๊ฒ test.jsp
๋ก ์ด๋ํ๋ผ๋ ์๋ต์ ๋ณด๋ด๊ณ ๋ค์ ํด๋ผ์ด์ธํธ์ ๋ธ๋ผ์ฐ์ ๊ฐ ์๋ฒ์ test.jsp
๋ฅผ ์์ฒญํด์ผ ํ๋ค.test.jsp
๋ฅผ ์๋ตํ๋ ค๋ฉด ์๋ฒ ๋ด๋ถ์ ์ผ๋ก test.jsp
์ ์์ฒญ์ ๋ณด๋ด๊ณ ํด๋ผ์ด์ธํธ์๊ฒ ์๋ตํด์ค๋ค.test.jsp
๋ฅผ ํธ์ถํ ๊ฒ์ด๋ค. ์ฌ์ค ์์ฒญ์ ์ถ๊ฐ๋ก ์๋ฒ ๋ด๋ถ์ ์ผ๋ก ๋ฐ์ํ์์ง๋ง ๋ธ๋ผ์ฐ์ ๋ ์ด ์ฌ์ค์ ์์ง ๋ชปํ๋ค.@WebServlet(name = "mvcMemberFormServlet", urlPatterns = "/servlet-mvc/members/new-form")
public class MvcMemberFormServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String viewPath = "/WEB-INF/views/new-form.jsp";
RequestDispatcher dispatcher = req.getRequestDispatcher(viewPath);
dispatcher.forward(req, res);
}
}
MemberRepository
๋ฅผ ํตํด ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ์ ์ฅ๋ ๊ฒฐ๊ณผ๋ฅผ ๋๋๋ง์ ์ํด save-result.jsp
์ ์ ๋ฌํด์ฃผ์ด์ผ ํฉ๋๋ค.
request.setAttribute("key", value);
save-result.jsp
)@WebServlet(name = "mvcMemberSaveServlet", urlPatterns = "/servlet-mvc/members/save")
public class MvcMemberSaveServlet extends HttpServlet {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String username = req.getParameter("username");
int age = Integer.parseInt(req.getParameter("age"));
Member member = new Member(username, age);
memberRepository.save(member);
String viewPath = "/WEB-INF/views/save-result.jsp";
RequestDispatcher dispatcher = req.getRequestDispatcher(viewPath);
req.setAttribute("member",member);
dispatcher.forward(req, res);
}
}
<%= %>
ํน์ ${ }
๋ฌธ๋ฒ์ ์ด์ฉํ์ฌ Model๋ก ์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ฟ๋ ค์ค๋๋ค.
setAttibute()
๋ก Model์ ๊ฐ์ ์ธํ
ํ๊ณ getAttribute()
๋ก ๋ฐ์์ต๋๋ค.<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<!-- ๋ฐฉ๋ฒ 1
getAttribute()๋ก ๋ฐ์ ๊ฐ์ด object ํ์
์ด๊ธฐ ๋๋ฌธ์ ์บ์คํ
์ด ํ์ํจ .. ์กฐ๊ธ ๋ฒ๊ฑฐ๋ก์-->
<%--<ul>--%>
<%-- <li>id=<%=((Member)request.getAttribute("member")).getId()%></li>--%>
<%-- <li>username=<%=((Member)request.getAttribute("member")).getUsername()%></li>--%>
<%-- <li>age=<%=((Member)request.getAttribute("member")).getAge()%></li>--%>
<%--</ul>--%>
<!-- ๋ฐฉ๋ฒ2 -->
<ul>
<li>id=${member.id}</li>
<li>username=${member.username}</li>
<li>age=${member.age}</li>
</ul>
<a href="/index.html">๋ฉ์ธ</a>
</body>
</html>
๋ง์ฐฌ๊ฐ์ง๋ก MemberRepository
์ findAll()
๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ค๊ณ ์ฝ์ด์จ ๋ฐ์ดํฐ๋ฅผ Model์ ๋ด์ View๋จ์ผ๋ก ์ ๋ฌํฉ๋๋ค.
Object
ํ์
์ผ๋ก ์ ๋ฌ๋๊ธฐ ๋๋ฌธ์ List
๋ ๊ด๊ณ์์ด Model
์ ๋ด์ ์ ๋ฌํ ์ ์์ต๋๋ค.@WebServlet(name = "mvcMemberListServlet", urlPatterns = "/servlet-mvc/members")
public class MvcMemberListServlet extends HttpServlet {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
List<Member> members = memberRepository.findAll();
req.setAttribute("members",members);
String viewPath = "/WEB-INF/views/members.jsp";
RequestDispatcher dispatcher = req.getRequestDispatcher(viewPath);
dispatcher.forward(req, res);
}
}
JSTL core tag
๋ฅผ ์ด์ฉํด ๋ฐ๋ณต๋ฌธ์ ๋๋ ค Model์ ๋ด๊ธด ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/index.html">๋ฉ์ธ</a>
<table>
<thead>
<th>id</th>
<th>username</th>
<th>age</th>
</thead>
<tobdy>
<c:forEach var="member" items="${members}">
<tr>
<td>${member.id}</td>
<td>${member.username}</td>
<td>${member.age}</td>
</tr>
</c:forEach>
</tobdy>
</table>
</body>
</html>
๐๐ป ์ฐธ๊ณ
- /WEB-INF ์ดํ์ ์๋ ํ์ผ์ ์ธ๋ถ์์ ์ง์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํฉ๋๋ค.
- ์ฆ, ์ธ๋ถ์์ ๋ฐ๋ก ์ ๊ทผ์ํค๊ณ ์ถ์ง ์์ ํ์ผ์ ์์น์์ผ์ผ ํฉ๋๋ค.
- ์๋ฅผ๋ค์ด
members.jsp
(์ ์ฒด ํ์์ ์กฐํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํ jspํ์ผ)์ ์ง์ ํ์ผ์ ๊ฒฝ๋ก๋ก ์ ๊ทผ์ ํ์ฉํ์ง ์๊ณ servlet์ ํตํด ์ ๊ทผํ๊ฒ ํ๋๋ก ํ ์ ์์ต๋๋ค.
์ธํ๋ฐ ๊น์ํ๋์ ์คํ๋ง MVC 1ํธ ์ ์๊ฐํ๊ณ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค.