스프링 프레임워크

 

자바 기반의 오픈소스 프레임워크

복잡한 기능을 구현하기 위해 만들어졌다.

 

대표적인  스프링 프로젝트

  • 스프링 부트 : 간편하게 스프링 프로젝트를 시작할 수 있도록 해주며, 마이크로 서비스 개발 및 애플리케이션 개발에 적합하다.
  • 스프링 데이터 : 데이터베이스 연동을 위한 개발을 지원한다.
  • 스프링 배치 : 대량의 데이터를 일괄 처리하기 위한 솔루션이다.
  • 스프링 시큐리티 : 보안과 관련되어 있다.
  • 스프링 HATEOAS : REST API에 대해 하이퍼 미디어 기반으로 서비스 정보를 제공하는 기능으로, API 구조 파악이나 테스트에 유용하다.

 

필자가 할 건 스프링 부트이다.

 


MVC

Model, View, Controller의 약자로, 웹 애플리케이션을 비즈니스 로직, 포레젠테이션로직, 데이터로 분리하는 디자인 패턴

 

  • Model : 비즈니스 로직 및 데이터 처리 담당
  • View : 모델이처리한 결과 데이터의 화면
  • Controller : 요청처리 및 흐름 제어 담당

 

 

 

 

컨트롤러 설정

// 컨트롤러 명시
@Controller
public class CalcController {
//	url 설정 : 8080/calcInput
	@RequestMapping(value = "/calcInput")
	public ModelAndView inputForm() {
//		ModelAndView 객체 생성
		ModelAndView mav = new ModelAndView();
//		뷰의 calcForm.html 실행
		mav.setViewName("calcForm");
		return mav;
	}
	@RequestMapping(value = "/calcControl")
	public ModelAndView hello2(HttpServletRequest request) {
		String sn1 = request.getParameter("num1");
		String sn2 = request.getParameter("num2");
		String op = request.getParameter("op");
		int n1 = Integer.parseInt(sn1);
		int n2 = Integer.parseInt(sn2);
		int result = 0;
		Calculator model = new Calculator();
		result = model.calc(n1, n2, op);
		ModelAndView mav = new ModelAndView();
//		뷰에 오브젝트를 myResult변수명으로 넘겨준다
		mav.addObject("myResult", result);
		mav.setViewName("calcResult");
		return mav;
	}
}

 

 

 

모델 설정

public class Calculator {
//	실제 업무 계산을 처리하는 모델
	public int calc(int n1, int n2, String operator) {
		int result = 0;
		switch(operator) {
		case "+":
			result = n1 + n2;
			break;
		case "-":
			result = n1 - n2;
			break;
		case "*":
			result = n1 * n2;
			break;
		case "/":
			result = n1 % n2;
			break;
		}
		return result;
	}
}

 

 

 

뷰 설정

 

 

-calcForm.hmtl-

<body>
	<h2>계산기 - 입력 뷰</h2>
	<hr>
	<form action="calcControl" method = "post">
		<input type="text" name="num1" size="10">
		<select name = "op">
			<option>+</option>
			<option>-</option>
			<option>*</option>
			<option>/</option>
		</select>
		<input type="text" name="num2" size="20">
		<input type="submit" value="실행">
	</form>
</body>

 

 

-calcResult.html-

<body>
	<h1>결과 보는 페이지</h1>
	<h1 th:text="'계산결과: ' + ${myResult} + '입니다!!' "></h1>
</body>

'JAVA > JAVA' 카테고리의 다른 글

[JAVA] 인터페이스  (0) 2023.12.22
[JAVA] this, super  (2) 2023.12.22
[JAVA] 추상 클래스  (0) 2023.12.22
[JAVA] 상속  (1) 2023.12.21
[JAVA] 접근 제한자  (0) 2023.12.20

+ Recent posts