70일 차 - 스프링 MVC 활용, lombok (23.04.07)

yvonne·2023년 4월 7일
0

📂Spring

목록 보기
2/18
post-thumbnail

1. MVC 활용

✔ 폴더 만들기

✔ application.properties

✔ TestController.java

package edu.global.ex.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class TestController {

	@GetMapping("/test") // http://localhost:8282/test
	public String test() {
		return "test"; //  /WEB-INF/views/test.jsp 를 의미
	}

}

✔ test.jsp

<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>이것은 테스트 뷰 페이지입니다.
</body>
</html>
  • 결과



✔ TestController.java

package edu.global.ex.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class TestController {

	@GetMapping("/test2") // http://localhost:8282/test
	public String test2() {
		return "test/test2"; //  /WEB-INF/views/test/test2.jsp 를 의미
	}

}

✔ 폴더

✔ test2.jsp

<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>이것은 테스트2 뷰 페이지입니다.
</body>
</html>
  • 결과




✔ BoardController.java

package edu.global.ex.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
//1. URL과 페이지 매핑 방법
//2. 뷰(jsp)에 데이터 전달
@RequestMapping("/board") 
@Controller
public class BoardController {
	@GetMapping("/content") // 📌 board/content로 치고 들어가기
	public String content() {
		return "board/content";
	}
}

✔ 폴더

✔ content.jsp

<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>이것은 게시판 컨텐트 페이지입니다.
</body>
</html>
  • 결과




🔎 점수 입력 후 총점, 합계 구하기

✔ GradeController.java

package edu.global.ex.controller;

import javax.servlet.http.HttpServletRequest;

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

@RequestMapping("/grade")
@Controller
public class GradeController {
	@GetMapping("/") // post 방식 사용하고 싶을 때는 PostMapping으로 사용
	public String kem_form() {
		return "kem_form";
	}

	// http://localhost:8282/grade/total?kor=90&eng=80&math=70
	@GetMapping("/total")
	public String kem_form(HttpServletRequest request, Model model) { // jsp에 값 보내기 위해 Model 객체 사용
		int kor = Integer.valueOf(request.getParameter("kor"));
		int eng = Integer.valueOf(request.getParameter("eng"));
		int math = Integer.valueOf(request.getParameter("math"));
		int total = kor + eng + math;
		double avg = total / 3.0;
		model.addAttribute("kor", kor); // jsp에 값 보내기
		model.addAttribute("eng", eng);
		model.addAttribute("math", math);
		model.addAttribute("total", total);
		model.addAttribute("avg", avg);
		return "grade";
	}

}

✔ kem_form.jsp

<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="/grade/total"> <!-- defalut 방식은 GET방식 -->
	국어 : <input type="text" name="kor"></br>
	영어 : <input type="text" name="eng"></br>
	수학 : <input type="text" name="math"></br>
	<button type="submit">입력</button>
	<button type="reset">취소</button>
	</form>
</body>
</html>

✔ grade.jsp

<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	국어 : ${kor}<br>
	영어 : ${eng}<br>
	수학 : ${math}<br>	
	합계 : ${total} <br>
	평균 : ${avg}
</body>
</html>
  • 결과




🔎 lombok 활용하여 성적 구하기

✔ GradeVO.java

package edu.global.ex.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Data // 아래 4개를 전부 포함
//@Getter
//@Setter
//@NoArgsConstructor
//@ToString

@AllArgsConstructor

public class GradeVO {

	private int kor;
	private int eng;
	private int math;

	public int getTotal() {
		return kor + eng + math;
	}

	public double getAvg() {
		return (kor + eng + math) / 3.0;
	}
	
	public char getGrade() {
		char grd = '수';
		double avg = getAvg();
		if (avg >= 90) {
			grd = '수';
		} else if(avg >= 80) {
			grd = '우';
		}else if(avg >= 70) {
			grd = '미';
		}else if(avg >= 60) {
			grd = '양';
		}else {
			grd = '가';
		}
		return grd;
	}

}

✔ GradeController.java

package edu.global.ex.controller;

import javax.servlet.http.HttpServletRequest;

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

import edu.global.ex.vo.GradeVO;

@RequestMapping("/grade")
@Controller
public class GradeController {
	@GetMapping("/")
	public String kem_form() {
		return "kem_form";
	}

	// http://localhost:8282/grade/total?kor=90&eng=80&math=70
	@GetMapping("/total")
	public String kem_form(HttpServletRequest request, Model model) {
		int kor = Integer.valueOf(request.getParameter("kor"));
		int eng = Integer.valueOf(request.getParameter("eng"));
		int math = Integer.valueOf(request.getParameter("math"));

		GradeVO grade = new GradeVO(kor,eng,math); // VO 객체 생성
		
		model.addAttribute("kor",kor);
		model.addAttribute("eng",eng);
		model.addAttribute("math",math);
		model.addAttribute("grade", grade);
		return "grade";
	}

}  

✔ grade.jsp

<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	국어 : ${kor}<br>
	영어 : ${eng}<br>
	수학 : ${math}<br>	
	합계 : ${grade.total} <br>
	평균 : ${grade.avg}<br>
	등급 : ${grade.grade}<br>
	<hr>
	합계 : ${grade.getTotal()} <br>
	평균 : ${grade.getAvg()}<br>
	등급 : ${grade.getGrade()}<br>
	toString : ${grade.toString()}<br>
</body>
</html>
  • 결과




🔎 커맨드 객체 사용

✔ GradeController.java

package edu.global.ex.controller;

import javax.servlet.http.HttpServletRequest;

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

import edu.global.ex.vo.GradeVO;

@RequestMapping("/grade")
@Controller
public class GradeController { 
	@PostMapping("/total2")
	public String kem_form2(GradeVO gradeVO, Model model) { // command 객체 생성 
		model.addAttribute("grade", gradeVO);
		return "grade";
	}
	
}

✔ grade.jsp

<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	국어 : ${grade.kor}<br>
	영어 : ${grade.eng}<br>
	수학 : ${grade.math}<br>	
	합계 : ${grade.total} <br>
	평균 : ${grade.avg}<br>
	등급 : ${grade.grade}<br>
	<hr>
	합계 : ${grade.getTotal()} <br>
	평균 : ${grade.getAvg()}<br>
	등급 : ${grade.getGrade()}<br>
	toString : ${grade.toString()}<br>
</body>
</html>
  • 결과
profile
개발 연습장

0개의 댓글