๐ŸŒธ [Spring] | Controller , Map<>

0
post-thumbnail


๐Ÿ”น @Controllor ์–ด๋…ธํ…Œ์ด์…˜, handlerMapping์˜ ์ผ

  • bean ๋“ฑ๋ก + ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด requestMapping๊ณผ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค
  • @Controller๊ฐ€ ์—†์œผ๋ฉด @RequestMapping ์•„๋ฌด ์“ธ์ž‘๋„ ์—†๋‹ค
  • handlerMapping์€ @Controller๋“ค์„ ๊ด€๋ฆฌํ•ด์ค€๋‹ค
  • Controller, Service, DAO, DB๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋งŒ๋“ ๋‹ค
  • Controller๋Š” view ์ด๋ฆ„, ์ฆ‰ return String

๐Ÿ”น ViewResolver, DispatcherServlet์˜ ์ผ

  • Controller๊ฐ€ returnํ•œ String์— prefix์™€ suffixํ•ฉ์ณ์„œ DispatcherServletํ•œํ…Œ ์ค€๋‹ค
  • DispatcherServlet์ด ๊ทธ๊ฒƒ๋“ค ๋‹ค ํฌํ•จํ•ด์„œ responseํ•ด์ค€๋‹ค.

๐Ÿ”น UserController.java

@Controller 
@RequestMapping("/user") 
public class UserController {

    @Autowired
    private UserService service;

    @RequestMapping("/login") 
    public String login() { 
        return "user/login"; 
    }

    @RequestMapping("/join")
    public String join() { return "user/join"; }

    @RequestMapping(value="/join", method = RequestMethod.POST)
    public String join(UserEntity param) {
        System.out.println("param = " + param);
        service.join(param);
        return "redirect:user/join";
    }
}
  • ์ปจํŠธ๋กค๋Ÿฌ๋Š” ๋งคํ•‘๋‹ด๋‹น

  • ์„œ๋น„์Šค๋Š” ๋กœ์ง๋‹ด๋‹น

  • @Autowired : ์—ฐ๊ฒฐํ•˜๋ ค๋Š” ํด๋ž˜์Šค๊ฐ€ ์–ด๋…ธํ…Œ์ด์…˜ ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ๐Ÿ‘‰ bean๋“ฑ๋ก์ด ๋˜์–ด ์žˆ์ง€ ์•Š์•„์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ด์š”. ๊ทธ๋ฆฌ๊ณ  ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์ด(ํด๋ž˜์Šค๊ฐ€) ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ์žˆ์œผ๋ฉด ์—๋Ÿฌ ๋– ์š”.

  • bean : ๊ฐ์ฒด

  • @ : ์–ด๋…ธํ…Œ์ด์…˜, bean์„ ๋“ฑ๋กํ•˜๋Š” ๊ฑฐ์˜ˆ์š”. ๊ฐ์ฒด๋ฅผ ๋„ˆ๊ฐ€ ๋“ค๊ณ  ์žˆ์–ด~๋ผ๊ณ  ์–˜๊ธฐํ•˜๋Š” ๊ฑฐ๋ž‘ ๊ฐ™์•„์š”. ๊ทธ๋ฆฌ๊ณ  private๋ผ๋„ ๊ฐ’์ด ์ฃผ์ž…์ด ๋ผ์š”. ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๊ฑฐ์˜ˆ์š”.

@Service
public class UserService {

    @Autowired
    private UserMapper mapper;
    
    โ˜ @Mapper๊ฐ€ @Autowired๋กœ ๊ฐ’์ด ์ฃผ์ž…๋ผ์š”. mapper๊ฐ€ private์—ฌ๋„ ์™ธ๋ถ€์—์„œ ๊ฐ’์ด ์ฃผ์ž…๋ผ์š”.
    
@Mapper
public interface UserMapper {
    int insUser(UserEntity param);
}
  • @Controller
    • ์ฃผ์†Œ๊ฐ’๊ณผ ๋งคํ•‘ ๋  ์ˆ˜ ์žˆ๋Š” bean
    • Controller๋กœ ์–ด๋…ธํ…Œ์ด์…˜๋œ ์ˆœ๊ฐ„ web.xml์˜ <servlet>๊ณผ ์—ฐ๊ฒฐ๋ผ์š”.
  • @RequestMapping("๊ฒฝ๋กœ")
    • RequestMapping๋กœ ์–ด๋…ธํ…Œ์ด์…˜๋œ ์ˆœ๊ฐ„ ์„œ๋ธ”๋ฆฟ(web.xml - <servlet>)๊ณผ ๋งคํ•‘๋ผ์š”.
    • ํด๋ž˜์Šค์˜ ์œ„์— ์œ„์น˜ํ•˜๋ฉด 1์ฐจ ์ฃผ์†Œ๊ฐ’, ํด๋ž˜์Šค ๋ฐ‘์— ์œ„์น˜ํ•˜๋ฉด 2์ฐจ ์ฃผ์†Œ๊ฐ’์ด์˜ˆ์š”.
    • ์†์„ฑ์ด ์žˆ์–ด์š”. ๐Ÿ‘‰ method="RequestMethod.Post
    • ๊ธฐ๋ณธ ์†์„ฑ : value="๊ฒฝ๋กœ" method="RequestMethod.Get" ๐Ÿ‘‰ ๊ทธ๋ž˜์„œ ๋ณดํ†ต ์ƒ๋žตํ•˜์—ฌ ์จ์š”.
    • method="RequestMethod.Post๋ฅผ ์“ธ ๋•Œ๋Š” ๊ฒฝ๋กœ๋ฅผ value="๊ฒฝ๋กœ" ์ด๋ ‡๊ฒŒ ์ง€์ •ํ•ด์•ผ ํ•ด์š”. ์ƒ๋žตํ•  ์ˆ˜ ์—†์–ด์š”. ๐Ÿ‘‰ value="๊ฒฝ๋กœ" method="RequestMethod.Post
  • ๋ฉ”์†Œ๋“œ
    • return์œผ๋กœ redirect ๋ถ™์ด์ง€ ์•Š๊ณ  String๋งŒ ์ ์œผ๋ฉด ๋ฌด์กฐ๊ฑด dispatcher์„ ์ด์šฉํ•ด์„œ forward์‹œ์ผœ์š”.
    ๐Ÿ‘‰ ๋””์ŠคํŒจ์น˜ ๋ฐฉ์‹
    @RequestMapping("/join")
        public String join() { return "user/join"; }
    ๐Ÿ‘‰ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋ฐฉ์‹
     @RequestMapping(value="/join", method = RequestMethod.POST)
     public String join(UserEntity param) { return "redirect:user/join"; }
    • ๊ทธ๋ฆฌ๊ณ  <servlet>์ด return๋œ ๊ฒฝ๋กœ์˜ ์•ž์—๋Š” prefix๋ฅผ ๋ถ™์ด๊ณ 
      ๋’ค์—๋Š” suffix๋ฅผ ๋ถ™์—ฌ์ค˜์š”.
    • request.getRequestDispatcher.forward("๊ฒฝ๋กœ") ์™€ response.sendRedirect("๊ฒฝ๋กœ")๋ฅผ ์ง์ ‘ ํ•  ํ•„์š”๊ฐ€ ์—†๊ณ  SPRING์ด ํ•ด์ฃผ๋Š” ๊ฑฐ์˜ˆ์š”.

๐Ÿ”น ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ๋ฐ›์•„์ฃผ๋Š” controller๋ฉ”์†Œ๋“œ์˜ ์ธ์ž

@RequestMapping("/detail")
    public String detail(BoardDTO param) { ๐Ÿ‘ˆ ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์œผ๋กœ ๋ณด๋‚ธ ๊ฐ’
        System.out.println("iboard : " + param.getIboard());
        return "board/detail";
    }
  • ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์œผ๋กœ ๋ณด๋‚ธ ๊ฐ’์„ BoardDTO param, ์–˜๊ฐ€ ๋ฐ›์•„์ค˜์š”
  • ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์œผ๋กœ ๋ณด๋‚ธ ๊ฐ’์„ BoardDTO param์˜ ๋ฉค๋ฒ„ํ•„๋“œ์— ๋„ฃ์–ด์ฃผ๋Š” ๊ฑฐ์˜ˆ์š”
  • ๋ฉค๋ฒ„ํ•„๋“œ์˜ ์ด๋ฆ„์œผ๋กœ request.getparameter() ๋‹ค ํ•ด๋ณด๊ณ  ์žˆ์œผ๋ฉด param์— set()ํ•ด์ฃผ๋Š” ๊ฑฐ์˜ˆ์š”
  • ์—ฌ๊ธฐ์— ์ ๊ธฐ๋งŒ ํ•ด๋„ ์„œ๋ธ”๋ฆฟ์ด /detail ์ž‘์—…ํ•ด์ฃผ์ž–์•„์š”
  • ๊ทธ ์„œ๋ธ”๋ฆฟ์ด param๊นŒ์ง€๋„ ๊ฐ์ฒดํ™” ํ•ด์ค˜์š”
  • ๊ทธ๋ž˜์„œ ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์ด ์—†์–ด๋„ ๊ฐ์ฒด๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— getIboardํ•˜๋ฉด 0์ด ๋‚˜์™€์š”.
  • ๋งŒ์•ฝ ๊ฐ์ฒดํ™”๊ฐ€ ์•ˆ๋˜์–ด ์žˆ์œผ๋ฉด param์ž์ฒด๊ฐ€ null์ด๊ณ  null์ธ ์• ํ•œํ…Œ getiboardํ•˜๋ฉด error๊ฐ€ ํ„ฐ์ง€๋Š”๋ฐ ์•ˆํ„ฐ์ง€์ž–์•„์š”
  • ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์œผ๋กœ ๋ฐ›์•„์„œ ๊ฐ์ฒดํ™” ํ•ด์ฃผ๊ณ  ๋ฉค๋ฒ„ํ•„๋“œ์— set๊นŒ์ง€ ํ•ด์ฃผ๋Š” ๊ฑธ,
    ๊ทธ๋ƒฅ public String detail(BoardDTO param)์ด๋ ‡๊ฒŒ ์ ์œผ๋ฉด ๋์ธ๊ฑฐ์ฃ .
    ์‹ ๊ธฐํ•˜์ฃ ? ๐Ÿค—

boardController

    @ResponseBody
    @RequestMapping("/cmtInsSel")
    public Map<String, Integer> cmtInsSel() {
        Map<String, Integer> result = new HashMap<>(); 
        result.put("result", 1);
        return result;
    }


๐Ÿ”น @ResponseBody , @RestController

  • ํ™”๋ฉด์ด๋ž‘ ์ „ํ˜€ ์ƒ๊ด€์ด ์—†์–ด์š”.
  • ๋ฐ์ดํ„ฐ๋ž‘ ์ƒ๊ด€ ์žˆ์–ด์š”.
  • ๋ฌด์กฐ๊ฑด JASONํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์•„์š”.
  • ์ด ์–ด๋…ธํ…Œ์ด์…˜ ์•„๋ž˜์˜ ๋ชจ๋“  ๋ฉ”์†Œ๋“œ์™€ ๋ณ€์ˆ˜์˜ ๋ชฉ์ ์€ JASONํ™”๊ฐ€ ๋ชฉ์ ์ด์˜ˆ์š”.
  • ๊ทธ๋ƒฅ ์ปจํŠธ๋กค๋Ÿฌ๋Š” ํ™”๋ฉด๊ณผ ๋ฐ์ดํ„ฐ ๋ชจ๋‘ ์ƒ๊ด€์ด ์žˆ์–ด์š”. ์„œ๋ฒ„์‚ฌ์ด๋“œ๋ Œ๋”๋ง์ด์˜ˆ์š”.
  • ๋ฐ์ดํ„ฐ๋งŒ ์ค„ํ…Œ๋‹ˆ ํ™”๋ฉด ๋‹ˆ๊ฐ€ ๋งŒ๋“ค์–ด์„œ ์“ฐ์„ธ์š”. ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ ๋ Œ๋”๋ง์ด์˜ˆ์š”.

๐Ÿ”น Map<>

Map<>์„ ์“ฐ๋Š” ์ด์œ ๋Š” ๋งค๋ฒˆ class๋ฅผ ๋งŒ๋“ค๊ธฐ ๊ท€์ฐฎ์œผ๋‹ˆ๊นŒ์š”.


๐Ÿ”น ์ฐธ๊ณ 

์‚ฌ์ง„์€ ์ „๋””๋ฒ„๊ฑฐ์˜ ์ฝ”๋”ฉ์ด์•ผ๊ธฐ ๋ธ”๋กœ๊ทธ์—์„œ ๊ฐ€์ ธ์™”์–ด์š”.

์ „๋””๋ฒ„๊ฑฐ์˜ ์ฝ”๋”ฉ์ด์•ผ๊ธฐ ๋ธ”๋กœ๊ทธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

profile
๋ช‡ ๋ฒˆ์„ ๋„˜์–ด์ ธ๋„ ์•ž์œผ๋กœ ๊ณ„์† ๋‚˜์•„๊ฐ€์ž

0๊ฐœ์˜ ๋Œ“๊ธ€