์ ๋ ฅ์ ์ข ๋ฅ / QueryString ์ ๋ ฅ
์ง๊ธ๊น์ง๋ ์คํ๋ง MVC๋ฅผ ์ด์ฉํ ๋งคํ, ์ถ๋ ฅ์ ์์๋ณด์๋ค. ์ด์ ๋ถํฐ๋ ์ฌ์ฉ์ ์ ๋ ฅ์ ๋ํด ์์๋ณด๋๋ก ํ๊ฒ ๋ค.
* QueryString ์ ๋ ฅ
์ปจํธ๋กค๋ฌ์์ ์ ๋ ฅ ๋๊ตฌ์ธ HttpServletRequest๋ฅผ ์ป์ด์์ getParameter ๋ฉ์๋๋ฅผ ์ด์ฉํด ๊ฐ์ ์ ๋ ฅ๋ฐ์ ์ ์๋ค. ํ์ง๋ง ์ด ๋ฐฉ๋ฒ์ ์ด์ ์ ์ฌ์ฉํ๋ ์๋ธ๋ฆฟ์์์ ๋ฐฉ๋ฒ๊ณผ ํฌ๊ฒ ๋ค๋ฅด์ง ์๋ค. ์คํ๋ง์์ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๋ ์กฐ๊ธ ๋ ๋์ ๋ฐฉ๋ฒ ์ญ์ ์ ๊ณตํ๋ค. ์ ๋ ฅ ๋๊ตฌ๊ฐ ์๋ ์ ๋ ฅ ๊ฐ์ ์ป์ด์ค๋ ๋ฐฉ๋ฒ์ด๋ค.
์ฌ์ฉ์๊ฐ "p=1" ์ด๋ผ๋ ์ต์ ์ผ๋ก ์์ฒญ์ ๋ณด๋์ ๋ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ์์ ์์ฒญ์ ๋ฐ์ ํด๋นํ๋ ์ปจํธ๋กค๋ฌ์๋ "1"์ด๋ผ๋ ๊ฐ์ ๋๊ฒจ์ฃผ๊ฒ ๋๋ค. ๊ทธ๋ฌ๋ฉด ์ปจํธ๋กค๋ฌ๋ "p"๋ผ๋ ๋ณ์๋ก ๊ฐ์ ์ป์ด์์ ์ฌ์ฉํ ์ ์๋ค.
* HttpServletRequest ์ฌ์ฉํ๊ธฐ
NoticeController.java (com.newlecture.web.controller.customer)
@Controller
@RequestMapping("/customer/notice/")
public class NoticeController{
@Autowired
private NoticeService noticeService;
@RequestMapping("list")
public String list(HttpServletRequest request) throws ClassNotFoundException, SQLException {
String p = request.getParameter("p");
System.out.println(p);
List<Notice> list = noticeService.getList(1, "TITLE", "");
return "notice.list";
}
}
"localhost:8080/customer/notice/list?p=1" ์ ์คํํ๋ฉด request ๊ฐ์ฒด๋ฅผ ์ป์ด์ ์ฝ์์ฐฝ์ "1"์ด ์ถ๋ ฅ๋๋ค.
* ์ ๋ ฅ ๊ฐ๋ง ์ป์ด์ ์ฌ์ฉํ๊ธฐ
NoticeController.java
@Controller
@RequestMapping("/customer/notice/")
public class NoticeController{
@Autowired
private NoticeService noticeService;
@RequestMapping("list")
public String list(String p) throws ClassNotFoundException, SQLException {
System.out.println(p);
List<Notice> list = noticeService.getList(1, "TITLE", "");
return "notice.list";
}
"localhost:8080/customer/notice/list?p=5" ์ ์คํํ๋ฉด ์ฝ์์ฐฝ์ "5"๊ฐ ์ถ๋ ฅ๋๋ค. url์์ "p=5"๋ผ๊ณ ์ต์ ์ ๋ฌ์ ์์ฒญ์ ๋ณด๋๊ณ , ์ปจํธ๋กค๋ฌ์์๋ ๋ณ์๋ช ๋ง ๋ง์ถฐ์ String์ ๋ฐ์์ค๋ฉด ์คํ๋ง์ด ์์์ url์ ์ฟผ๋ฆฌ์คํธ๋ง ๊ฐ์ ๋ฐ์์จ๋ค.
* QueryString ๋ณ์๋ช ๊ณผ ๊ธฐ๋ณธ ๊ฐ ์ฒ๋ฆฌ
์์ ์์์ฒ๋ผ url์ ์ต์ ์ผ๋ก ๊ฐ์ ๋ณด๋ด๋ฉด์ ์์ฒญํ๋ฉด ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๋ ๊ทธ๊ฒ์ ๋ฐ์ ๊ฐ์ ๋ณ์๋ช (์ฌ๊ธฐ์๋ p)์ผ๋ก ์ปจํธ๋กค๋ฌ์ ๊ฐ์ ๋ณด๋ด๊ณ , ์ปจํธ๋กค๋ฌ๋ ๊ทธ ๋ณ์๋ช ์ผ๋ก String์ ๋ฐ์ ๊ฐ์ ์ฌ์ฉํ ์ ์๋ค. ์ด๋ ์ปจํธ๋กค๋ฌ์์ ๋ค๋ฅธ ๋ณ์๋ช ์ผ๋ก ๊ฐ์ ๋ฐ์์ค๊ณ ์ถ์ ๋ ์ฐ๋ ๋ฐฉ๋ฒ์ด ๋ฐ๋ก @RequestParam์ด๋ค.
url์ ์์ฒญ๋๋ก "p"๋ผ๋ ๋ณ์์ ๊ทธ์ ํด๋นํ๋ ๊ฐ์ ๋ณด๋ด๋๋ฐ, ์ปจํธ๋กค๋ฌ์์๋ "page"๋ผ๋ ์ด๋ฆ์ผ๋ก ๊ทธ๊ฒ์ ๋ฐ๊ฒ ๋ค๋ ์ ์ธ์ด๋ค.
๋ง์ฝ ์ฌ์ฉ์๊ฐ p๋ผ๋ ์ต์ ์์ด url ์์ฒญ์ ํ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
์ด๋ฌํ ์ํฉ์ ๋ฐฉ์งํ๊ธฐ ์ํด์ p ๊ฐ์ด ์์ ๋๋ ๊ธฐ๋ณธ๊ฐ์ ์ง์ ํด ์ฃผ๋ ๊ฒ์ด ์ข๋ค. ์คํ๋ง์์๋ ๊ฐ์ ๋ฐ์์ฌ ๋ defaultValue, name, required, value์ ๊ฐ์ ๋ช ๊ฐ์ง ์ต์ ์ ์ ๊ณตํ๋ค.
NoticeController.java
@Controller
@RequestMapping("/customer/notice/")
public class NoticeController{
@Autowired
private NoticeService noticeService;
@RequestMapping("list")
public String list(@RequestParam(name="p", defaultValue="1") int page) throws ClassNotFoundException, SQLException {
/*
* ์
๋ ฅ ๊ฐ ์ป์ด์ค๋ ๋ฐฉ๋ฒ *
1. HttpServletRequest ์
๋ ฅ ๋๊ตฌ ๊ฐ์ฒด๋ฅผ ์ป์ด์์
String p = request.getParameter("p"); ๋ฐฉ์์ผ๋ก ๋ฐ๋๋ค.
2. String p ๋ผ๋ ๊ฐ์ผ๋ก ๋ฐ์์จ๋ค.
url์์ ๋ณด๋ธ ๋ณ์๋ช
๋๋ก ๋ฐ์์ฌ ๋ ์ฌ์ฉํ๋ค.
3. @RequestParam("p") String page ๋ผ๋ ๊ฐ์ผ๋ก ๋ฐ์์จ๋ค.
p๋ผ๋ ๋ณ์๋ก ๋ณด๋ด๊ณ page๋ผ๋ ๋ณ์๋ก ๋ฐ๋๋ค.
*/
System.out.println("page : " + page);
List<Notice> list = noticeService.getList(1, "TITLE", "");
return "notice.list";
}
}
name์ผ๋ก ๋ฐ์์ฌ ์ฟผ๋ฆฌ์คํธ๋ง์ ์ง์ , defaultValue๋ก ๋ฐ์์ฌ ๊ฐ์ ๊ธฐ๋ณธ๊ฐ ์ง์ ์ ํ์๋ค. ์ฟผ๋ฆฌ์คํธ๋ง์ ์ผ๋ฐ์ ์ผ๋ก String์ผ๋ก ๋ฐ์์์ ๋ค๋ฅธ ํ์ ์ผ๋ก ํ ๋ณํ์ ํด์ผํ์ง๋ง(ParseInt ๋ฑ) ์คํ๋ง์ ํ ๋ณํํ์ง ์๊ณ ๋ ๊ฐ์ ๋ฐ์์ฌ ๋ ๊ฐ ํ์ ์ ์ง์ ํ๋ฉด ๊ทธ ํ์ ์ผ๋ก ๋ฐ์์ฌ ์ ์๊ฒ ํ๋ ๊ธฐ๋ฅ ์ญ์ ์ง์ํ๋ค.
* @RequestParam์ required ์์ฑ
NoticeController.java
@Controller
@RequestMapping("/customer/notice/")
public class NoticeController{
@Autowired
private NoticeService noticeService;
//url ๊ตฌ์กฐ๋ ํ์ผ ๊ตฌ์กฐ์ ๊ฐ๊ฒ ํด์ผ ํ์ผ ์ฐพ์๊ฐ๊ธฐ ์ฝ๋ค.
@RequestMapping("list")
public String list(@RequestParam(name="p", required=false) Integer page) throws ClassNotFoundException, SQLException {
//defaultValue ๋์ required=false ์ฌ์ฉ, Integer ํ์
์ผ๋ก ๋ฐ์์จ๋ค.
System.out.println("page : " + page);
List<Notice> list = noticeService.getList(1, "TITLE", "");
return "notice.list";
}
}
required ์ต์ ์ ํด๋น ํ๋ผ๋ฏธํฐ๊ฐ ํ์์ธ์ง์ ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ฉฐ, ๊ธฐ๋ณธ๊ฐ์ true์ด๋ค.
"@RequestParam(name="p", required=false) Integer page"์ ์๋ฏธ๋ ํ๋ผ๋ฏธํฐ p๊ฐ ์กด์ฌํ์ง ์์์ ๋ Integer page๋ฅผ null๋ก ๋ฐ๋๋ค๋ ๋ป์ด๋ค. ๊ธฐ๋ณธํ์ ์ธ int๋ null์ ๋ฐ์ ์ ์์ผ๋ฏ๋ก page ํ๋ผ๋ฏธํฐ์ ํ์ ์ Integer์ผ๋ก ์ ์ธํ์๋ค.
* @RequestParam์ value ์์ฑ
value ์์ฑ์ name์ ๋์ ํ ์ ์๋, name์ ๋ณ์นญ์ด๋ค. ๋์ด ๋น์ทํ์ง๋ง name๊ณผ value๋ฅผ ๋ ๋ค ์ฌ์ฉํด์๋ ์ ๋๋ค. ๋์ด ๊ฐ์ผ๋ ์๋ฌด๊ฑฐ๋ ์ฐ์.