11. (실습) 학생의 국영수 평균, 총점 구하기

홍준성·2022년 7월 22일
0
  1. com.example.ex02.domain.vo에 StudentVO.java 추가
package com.example.ex02.domain.vo;

import org.springframework.stereotype.Component;

import lombok.Data;

@Component
@Data
public class StudentVO {
	private int num;
	private int kor;
	private int eng;
	private int math;
}

  1. 컨트롤러 작성
package com.example.ex02.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.ex02.domain.vo.InfoDTO;
import com.example.ex02.domain.vo.StudentVO;

import lombok.extern.log4j.Log4j;

@Controller
@RequestMapping("/ex/*") 
@Log4j
public class SampleController {
	
	@RequestMapping(value = "/basic", method = {RequestMethod.GET, RequestMethod.POST})
	public void basic(HttpServletRequest req) {
		
		log.info("basic......"+req.getMethod());
	}
	
	@RequestMapping
	public void basic2() {
		log.info("basic2...............");
	}
	
	@GetMapping("/basicOnlyGet")
	public void basic3() {
		log.info("basic3..........only get");
	}
	
	@GetMapping("/ex07")
//	외부에서 학생의 번호, 국어, 영어, 수학 점수를 모델 객체로 전달받는다.
//	파라미터명과 매개변수에 선언된 모델객체의 필드명이 동일하면 자동으로 매핑된다.
//	리턴 시 알맞는 페이지로 이동이 되고, 모델 객체는 직접 KEY를 지정하지 않아도
//	화면 쪽에서는 앞글자만 소문자로 바뀐 값으로 해당 필드를 접근할 수 있다.
	public String ex07(StudentVO studentVO) {
		log.info("ex07.....................................");
		log.info(studentVO.toString());	
		return "ex/ex07";
	}
}

  1. jsp 작성
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EX07</title>
</head>
<body>
<c:set var="total" value="${studentVO.kor + studentVO.eng + studentVO.math}"/>
<c:set var="avg" value="${total / 3}"/>
	<table border = "1">
		<tr>
			<th>번호</th>
			<th>국어</th>
			<th>영어</th>
			<th>수학</th>
			<th>총점</th>
			<th>평균</th>
		</tr>
		
		
		<tr>
			<td><c:out value="${studentVO.num}"/></td>
			<td><c:out value="${studentVO.kor}"/></td>
			<td><c:out value="${studentVO.eng}"/></td>
			<td><c:out value="${studentVO.math}"/></td>
			<td><c:out value="${total}점"/></td>
			<td><c:out value="${avg}점"/></td>

		</tr>
	</table>
</body>
</html>
profile
준성이의 개발자 공부 velog

0개의 댓글