Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

HomeController.JAVA 본문

SPRING/chapter03_MVC

HomeController.JAVA

GAWON 2023. 6. 27. 18:44
package org.joonzis.controller;

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

import org.apache.taglibs.standard.lang.jstl.test.beans.PublicBean1;
import org.joonzis.dto.StudentDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/*
	 * @RequestMapping : url-mapping
	 * 메소드를 대상으로 어노테이션을 붙인다
	 * value = "/" : 컨텍스트 패스를 의미, 서버:포트/디폴트패키지
	 * method : RequestMethod.GET : get/post방식
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		/*
		(request가 이제는model로 바뀐것)model.addAttribute("serverTime", formattedDate );
		*/
		model.addAttribute("serverTime", formattedDate );
		
		return "member/input";
		
		}
	
		@RequestMapping(value = "member/result", method = RequestMethod.POST)
		public String login(@RequestParam("id") String id, @RequestParam("pw") String pw, Model model ) {
			
			model.addAttribute("id", id);
			model.addAttribute("pw", pw);
			
			return "member/output";
		
		}
		// 주소에 http://localhost:8080/controller/a/b/c/d/e 를 입력 시 view01.jsp로 이동
		// method 생략 가능(기본 값 : get)
		// value 생략 가능 @RequestMapping("/a/b/c/d/e")
		@RequestMapping(value = "/a/b/c/d/e")
		public String goView01() {
			//1. 리턴 타입 : 뷰(view)를 리턴하기 때문에 언제나 String을 리턴한다.
			//2. 메소드명 : goview01 은 아무런 의미가 없다.(메소드 들의 이름만 다르면 된다.
			//3. 리턴 : "/view01" , "view01"의 차이점은 없다.
			return "/view01";
		}
		
		
		//메소드 이름 goView02
		//url에  "admin/view02"로 접근 시
		//view02.jsp 로 데이터 전달 하여 화면에 출력
		//속성 명 : id, 속성 값 :admin
		//속성 명 : pw, 속성 값 :1234
		@RequestMapping(value = "/admin/view02")
		public String goView02(Model model) {
			/*
			 * Model 클래스
			 * 
			 * 1. request의 attribute의 역할을 수행하는 클래스
			 * 2. attribute("속성명" , "값")방식으로 Attribute저장
			 * model.attribute == request.setAttribute
			 * 3. controller가 jsp 에게 파라미터를 전달하려면 무조건 model을 사용한다.
			 * 	(스프링 버전 2 전에는 ModelAndView를 사용했다)
			 * 4.Model model 을 매개변수로 선언한다.
			*/
			model.addAttribute("id", "admin");
			model.addAttribute("pw", "1234");
			
			return "/view02";
		}
		
		//url : index
		@RequestMapping("/index")
		public String goIndex() {
			return "index";
		}
		
		@RequestMapping(value = "v01", method= RequestMethod.POST)
		public String goResult1(Model model, StudentDto sDto) {
			model.addAttribute("sDto", sDto);
			return "result";
		}
		
		@RequestMapping(value = "v02", method= RequestMethod.POST)
		public String goResult2(@ModelAttribute("s")StudentDto dto) {
			return "result";
		}
		
	
		
}

'SPRING > chapter03_MVC' 카테고리의 다른 글

input.jsp / output.jsp  (0) 2023.06.27
SampleController.JAVA  (0) 2023.06.27
spring 환경설정 및 라이브러리 저장방법(chapter03_MVC)  (0) 2023.06.27
spring framework 실행 순서.PNG  (0) 2023.06.27
01_spring MVC.txt  (0) 2023.06.27