스프링(redirect)

서울IT코드정리 /kyChoi·2021년 11월 14일
0

스프링

목록 보기
14/17

RequestMapping 에서 get방식과 post 방식

<form action ="student" method="get">
	student id: <input type="text" name="id"><br/>
	<input type="submit" value="전송">
</form>

@RequestMapping(method = RequestMethod.GET value="/student")
public String goStudent(HttpServletRequest httpServletRequst, Model model){
System out.println(RequestMethod.Get);

String id = httpServletRequest.getParameter("id");
System.out.println("id : " + id);
model.addAttribute("studentId", id);

return "student/studentId";

}

-> form 태그에서 get 방식인데, RequestMapping에서 get 방식이 아닌, post 방식 도는 form 태그에서 post 인데 맵핑에서 get 이라면 405
에러가 발생합니다

///

@ModelAttribute

@ModelAttribute 어노테이션을 이용하면 커맨드 객체의 이름을 개발자가 변경할 수 있습니다.

@RequestMapping("/studentView")
public String studentVeiw(StudentInformation studentInformation //커맨드 객체)

return "studentVeiw";
}

윗 로직은

이름 : ${studentInformation.name}
나이 : ${studentInformation.age}

@RequestMapping("/studentView"){
public String studentView(@ModelAttribute("studentInfo") StudentInformation studentInformation){
return "studentView";

}

이름 : ${studentInfo.name}
나이 : ${studentInfo.age} StudentInformation - > studentInfo 로 이름이 바뀌었다
}

**리다이렉트(redirect)
다른 페이지로 이동할 때 사용합니다

@RequestMapping("/studentConfirm")
public String studentRedirect(HttpServletRequest httpServletRequest, Model model){
String id = httpServletRequest.getParameter("id");
if(id.equals("abc")){
return "redirect:studentOk";
}
return "redirect:studentNg";
}

id 가 abc 라면 웹은 studentOk 로 이동, 그게 아니면 studentNg 로 이동

@RequestMapping("/studentNg")
public String studentNg(Model model){
return "student/studentNg";
}

studentNg 는 studentNg mapping 에 걸리다

profile
건물주가 되는 그날까지

0개의 댓글