10.Spring. annotation의 활용

리턴 은 주로 String, ModelActivew, Object로 한다.
메소드 단독매핑

MainController.java
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
package com.bigdata2017.springex.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainController {

// request mapping (method 단독 매핑)
// 접속시 MainController:main 출력됨
@ResponseBody
@RequestMapping("/main")
public String main() {
return "MainController:main";
}


@ResponseBody
@RequestMapping("/main2/a/b/c")
public String main2() {
return sendMessage("main2");
}

private String sendMessage( String message) {
return "MainController:"+message;
}
}

타입+ 메소드 메핑

UserController.java
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
31
32
33
34
35
36
37
38
39
40
41
package com.bigdata2017.springex.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/* request mapping( type + method) */

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

@ResponseBody
@RequestMapping("/joinform")
public String joinform() {
return "UserController:joinfrom";

}

@ResponseBody
@RequestMapping({"/join","/doJoin"})
public String join() {
return "UserController:join";

}

@ResponseBody
@RequestMapping( value = "/loginform", method=RequestMethod.GET)
public String loginform() {
return "UserController:loginform";
}


@ResponseBody
@RequestMapping( value = "/login", method=RequestMethod.GET)
public String login() {
return "UserController:login";
}
}

타입단독 메핑

GuestbookController.java
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
package com.bigdata2017.springex.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//Request Mapping (type 단독 매핑)

@Controller
@RequestMapping("/guestbook/*")
public class GuestbookController {

@ResponseBody
@RequestMapping
public String list() {
return "GuestbookController:list";
}

@ResponseBody
@RequestMapping
public String insert() {
return "GuestbookController:insert";
}
}

정리

타입+메소드 매핑이 가장 많이 쓰인다.!

Share