spring/spring

spring 시작하기

bono.html 2022. 8. 29. 17:52

 

설치과정

 

- 파일 없는 경우

2022.08.29 - [시작하기] - (eclipse spring) jdk, Apache 설치하기

- 파일 있는 경우

2022.08.29 - [시작하기] - (eclipse spring) 설치하기 2

 

 

파일 만들기

new -> New Spring Legacy project

Spring MVC Project 가 없는 경우

https://integer-ji.tistory.com/232

 

크롬으로 실행하기

Home 파일 우클릭

controller 수정해서 경로 연결하기

 

HomeController

package com.num.hello;

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

import javax.servlet.http.HttpServletRequest;

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;


// @Controller 는 이 객체로 클라이언트의 요청에 대해서 응답 하겠다는 의미
@Controller
public class HomeController {

	
	// 해당 context 에 root 요청이 왔을때 이 메소드로 응답하겠다는 의미
	/*
	 *  HttpServletRequest, HttpServletResponse, HttpSession 등의 객체가 메소드 안에서
	 *  필요하다면 필요한 객체를 전달받을 매개변수를 선언만 해  놓으면 자동으로 객체가 전달된다.
	 */
	@RequestMapping("/")
	public String home(HttpServletRequest request) {
		// request 영역에 fortuneToday 라는 키값으로 String type 담기
		request.setAttribute("fortuneToday", "동쪽으로 가면 귀인을 만나요!");
		
		/*
		 * 여기서 리턴한 문자열의 접두어로 "/WEB-INF/view/" 가 붙고
		 * 접미어로 ".jsp" 가 붙어서 
		 * 
		 * "/WEB-INF/views/"+"home"+".jsp"
		 * 
		 * 결과적으로 "/WEB-INF/views/home.jsp" 가 되어서
		 * 해당 jsp 페이지로 forward 이동 되어서 응답하게 된다.
		 */
		return "home";
	}
	
	// "/study" 경로의 요청이 왔을때 이 메소드를 이용해서 응답을 하겠다는 의미
	@RequestMapping("/study")
	public String study() {
		
		// /WEB-INF/views/study.jsp 페이지로 forward 이동해서 응답하겠다는 의미
		return "study";
	}
	
	@RequestMapping("/play")
	public String play() {
		
		// /WEB-INF/views/study.jsp 페이지로 forward 이동해서 응답하겠다는 의미
		return "play";
	}
	
}

 

 

home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>/views/home.jsp</title>
</head>
<body>
<div class="container">
	<h1>인덱스 페이지 입니다.</h1>
	<p>오늘의 운세 : <strong>${requestScope.fortuneToday}</strong></p>
	<ul>
		<li><a href="${pageContext.request.contextPath}/study">공부하러 가기</a></li>
		<li><a href="${pageContext.request.contextPath}/play">놀러가기</a></li>
	</ul>
</div>
</body>
</html>

 

study.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>/views/study.jsp</title>
</head>
<body>
	<p>
		공부 페이지 입니다. 열심히 공부해 보아요
		<a href="${pageContext.request.contextPath}/">인덱스로 돌아가기</a>
	</p>
</body>
</html>

 

play.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>/views/play.jsp</title>
</head>
<body>
	<p>
		play 페이지 입니다. 재밌게 놀아 보아요
		<a href="${pageContext.request.contextPath}/">인덱스로 돌아가기</a>
	</p>
</body>
</html>