Cheat Sheet

#HttpServletRequest # RequestParam #데이터객체 #ModelAttribute #Model 본문

Back End/프레임워크

#HttpServletRequest # RequestParam #데이터객체 #ModelAttribute #Model

원파 2018. 8. 29. 11:10

#HttpServletRequest #RequestParam #데이터객체 

#ModelAttribute  #Model

 

 

 

# HttpServletRequest 

사용자가 요청한 데이터를 저장하고 있다.

예를들어

http://localhost:8181/ex/board/confirmId?id=abcde&pw=12345

로 요청이 들어왔을때

controller에서 

HttpServletRequest httpservletRequest 를 인자로 받는다면

String id = httpservletRequest.getparameter("id");

String pw = httpserveletRequest.getparameter("pw");

와 같이 받을수 있다.

 

또한, 

String context = httpservletRequest.getContextPath();

를 통하여서 contextpath도 얻을수 있다.

 

contetpath는
처음 project만들때
첫 패키지 이름을 
com.javalec.spring_mvc_ex1
이라고 설정한다면
1)com
2)javalec
3)spring_mvc_ex1

 

3번째 있는 것이 contextpath가 된다.

 

 

# RequestParam 어노테이션

@RequestParam은 HttpServletRequest와 역할이 비슷하다

차이점은 특정이름으로 넘어오는 값을 받는다는 것이다.

같은 예로

http://localhost:8181/ex/board/confirmId?id=abcde&pw=12345

로 요청이 들어왔을때

controller에서 

@RequestParam("id") String ID

를 인자로 받는다면

controller에서 id라는 이름으로 넘어온 값을

ID라는 이름으로 사용이 가능하다.

즉, 'id'넘어온 값을 ID로 받아서 사용하는 것이다.

 

 

# 데이터객체

데이터객체는 요청하는 값이 많을때 사용된다.

예를들어

회원가입을 할때 controller로 넘어오는 parameter가 많은데

위의 예에서 처럼

http://localhost:8181/ex/board/confirmId?id=abcde&pw=12345&..&..&..&..&..&..&..&..&..&..&..

이런식으로 값들이 넘어왔을때 

controller에서 일일이 값들을 받는다면 코드가 너무 길어지기 때문에

DTO와 같은 객체class를 만들어서 

그곳에 값을 받은 인자를 만들고

getter, setter를 통해 값들을 사용하면 훨씬 편리하다.

 

 

# ModelAttribute 어노테이션

@RequestMapping은 데이터객체의 이름을 바꾸는 것이다.

controller에서

@ModelAttribute("stuInfo") StudentInformation studentinfomation

을 인자로 받는다면

studentinfomation는 controller에서 사용되는 이름

stuInfo는 jsp로 넘어갔을때

${stuInfo.id}

${stuInfo.name}

과 같이 사용하는 이름이 된다.

 

 

# Model

Model은 

controller에서

Model model을 인자로 갖는다면

model.addAttribute("id",ID);

와 같은 형식으로 사용하는데

""안에들어간 id는 jsp에서 활용할 이름을 쓰는곳이고

ID부분은 contorller에서 받아온 값을 써주면된다.

즉, id라는 이름에 ID의 값을 넣어주는 것이다. 이것은 jsp에서 id라는 이름으로 가져올수 있다.

${id}

이렇게

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'Back End > 프레임워크' 카테고리의 다른 글

#SpringBoot #Swagger  (0) 2023.09.08
#SpringBoot #lombok  (0) 2023.09.08
#Spring Boot #log4j  (0) 2023.09.08
#Interceptor #login #spring  (0) 2019.04.05
#CKEditor #이미지업로드  (0) 2018.08.08