휴먼교육센터 개발자과정 77일차

조하영·2022년 11월 24일
0

MVC2
지금까지 배운 동기방식의 통신
클라이언트 요청 -서버처리

model, view, controller

비동기방식: 요청 후 기다리지 않고 다른 작업 수행(ajax)
응답이 안와도 상관없음.
장점: 자원의 낭비가 덜하다(동시에 여러 업무 가능)
단점: 설계가 동기보다 복잡

게시글 쓰기에서 동기와 비동기를 구분

동기는 글을 모두 작성 후 서버에게 전송하고
응답을 받아서 화면이 바뀐다.
만일 서버 응답이 없으면 화면은 안바뀐다.

비동기는 글 등록 여부와 상관없이 화면이 새로고침 되지 않고
html 요소만 지정하여 변경가능.

컨트롤러
개발을 프레임워크 기반에서 한다. 운영은 프레임워크가 하고 개발자는 기능을 정의해준다.

스프링 프레임워크

A. IOC(Inversion Of Control)
개발의 주체가 개발자가 아니라 프레임 워크

B. DI(Dependency Injection-의존성 주입)
클래스에서 의존하는 객체가 있을 경우
객체를 직접 생성하지 않고 외부에서 객체의 주소를 주입받는 방식
클래스의 의존관계를 별도의 설정파일에서 설정하고 관리함.

class AAA{
BBB b;
}

class BBB{

}

AAA클래스에서 b객체를 참조해야한다.
AAA객체는 BBB객체에 의존하는데 실제 객체는 생성되기 전이다.
b변수가 BBB 객체의 주소를 저장하는 방법은

**필요하면 내가 만든다.
1. new 연산자를 통해 객체 생성
BBB b = new BBB();

**필요하면 달라고 한다.(Spring -DI)
2. 다른 클래스에서 BBB 객체를 생성 후 생성자로 주소를 전달.
3. 다른 클래스에서 BBB 객체를 생성 후 setter로 주소를 전달.

C. 컨테이너
스프링 설정에 의해 객체가 만들어지면 이 객체를 컨테이너에 보관하고
소스에서 필요하면 주입받아서 사용한다.

스프링 정리

  1. 메이븐 디렉토리 구조 이해
  2. 설정파일 이해 web.xml > root-context.xml > servlet-context.xml
  3. MVC 구조
  4. 클라이언트에서 파라미터 보내고 컨트롤러에서 받는 법
    (변수 하나받기, 객체로 받기, json으로 받기 등)

과제

  1. "/"를 입력하면 메인화면이 출력된다.
    메인화면은

    스프링 공부


    메뉴는 [홈으로][글쓰기] [글보기]

2.글쓰기를 누르면 이름과 제목과 내용을 입력한다.

  1. 등록버튼을 누르면 컨트롤러에서 파라미터로 받고 wrChk.jsp에서 출력한다.

  2. 한글이 깨짐 해결

Spring01_test 프로젝트 생성

1.컨트롤러

1.1보드컨트롤러

package com.human.test;

import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.human.testvo.TboardVO;

@Controller
public class BoardController {

	@RequestMapping(value = "/addForm", method = RequestMethod.GET)
	public String addForm(Locale locale, Model model) {
		return "addFormView";
	}
	
	@RequestMapping(value = "/addAction", method = RequestMethod.GET)
	public String addAction(Locale locale, Model model, TboardVO tvo) {
		
		model.addAttribute("tvo", tvo);
		return "wrChk";
	}
}

1.2홈 컨트롤러

package com.human.test;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
	}
	
}

2.VO

package com.human.testvo;

public class TboardVO {
	String name;
	String title;
	String context;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContext() {
		return context;
	}
	public void setContext(String context) {
		this.context = context;
	}

}

3.뷰

3.1홈뷰

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
<style type="text/css">
ul li{
list-style: none;
display: inline;
}
</style>
</head>
<body>
	<header>
		<h1>스프링공부</h1>
	</header>
	<nav>
	<ul>
	<li>[홈으로]</li>
	<li><a href="addForm">[글쓰기]</a></li>
	<li>[글보기]</li>
	</ul>
	</nav>
	<section>
	</section>
</body>
</html>

3.2글쓰기뷰

<%@ 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>
<%@ include file="home.jsp"%>
<hr>
	<section>
		<form action="addAction" method="get">
			<table>
				<tr>
					<th>이름</th>
					<td><input type="text" name="name"></td>
				</tr>
				<tr>
					<th>제목</th>
					<td><input type="text" name="title"></td>
				</tr>
				<tr>
					<th>내용</th>
					<td><input type="textArea" name="context"></td>
				</tr>
				<tr>
				<td colspan = "2" style="text-align: center;"><input type="submit" value="저장"></td>
				</tr>
			</table>
		</form>
	</section>
</body>
</html>

3.3출력뷰

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%@ page import="com.human.testvo.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@ include file="home.jsp"%>
	<section>
		<form action="addAction" method="get">
			<table>
				<tr>
					<th>이름</th>
					<td>${tvo.name}</td>
				</tr>
				<tr>
					<th>제목</th>
					<td>${tvo.title}</td>
				</tr>
				<tr>
					<th>내용</th>
					<td>${tvo.context}</td>
				</tr>
			</table>
		</form>
	</section>
</body>
</html>
profile
공부하는 개발자

0개의 댓글