
[application.properties]
#server port number
server.port = 8282
#datasource (oracle)
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/xe
#spring.datasource.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
#spring.datasource.url=jdbc:log4jdbc:oracle:thin:@localhost:1521/xe
spring.datasource.username=scott
spring.datasource.password=tiger
#xml location
#mybatis.mapper-locations=classpath:mappers/**/*.xml
#### jsp
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp





[계산 TestController.java]
package edu.global.ex.controller;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller // RestController로 하면 return 값의 문자가 출력되지만, Controller로 하면 return 값 이름의 파일을 불러온다.
public class TestController {
@GetMapping("/test") // http://localhost:8282/test
public String test() {
return "test"; // test.jsp를 불러온다.
// return "/WEB-INF/views/test.jsp"; 상기 test.jsp를 불러오는 로직
/*
* application.properties 파일에 하기 문구를 추가
* spring.mvc.view.prefix=/WEB-INF/views/
* spring.mvc.view.suffix=.jsp
*/
}
@GetMapping("/test2") // http://localhost:8282/test2
public String test2() {
return "test/test2"; // test폴더 안의 test2.jsp를 불러온다.
}
@GetMapping("/test/test3")
public String test3() {
return "test/test3";
}
@RequestMapping("/test/test4")
public String test4() {
return "test/test4";
}
}
---------------------------------------------------------------------
[계산 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>list</title>
</head>
<body>이것은 테스트 뷰 페이지입니다.
</body>
</html>
---------------------------------------------------------------------
[계산 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>list</title>
</head>
<body>이것은 테스트2 뷰 페이지입니다.
</body>
</html>
[결과값]



[계산 BoardController.java]
package edu.global.ex.controller;
import org.springframework.stereotype.*;
import org.springframework.ui.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
//1.URL과 페이지 매핑 방법
//2.뷰(jsp)에 데이터 전달
@RequestMapping("/board") // http://localhost:8282/board
@Controller
public class BoardController {
@GetMapping("/") // http://localhost:8282/board/
public String main() {
return "board/main";
}
@GetMapping("/content") // http://localhost:8282/board/content
public String content() {
return "board/content";
}
@GetMapping("/content_view") // http://localhost:8282/board/content_view
public String content_view(Model model) {
// 하기 Command와 동일하게 DB에 데이터 보내기
// request.setAttribute("mLists", mDto);
model.addAttribute("id", 30); // key, value
model.addAttribute("pw", "1234");
return "board/content";
}
@GetMapping("/reply") // http://localhost:8282/board/reply
public ModelAndView reply() {
ModelAndView mv = new ModelAndView(); // 객체생성하여 key, value형태로 view(board/reply) 페이지 지정하여 넘김
mv.addObject("id", 50);
mv.addObject("pw", "1234");
mv.setViewName("board/reply");
return mv;
}
}
---------------------------------------------------------------------
[계산 main.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>list</title>
</head>
<body>게시판 메인 페이지입니다.
</body>
</html>
---------------------------------------------------------------------
[계산 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>list</title>
</head>
<body>
게시판 컨텐트 페이지입니다.<br>
${id}<br>
${pw}<br>
</body>
</html>
---------------------------------------------------------------------
[계산 reply.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>list</title>
</head>
<body>
리플라이 페이지입니다.<br>
${id}<br>
패스워드 : ${pw}<br>
</body>
</html>
[결과값]





[계산 GradeController.java]
package edu.global.ex.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.*;
import org.springframework.ui.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import edu.global.ex.vo.GradeVO;
//1.URL과 페이지 매핑 방법
//2.뷰(jsp)에 데이터 전달
@RequestMapping("/grade") // http://localhost:8282/grade
@Controller
public class GradeController {
@GetMapping("/") // http://localhost:8282/grade/
public String grade_form() {
return "grade_form";
}
@PostMapping("/total") // http://localhost:8282/grade/total
public String grade_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"));
// int total = kor + eng + math;
// double avg = total / 3.0;
GradeVO grade = new GradeVO(kor, eng, math); // lombok으로 만든 생성자에 객체로 넘긴다
model.addAttribute("grade", grade);
return "grade";
}
@PostMapping("/total2") // http://localhost:8282/grade/total2
public String grade_form2(GradeVO gradeVO, Model model) { // 커맨드 객체 : GradeVO gradeVO
// grade_form2(GradeVO gradeVO) == GradeVO gradeVO = new gradeVO(); 의 역할을 함
// gradeVO.setkor(70); 를 호출한다.
// gradeVO.setEng(70); 를 호출한다.
model.addAttribute("grade", gradeVO);
return "grade";
}
}
---------------------------------------------------------------------
[계산 GradeVO.java]
package edu.global.ex.vo;
import lombok.*;
//Grade grade = new Grade();
//System.out.print(grade) >> 주소값이 나옴 | toString 함수 오버라이딩을 사용하면 값이 나온다.
@Getter // : 컴파일 될 때 getter함수가 자동으로 생성
@Setter // : 컴파일 될 때 setter함수가 자동으로 생성
@NoArgsConstructor // : 컴파일 될 때 비어있는 생성자 자동으로 생성
@AllArgsConstructor // : 컴파일 될 때 생성자 자동으로 생성
@ToString // : 주소값이 아닌 데이터 멤버 값이 나오도록 변경
//@Data // : 상기 모든 것을 포함한 어노테이션
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 String getGrade() {
String grade = "가";
double avg = getAvg();
if (avg >= 90) {
grade = "수";
} else if (avg >= 80) {
grade = "우";
} else if (avg >= 70) {
grade = "미";
} else if (avg >= 60) {
grade = "양";
} else {
grade = "가";
}
return grade;
}
}
---------------------------------------------------------------------
[계산 grade_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>list</title>
</head>
<body>
<form action="/grade/total" method = "post">
국어 : <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><br><hr>
<form action="/grade/total2" method = "post">
국어 : <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>list</title>
</head>
<body>
합계 : ${grade.getTotal()}
<br> 평균 : ${grade.getAvg()}
<br> 성적 : ${grade.getGrade()}
<br>
<br> 또는
<br>
<br> 합계 : ${grade.total}
<br> 평균 : ${grade.avg}
<br> 성적 : ${grade.grade}
<br> toString : ${grade.toString()}
<br>
</body>
</html>
[결과값]

