Notice
Recent Posts
Recent Comments
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
관리 메뉴

충분히 쌓여가는

중복된 URL 코드 제거(@RequestMapping) 본문

Spring/연습

중복된 URL 코드 제거(@RequestMapping)

빌드이너프 2023. 10. 3. 11:45
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");
    }
}