7장 컨트롤러 데이터 딜리버리 이어서...

 

@GetMapping("/request2")
public String request2(Model model, Ch07Board board) {

    model.addAttribute("chNum", "ch07");
    return "ch07/writeBoardForm";
}

/request2 부터 시작

DTO: reqeust 범위부터 시작.

 

writeBoardForm.jsp 작성

 

폼안의 버튼은 서밋효과가 있다.

 

@GetMapping("/request2")
public String request2(Model model) {
    model.addAttribute("chNum", "ch07");

    List<String> jobList = new ArrayList<>();
    jobList.add("개발자");
    jobList.add("공무원");
    jobList.add("은행원");
    model.addAttribute("jobList", jobList);
    model.addAttribute("defaultJob",  "은행원");

    List<String> cityList = new ArrayList<>();
    cityList.add("서울");
    cityList.add("대전");
    cityList.add("제주");
    model.addAttribute("cityList", cityList);
    model.addAttribute("defaultCity",  "제주");


    return "ch07/joinForm";
}

다르게 사용해봄. joinForm.jsp 생성

 

select 태그와 option태그 (default속성값 selected)

 

 

@ModelAttribute("jobList")
public List<String> getJobList() {
    List<String> jobList = new ArrayList<>();
    jobList.add("개발자");
    jobList.add("공무원");
    jobList.add("은행원");

    return jobList;
}

컨트롤러에 추가

 

----

@GetMapping("/sessionLoginForm")
public String sessionLoginForm(Model model) {

  model.addAttribute("chNum", "ch07");
  return "ch07/loginForm";
}

@PostMapping("/sessionLogin")
public String sessionLogin(Model model, HttpSession session) {
  session.setAttribute("login", "success");
  return "redirect:/ch07/sessionLoginForm";
}

@GetMapping("/sessionLogout")
public String sessionLogout(Model model, HttpSession session) {
  session.removeAttribute("login");
  return "redirect:/ch07/sessionLoginForm";
}

 

application 범위

@GetMapping("/application")
public String application(Model model, HttpServletRequest request) {
   model.addAttribute("chNum", "ch07");

   // application 범위에 counter 이름의 값(객체)를 가져오기
   ServletContext application = request.getServletContext();
   Integer counter = (Integer) application.getAttribute("counter");

   // 값(객체) 존재 유무에 따라 처리
   if(counter == null) {
       application.setAttribute("counter", 1); // 1이라는 값이 Integer 객체로 boxing이 됨
   } else {
       application.setAttribute("counter", 1 + counter); // 계속적으로 1씩 추가가 된다.
   }

   return "ch07/applicationData";
}

크롬과 엣지에서 클릭하여 카운터를 올리면 서로 공유한다.

application 범위를 사용하면 모든 브라우저에서 값을 공유하는 특성을 가졌다.

 

p70

# 데이터(객체) 사용범위 scope

request 범위 컨트롤러 jsp 하나의 서버에서의 범위

session 범위 예시 유저가 로그인 후 로그아웃으로 해제하기 전까지의 정보를 담아둘 수 있는. 해당 브라우저에서 이용하기까지 남아있는 데이터 범위

application 범위 예시 모두가 공용으로 사용할 수 있도록 남아있는 데이터, 즉 게시판같은 어떤 브라우저에서 접속하더라도 남아있는 데이터 범위를 뜻한다.

 

request 범위 / Model / ModelAndVeiw / @modelAttribute / HttpSerletRequest

seission 범위 HttpSession

application 범위 ServletContext

 

$ {} 는 JSP에서 위의 범위에서 사용하는 데이터를 불러올 수 있게끔 해줌

${"이름"} / ${"이름".게터 or 객체의 이름}

 

request 범위 HttpServletRequest 예시

request.setAttribute("이름", 값);

session.setAttribute("이름", 값);

application.setAttribute("이름", 값);

를 사용하였을 때 JSP에서 ${"이름"} 을 사용한다면 어느 범위에서 값을 찾아올까?

1. request 범위에서 해당 이름을 찾고,

2. session 범위에서 찾고,

3. application 범위에서 찾는다.

4. 만약 모든 범위에서 데이터가 없다면 오류를 출력하지 않고 아무것도 출력하지 않는다.

 

JSTL 사용해보았던 것.

<c:forEach ---> items="${'이름'}" 이렇게 속성의 값으로 사용할수도 있었다.

<c:if ---> test=${이름='xxx'?xxx:xxx} 이렇게 조건식으로도 사용했었다.

 

input 태그에 value="${이름}" 이렇게도 사용해보았다.

이렇게 EL은 request범위, session범위, application범위에 해당 값이 있으면 폭 넓게 사용할 수 있다.

 

 

 

ch07 첫번째 실습으로 사용해보았다.

 

8장 컨트롤러 세션 서포트

remove로 제거하는 것 뿐만아니라 invalidate(유효하지 않은) 즉 무효화시켜 제거가 가능하다.

remove는 세션에서 특정한 객체만 제거

invalidate는 세션 객체 전부를 제거

 

# ch08컨트롤러 생성

--> 장바구니 구현해보기

1. 컨트롤러부터 구성. (무엇이 필요한지 먼저 생각)

2. 컨트롤러 구성을 대략적으로 끝낸 뒤 필요한 JSP 만들기

3. JSP 대략적인 구성 완료 뒤 DTO 객체 만들기

4. DTO와 컨트롤러를 이용하여 JSP에 출력해보기.

 

컨트롤러와 JSP의 데이터 전달방식 확실히 이해하기.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'JAVA' 카테고리의 다른 글

41일차 2024-04-24  (0) 2024.04.24
40일차 2024-04-23  (0) 2024.04.23
34일차 2024-4-15  (0) 2024.04.15
33일차 2024 - 4 - 12  (0) 2024.04.12
32일차 2024 - 4 - 11  (0) 2024.04.11

+ Recent posts