나의 수업일지

인천 일보 아카데미 44일차 -1- 실습

2023. 4. 24. 22:05
  • 1. 주요 동작 

1. 모든 요청은 MainController에서 처리함 
        2. 프로젝트 시작하면 index.jsp 출력 

 

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MainController {
    @GetMapping("/")
    public String index(){
        return "index";
    }
}

 

  • 2. index.jsp

    1. monday1.jsp를 출력하기 위한 링크 있음(요청주소: /monday1)
    2. monday2.jsp를 출력하기 위한 버튼 있음(요청주소: /monday2)
    3. monday3.jsp를 출력하기 위한 버튼 있음(요청주소: /monday3)
    4. monday4.jsp를 출력하기 위한 버튼 있음(요청주소: /monday4)

 

index.jsp

1번을 제외하면 2,3,4는 버튼이기 때문에 함수를 호출하여 주소를 리턴합니다

 

 

  • 3. monday1.jsp

1. 링크사용 monday1-param 주소로 month, day 파라미터를 get 방식으로 요청함
    a. js 함수 활용

monday1.jsp

 


2. 컨트롤러에서는 month, day 파라미터 값을 System.out으로 출력함 

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class MainController {
    @GetMapping("/monday1")
    public String monday1(){
        return "monday1";
    }
monday1.jsp를 화면에 보여주기 위한 GetMapping 메소드

    @GetMapping("/monday1-param")
    public String monday1_param(@RequestParam("month") String month ,
    				@RequestParam("day") String day ){
    파라미터로 받은 정보를 @RequestParam를 이용하여 각각의 String 변수에 담아줌
    
        System.out.println("month = " + month + ", day = " + day);
        이후 출력문을 사용하여 출력함(soutp 자동 완성 사용)
        
        return "monday1";
        이후 화면은 monday1.jsp를 보여줌
    }
    
}

 

 

  • 4. monday2.jsp

1. form 태그를 활용하여 monday2-param 주소로

이름(name), 전화번호(mobile) 정보를 get 방식으로 요청함

monday2.jsp


2. 컨트롤러에서는 파라미터 값을 model에 각각 담아서 result2.jsp에 출력함 

result2.jsp

${name} 이런 양식을 영어로는 String interpolation이라고 하고

한국어로는 문자열 보간이라고 한다

문자열 보간을 이용하여 Model인터페이스로 받은 속성값을 화면에 출력해준다

 

 

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class MainController {
    @GetMapping("/monday2")
    public String monday2(){
        return "monday2";
    }
monday2.jsp를 화면에 보여주기 위해
return을 monday2로해주는 GetMapping

    @GetMapping("/monday2-param")
    public String monday2_param(Model model,
    				@RequestParam("name") String name,
                		@RequestParam("mobile") String mobile){
	사용자가 입력한 값을 파라미터로 전송받고,
    전송받은 파라미터를 @RequestParam으로 정보를 꺼내왔다

        model.addAttribute("name",name);
        model.addAttribute("mobile",mobile);
        이후 Model 인터페이스를 이용해 해당 꺼내온 정보를
        Model 객체에 담아주고 result2.jsp를 보여주기 위해 result2를 리턴
        return "result2";
    }
}

 

 

  • 5. monday3.jsp

1. form 태그를 활용하여 monday3-param 주소로

이메일(email) , 비밀번호(password) 정보를 post 방식으로 요청함

monday3.jsp

 

2. 컨트롤러에서는 파라미터 값을 MemberDTO객체

필드값으로 담아서 result3.jsp에 출력함

MemberDTO/ result3.jsp

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.ui.Model;

@Controller
public class MainController {
    @GetMapping("/monday3")
    public String monday3(){
        return "monday3";
    }
monday3.jsp를 화면에 보여주기 위해
return을 monday3로해주는 GetMapping
    
    @PostMapping("/monday3-param")
    public String mondy3Param(@ModelAttribute MemberDTO memberDTO , Model model){
     
     매개변수에 ModelAttribute로 파라미터로 받은 값을 바로 넣을 수 있다
     하지만 전송받는 파라미터의 name과 DTO의 필드 이름이 같아야한다
     
        model.addAttribute("email",memberDTO.getEmail());
        model.addAttribute("password",memberDTO.getPassword());
        그외로 한번에 출력하는 것이 아닌
        따로 출력하기 위해서는 이렇게 따로 받아주는 작업이 필요하다
        
        return "result3";
        result3.jsp에 띄울것이기 때문에 reuturn을 result3로 보낸모습
    }
}

 

 

  • 6. monday4.jsp

1. 컨트롤러에서 10명의 회원 정보 (이메일,비밀번호만)를 만들어서

memberDTOList에 담고 model에 담은 뒤 monday4.jsp에 표 형태로 출력

monday4.jsp

forEach문을 사용하는 c태그는 롬북이기 때문에 롬북 태그가 필요하다

자동완성을 사용하면 임폴트처럼 자동으로 생성되지만 안된다면 아래를 복사 붙여넣기하자

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

Controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MainController {
    private List<MemberDTO> newMemberDTO(){
        List<MemberDTO> memberList = new ArrayList<>();
        for (int i=1; i<=10; i++) {
            MemberDTO memberDTO = new MemberDTO();
            memberDTO.setMemberID((long) i);
            memberDTO.setEmail("이메일" + i);
            memberDTO.setPassword("비밀번호" + i);
            memberList.add(memberDTO);
        }
        return memberList;
    }
    정해진 멤버의 정보를 담는것이 아니기 때문에
    따로 메소드를 만들어 쉽게 담아주었습니다
    객체의 정보를 ArrayList에 담아주고 리턴합니다

    @GetMapping("/monday4")
    public String monday4(Model model){
        List<MemberDTO> newList = newMemberDTO();
        model.addAttribute("mList",newList);
        위의 메소드를 생성자로 사용하여 newList에 담아줍니다
        Modle 인터페이스를 이용하여 담아 출력합니다
        
        model.addAttribute("mList",newMemberDTO());
        애초에 ArrayList를 따로 받지 않고
        이렇게 Model인터페이스에 속성값으로 바로 담아
        사용하는 방법도 있습니다
        
        return "monday4";
    }
}

'나의 수업일지' 카테고리의 다른 글

인천 일보 아카데미 45일차 -1- 실습 / MySQL - DB / table 만들기  (0) 2023.04.25
인천 일보 아카데미 44일차 -2- ModelAttribute / 체크박스로 받은 값 담기 / @Service 알아보기 / 의존성 주입 - Autowired  (0) 2023.04.25
인천 일보 아카데미 43일차 -1- 프로젝트 다시 만들기 / Model 알아보기 / Model - 객체를 출력 / Model - List를 출력  (0) 2023.04.23
인천 일보 아카데미 42일차 -2- 어노테이션 - Controller , GetMapping / 주소를 parameter로 / parameter로 받은 값 꺼내기 /  (0) 2023.04.20
인천 일보 아카데미 42일차 -1- IntelliJ - Spring Framework 프로젝트 만들어보기  (0) 2023.04.20
'나의 수업일지' 카테고리의 다른 글
  • 인천 일보 아카데미 45일차 -1- 실습 / MySQL - DB / table 만들기
  • 인천 일보 아카데미 44일차 -2- ModelAttribute / 체크박스로 받은 값 담기 / @Service 알아보기 / 의존성 주입 - Autowired
  • 인천 일보 아카데미 43일차 -1- 프로젝트 다시 만들기 / Model 알아보기 / Model - 객체를 출력 / Model - List를 출력
  • 인천 일보 아카데미 42일차 -2- 어노테이션 - Controller , GetMapping / 주소를 parameter로 / parameter로 받은 값 꺼내기 /
GUPING
GUPING
안녕하세요 개발자를 꿈꾸는 GUPING 입니다
GUPING
개발자를 꿈꾸는 GUPING
GUPING
전체
오늘
어제
  • 분류 전체보기 (89)
    • 나의 수업일지 (88)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 파일 이름 가져오기
  • 틀만들기
  • 업로드파일이름출력
  • naver
  • PAGE
  • 버튼2가지이벤트
  • 개힘드렁
  • Paging
  • realative
  • 로그인게시판
  • 삭제
  • 여러사진프리뷰
  • 지식인
  • 버튼 이벤트 함수 변경
  • 답변 채택
  • 로그인form
  • 버튼 이벤트 변경
  • 페이징Controller
  • 파일이름(다중)
  • 다중프리뷰
  • Ajax
  • 파일이름가져오기
  • 검색
  • 클래스 이름 변경
  • pagingAjax
  • 페이징
  • 수정
  • 검색결과페이징
  • 로그인 경고문
  • 네이버

최근 댓글

최근 글

hELLO · Designed By 정상우.
GUPING
인천 일보 아카데미 44일차 -1- 실습
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.