간펴니
간편 자바프로그래밍
간펴니
전체 방문자
오늘
어제
  • 전체보기 (185)
    • 알고리즘 (2)
    • JAVA (69)
      • 이펙티브 자바 (47)
      • JAVA 병렬프로그래밍 (5)
      • 자바 (17)
    • SPRING (60)
      • Spring (12)
      • IceWater Community (37)
      • Homme Shop (10)
      • 토비의 스프링 (1)
    • SPRING BOOT (4)
      • WhiteRecord (7)
    • 오류 (9)
    • DB (10)
      • ORACLE (5)
      • MYSQL (1)
      • MYBATIS (4)
      • JPA (0)
      • 대용량 데이터 베이스 (0)
      • SQL (0)
    • FRONT (8)
      • JSP (2)
      • JavaScript (2)
      • Jquery (3)
      • Thymeleaf (1)
    • AWS (6)
    • JNI (10)
    • 회고 (0)
    • MQ (0)
    • Radis (0)
    • Git (0)
    • Docker (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 블로그 컨셉 변경

인기 글

태그

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
간펴니

간편 자바프로그래밍

SPRING/Spring

스프링 데이터를 전달하는 형태(Model,ModelAndView)

2021. 7. 18. 03:24
728x90

1. Model - 파라미터 방식으로 메소드에 (Model model)파라미터를 넣어주고 String형태로 리턴한다

@RequestMapping("/joinRequest")
	public String join(HttpServletRequest request, Model model) {
		model.addAttribute("name", request.getParameter("name"));
		model.addAttribute("gender", request.getParameter("gender"));
		model.addAttribute("email", request.getParameter("email"));
		model.addAttribute("method", "HttpServletRequest");
		
		return "board/index";
	}

 - 실행되어질 Method의 파라미터로 Model 타입의 변수를 선언한 후
  선언한 변수에 attribute로 데이터를 담는다.
  : Model타입변수.addAttribute("속성명", 저장할데이터);

 

 

2. ModelAndView - 컴포넌트 방식으로 ModelAndView 객체를 생성해서 객체형태로 리턴한다.

	@RequestMapping("/joinRequest")
	public ModelAndView join(HttpServletRequest request) {
		ModelAndView model = new ModelAndView();
		model.addObject("name",request.getParameter("name"));
		model.addObject("gender",request.getParameter("gender"));
		model.addObject("email",request.getParameter("email"));
		model.addObject("method", request.getParameter("HttpServletRequest"));
        	model.setViewName("board/index");
		
		return model;
		}


  - ModelAndView 객체를 생성한 후 addObject로 데이터를 담는다.
  ModelAndView model = new ModelAndView();
  model.addObject("속성명", 저장할데이터);

- setViewName으로 이동하고자 하는 view를 저장
 

- 선언한 ModelAndView 객체를 반환한다, return model;

 

* ModelAndView는 @Controller를 이용하기 전부터 사용했지만, Spirng MVC가 @Controller annotation을

지원하기 시작한 이후로 ModelAndView는 잘사용하지 않는다고 함.




728x90

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

HttpserveltRequest와 HttpSession,세션과 로그인 로그아웃  (1) 2021.08.03
UriComponents과 URLEncoding  (0) 2021.07.31
AOP를 이용한 트랜잭션 구현 중 트랜잭션이 먹지 않는 현상과 메모리누수경고(Memory leak)=(톰캣구동안됨) 404에러 해결  (1) 2021.07.21
스프링 게시글 글쓰기 및 수정, ModelAttribute와 RedirectAttributes의 사용  (0) 2021.07.20
스프링 파라미터 받아오기(HttpServletRequest,RequestParam)  (0) 2021.07.18
    'SPRING/Spring' 카테고리의 다른 글
    • UriComponents과 URLEncoding
    • AOP를 이용한 트랜잭션 구현 중 트랜잭션이 먹지 않는 현상과 메모리누수경고(Memory leak)=(톰캣구동안됨) 404에러 해결
    • 스프링 게시글 글쓰기 및 수정, ModelAttribute와 RedirectAttributes의 사용
    • 스프링 파라미터 받아오기(HttpServletRequest,RequestParam)
    간펴니
    간펴니
    개발공부 기록하는 곳

    티스토리툴바