충분히 쌓여가는
중복된 URL 코드 제거(@RequestMapping) 본문
package com.buildenough.ch2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping("/login")
@Controller
public class LoginController {
@GetMapping("/login")
public String showLogin() {
return "login";
}
@PostMapping("/login")
public String login(String id, String pwd) {
if(loginCheck(id, pwd)) {
return "userInfo";
} else {
return "redirect:login/login";
}
}
private boolean loginCheck(String id, String pwd) {
return id.equals("asdf") && pwd.equals("1234");
}
}
/login이라는 것이 중복되어 있다
package com.buildenough.ch2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LoginController {
@GetMapping("/login/login")
public String showLogin() {
return "login";
}
@PostMapping("/login/login")
public String login(String id, String pwd) {
if(loginCheck(id, pwd)) {
return "userInfo";
} else {
return "redirect:login/login";
}
}
private boolean loginCheck(String id, String pwd) {
return id.equals("asdf") && pwd.equals("1234");
}
}
@RequestMapping을 사용
메서드의 "/login" 중복을 제거해준다(클래스에 붙은 /login과 각 메서드의 /login이 합쳐져서 사용된다)
package com.buildenough.ch2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping("/login")
@Controller
public class LoginController {
@GetMapping("/login")
public String showLogin() {
return "login";
}
@PostMapping("/login")
public String login(String id, String pwd) {
if(loginCheck(id, pwd)) {
return "userInfo";
} else {
return "redirect:login/login";
}
}
private boolean loginCheck(String id, String pwd) {
return id.equals("asdf") && pwd.equals("1234");
}
}
'Spring > 연습' 카테고리의 다른 글
JSP, thymeleaf, text, utext, |(리터럴 치환) (0) | 2023.10.04 |
---|---|
@RequestMapping, @GetMapping, @PostMapping (0) | 2023.10.03 |
매개변수 묶어서 사용(@ModelAtrribute) (0) | 2023.10.02 |
컨트롤러 메서드의 반환타입(String, void, ModelAndView) (0) | 2023.10.02 |
thymeleaf 기본 설정 (0) | 2023.10.02 |