์ด์ ํฌ์คํ ์์๋ ์์ฑ๋ ๊ฐ์ฒด์ DI๋ง ์ด๋ ธํ ์ด์ ์ผ๋ก ์ฒ๋ฆฌํ๋ค๋ฉด, ์ด๋ฒ์๋ ๊ฐ์ฒด ์์ฑ๊น์ง ์ด๋ ธํ ์ด์ ์ผ๋ก ์ฒ๋ฆฌํด ๋ณด๋๋ก ํ๊ฒ ๋ค.
service-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- <context:annotation-config /> --> <!-- ์์ฑํ ๊ฐ์ฒด๋ฅผ ์ด๋
ธํ
์ด์
(autowired)์ ํตํด set ํด๋ฌ๋ผ๋ ํ๊ทธ -->
<context:component-scan base-package="com.newlecture.web.service"></context:component-scan>
<!-- base-package ์์์ @๊ฐ ๋ถ์ ํด๋์ค๋ฅผ ์ฐพ์ ๊ฐ์ฒด ์ธ์คํด์ค๋ฅผ ์์ฑํด ๋ฌ๋ผ๋ ํ๊ทธ
์ปดํฌ๋ํธ ์ค์บํ๊ฒ ๋๋ฉด @Autowired๋ ์ฐพ๊ธฐ ๋๋ฌธ์
DI์ ์ฌ์ฉํ๋ annotation-config ํ๊ทธ๋ ์ญ์ ํด๋ ๋๋ค.
-->
<!--
<bean id="noticeService" class="com.newlecture.web.service.jdbc.JDBCNoticeService">
<property name="dataSource" ref="dataSource"/>
</bean>
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@14.35.198.58:1522/orcl" />
<property name="username" value="USER01" />
<property name="password" value="1111" />
</bean>
</beans>
<context:component-scan> ํ๊ทธ๋ฅผ ์ถ๊ฐํ๋ค. base-package ์์ ์๋ ํด๋์ค ์ค์์ @๊ฐ ๋ถ์ ํ์ผ์ ์ฐพ์ ๊ฐ์ฒด ์ธ์คํด์ค๋ฅผ ์์ฑํด ๋ฌ๋ผ๋ ํ๊ทธ์ด๋ค. ์ปดํฌ๋ํธ ์ค์บ์ ํ๊ฒ ๋๋ฉด ์์ฐํ ๊ทธ ์์์ @Autowired๊ฐ ๋ถ์ ๋ฉ์๋๋ฅผ ์ฐพ๊ธฐ ๋๋ฌธ์ <context:annotation-config/> ํ๊ทธ๋ ์ง์๋ ๋๋ค. ์ด๋ ธํ ์ด์ ์ปจํผ๊ทธ๋ณด๋ค ์์ ํ๊ทธ๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค.
JDBCNoticeService.java
package com.newlecture.web.service.jdbc;
//๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ IoC ์ปจํ
์ด๋์ ๋ด๋ ์ปดํฌ๋ํธ ์ด๋
ธํ
์ด์
//@Component //@Controller, @Service, @Repository
@Service
public class JDBCNoticeService implements NoticeService{
@Autowired
private DataSource dataSource;
@Component ์ด๋ ธํ ์ด์ ์ ๋ถ์ด๋ฉด ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ค. ํ์ง๋ง ์ง๊ธ์ MVC ํจํด์ผ๋ก ๊ฐ๋ฐ์ ํ๊ณ ์๊ณ , MVC์์์ ์ญํ ์ ๊ฐ์กฐํ๊ธฐ ์ํด์๋ @Service ์ด๋ ธํ ์ด์ ์ ๋ถ์ด๋ ๊ฒ์ด ๋ฐ๋์งํ๋ค.
์ด์ servlet-context์ IndexController๋ ์์ ํด์ผ ํ๋ค. ํ์ง๋ง ์์ ์์ ํ ๋ฐฉ๋ฒ๊ณผ๋ ๋ค๋ฅด๋ค. ์ฐ์ servlet-context.xml ํ์ผ์ IndexController ๋น ์์ฑ ํ๊ทธ๋ฅผ ์ดํด๋ณด๋๋ก ํ๊ฒ ๋ค.
<bean id="/index" class="com.newlecture.web.controller.IndexController" />
servlet-context์์๋ ์์ฒญ url์ธ "/index"๊ฐ ๋น์ id์ด๋ค. @Service ์ด๋ ธํ ์ด์ ๋ง์ผ๋ก ๊ฐ์ฒด ์์ฑ์ ํ๋ JDBCNoticeService ๊ฐ์ฒด์๋ ์ฑ์ง์ด ๋ค๋ฅด๋ค.
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- <context:annotation-config /> -->
<context:component-scan base-package="com.newlecture.web.controller"></context:component-scan>
<!--
<bean id="/index" class="com.newlecture.web.controller.IndexController" />
-->
<bean id="/notice/list" class="com.newlecture.web.controller.notice.ListController">
<!--
<property name="noticeService" ref="noticeService" />
-->
</bean>
<bean id="/notice/detail" class="com.newlecture.web.controller.notice.DetailController" />
<!-- Tiles ViewResolver -->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions" value="/WEB-INF/tiles.xml" />
</bean>
<!-- View Resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
<property name="order" value="2" />
</bean>
<mvc:resources location="/static/" mapping="/**"></mvc:resources>
<mvc:annotation-driven />
</beans>
service-context.xml๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก annotation-config ํ๊ทธ๋ฅผ ์ง์ฐ๊ณ , component-scan ํ๊ทธ๋ฅผ ์ถ๊ฐํ๋ค. <bean id="/index"...> ํ๊ทธ๋ฅผ ์ง์ด๋ค. <mvc:annotation-driven /> ํ๊ทธ๋ฅผ ์ถ๊ฐํ๋ค.
IndexController.java
package com.newlecture.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController /*implements Controller*/{
@RequestMapping("/index")
public void aaaa() {
System.out.println("hihi");
}
/*
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// System.out.println("index controller");
ModelAndView mv = new ModelAndView("root.index"); //๋ทฐ ๋ฆฌ์กธ๋ฒ๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ํ๊ธฐ ๋๋ฌธ์ ์์ /WEB-INF/view/์ ๋ค์ .jsp๋ฅผ ์ ์จ๋ ๋๋ค.
mv.addObject("data", "Hello Spring MVC"); //view์์ ์ถ๋ ฅํ data๋ฅผ ๋ด๋๋ค.
//mv.setViewName("/WEB-INF/view/index.jsp");
return mv;
}
*/
}
/index ๊ฒฝ๋ก๋ฅผ ๋ฐ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ์์ฑํ๊ธฐ ์ํด์๋ Controller๋ผ๋ ์ธํฐํ์ด์ค๋ฅผ ์์ํ์ฌ handleRequest๋ฅผ ์ค๋ฒ๋ผ์ด๋ํ๊ณ ModelAndView ๊ฐ์ฒด๋ฅผ ํตํด ๋ทฐ ํ์ด์ง๋ฅผ ์ธํ ํด์ผ ํ๋ค.
ํ์ง๋ง @Controller ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ๋ฉด ๊ณผ์ ์ด ํ๊ฒฐ ๊ฐ๋จํด์ง๋ค.
์ฐ์ implements Controller๋ ๋์ด์ ํ์ง ์์๋ ๋๋ฉฐ, ๊ทธ์ ๋ฐ๋ฅธ handleRequest ๋ฉ์๋ ์ญ์ ๋ถํ์ํด์ง๋ค. void ํ์ ์ผ๋ก ๋ฉ์๋๋ฅผ ์์ฑํ๊ณ (๋ฉ์๋์ ์ด๋ฆ์ ๊ด๊ณ์๋ค) @RequestMapping์ผ๋ก url ์ฃผ์๋ฅผ ์ธํ ํ๋ค. ์ด๋ ๊ฒ @Controller ์ด๋ ธํ ์ด์ ์ฌ์ฉ๊ณผ ๊ทธ์ ๋ฐ๋ผ @RequestMapping ์ด๋ ธํ ์ด์ ์ค์ ์ ์ํด xml์์ <mvc:annotation-driven /> ํ๊ทธ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ์คํํ๋ฉด ์๋์ ๊ฐ์ ํ๋ฉด์ด ๋ฌ๋ค.
tiles ์ธํ ์ด ๋์ง ์์ index.jsp ํ์ด์ง๊ฐ ๋จ๊ณ , @RequestMapping("/index") ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ๋ฉ์๋์์ ์คํํ๋ Sysout ์ฝ์ ์ถ๋ ฅ๋ ์ ๋๋ค.
ModelAndView์ ๊ฐ์ด ๋ทฐ ํ์ด์ง ์ค์ ์ ๋ฐ๋ก ํ์ง ์์๋ ๋งคํ ๊ฒฝ๋ก์ ๋ฐ๋ผ ๋ทฐ ํ์ด์ง ์ค์ ์ ํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ๋ง์ฝ ๋งคํ์ด "/aaa"์ผ๋ก ๋์ด์์๋ค๋ฉด "/WEB-INF/view/aaa.jsp"๋ฅผ ์ฐพ์ ๊ฒ์ด๊ณ , ๊ทธ ๊ฒฝ๋ก์ ํ์ด์ง๊ฐ ์๋ค๋ฉด 404 ์๋ฌ๊ฐ ๋จ๊ฒ ๋๋ค.
์ฐ์ ์์ 1์ธ tiles ์ค์ ์ด ์ ๋๋ ์ฐ์ ์์ 2์ธ ๋ทฐ ๋ฆฌ์กธ๋ฒ ์ค์ ์ผ๋ก ๊ฐ์ prefix, suffix๋ฅผ ๊ฑฐ์ณ index.jsp ํ์ด์ง๋ฅผ ๋์ด๋ค. (tiles.xml์ definition name์ด root.index์ผ๋ก ์ค์ ๋์ด ์์ด์ tiles ์ค์ ์ด ์ ๋จนํ)
* HomeController ๋ง๋ค๊ธฐ
IndexController.java
@Controller
public class IndexController {
@RequestMapping("/index")
public void aaaa() {
System.out.println("hihi");
}
@RequestMapping("/help")
public void bbbb() {
System.out.println("hello");
}
@Controller ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ์ฌ ์์ผ๋ก๋ ํ ์ปจํธ๋กค๋ฌ ํ์ผ ์์ ์ฌ๋ฌ ๊ฐ์ ๋ฉ์๋๊ฐ ์์ ์ ์๊ณ , ๊ฐ๊ฐ์ ๋ฉ์๋๋ง๋ค ๋งคํ์ ํ ์ ์๋ค. ์์ ์์๋ฅผ ์ดํด๋ณด๋ฉด "/index", "/help" ๋ ๊ฐ์ ์์ฒญ์ด ์๊ธฐ ๋๋ฌธ์ ํ์ผ ์ด๋ฆ์ด IndexController์ธ ๊ฒ์ ์ ์ ํ์ง ์์ ๋ณด์ธ๋ค. ๋๋ ํ ๋ฆฌ์ ํด๋๋ช ๋ฑ์ผ๋ก ์ปจํธ๋กค๋ฌ ํ์ผ ์ด๋ฆ์ ์ง์ ์ ์์ ๊ฒ์ด๋ฉฐ ํ์ฌ "/index" ์์ฒญ์ ๋ฐ๋ ๋ทฐ ํ์ผ์ธ index.jsp ํ์ผ์ webapp/WEB-INF/view/index.jsp์ ๊ฒฝ๋ก์ ์์ผ๋ฏ๋ก RootController, HomeController ๋ฑ์ผ๋ก ๋ช ๋ช ํ ์ ์๋ค. HomeController์ผ๋ก ์ด๋ฆ์ ๋ฐ๊พธ๋๋ก ํ๋ค.
HomeController.java
@Controller
public class HomeController{
@RequestMapping("/index")
public String index() { //๋ฉ์๋ ์ด๋ฆ์ ๋งคํ๋ช
๊ณผ ๋์ผํ๋๋ก ์์
return "root.index";
}
/*
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView("root.index");
mv.addObject("data", "Hello Spring MVC"); //view์์ ์ถ๋ ฅํ data๋ฅผ ๋ด๋๋ค.
return mv;
}
*/
}
ModelAndView ๊ฐ์ฒด์ "root.index"๋ฅผ ์ค์ ํ์ฌ ๋ฐํํ๋ handleRequest ๋ฉ์๋์ ๋ฌ๋ฆฌ String ํ์์ผ๋ก "root.index"๋ฅผ ๋ฐํํ๋ฉด tiles ์ค์ ์ญ์ ์ ์ฉ๋๋ค.
* Notice ์ปจํธ๋กค๋ฌ ์ ๋ฆฌํ๊ธฐ
์ปจํธ๋กค๋ฌ๋ view ํ์ผ์ ๊ธฐ์ค์ผ๋ก ์์ฑํ๋ฉด ๋๋ค.
๋ค์๊ณผ ๊ฐ์ ๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ์ผ ๋๋ EventController, FaqController, NoticeController ํด๋์ค๋ฅผ ์์ฑํ๊ณ , NoticeController ํด๋์ ์์ detail, list ๋ฉ์๋๋ฅผ ๋ง๋ค๋ฉด ๋๋ค. event, faq, inc, notice๋ฅผ ํฌํจํ๊ณ ์๋ customer๋ ํจํค์ง๋ก ์์ฑํ๋ฉด ๋๋ค.
IndexController๋ฅผ HomeController์ผ๋ก ์ด๋ฆ ๋ณ๊ฒฝํ๊ณ ๋ฉ์๋๋ฅผ ์์ ํ ๊ฒ๊ณผ ๊ฐ์ด notice ๊ด๋ จ ์ปจํธ๋กค๋ฌ๋ ์์ ํ๋๋ก ํ๋ค.
NoticeController.java
package com.newlecture.web.controller.customer;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.service.NoticeService;
@Controller
public class NoticeController{
@Autowired
private NoticeService noticeService;
//url ๊ตฌ์กฐ๋ ํ์ผ ๊ตฌ์กฐ์ ๊ฐ๊ฒ ํด์ผ ํ์ผ ์ฐพ์๊ฐ๊ธฐ ์ฝ๋ค.
@RequestMapping("/customer/notice/list")
public String list() throws ClassNotFoundException, SQLException {
List<Notice> list = noticeService.getList(1, "TITLE", "");
return "notice.list";
}
@RequestMapping("/customer/notice/detail")
public String detail() {
return "notice.detail";
}
}
servlet-context.xml
<!--
<bean id="/index" class="com.newlecture.web.controller.IndexController" />
-->
<!--
<bean id="/notice/list" class="com.newlecture.web.controller.notice.ListController">
<property name="noticeService" ref="noticeService" />
</bean>
-->
<!--
<bean id="/notice/detail" class="com.newlecture.web.controller.notice.DetailController" />
-->
header.jsp
<li><a href="/customer/notice/list"><img src="/images/txt-customer.png" alt="๊ณ ๊ฐ์ผํฐ" /></a></li>
๊ธฐ์กด์ ListController, DetailController์์ ๊ฐ๊ฐ ์์๋ ๋ด์ฉ์ NoticeController ์์ ๋ฉ์๋๋ก ๋ง๋ค์ด์ ํฉ์น๋ค. "/notice/list"์๋ ๋งคํ์ "/customer/notice/list"์ผ๋ก ๋ณ๊ฒฝํ ์ด์ ๋, ํ์ผ ๊ตฌ์กฐ์ url ๊ตฌ์กฐ๊ฐ ๋์ผํด์ผ ๋๋ ํ ๋ฆฌ์์ ํ์ผ์ ์ฐพ๊ธฐ๊ฐ ์ฝ๊ธฐ ๋๋ฌธ์ด๋ค. header.jsp์์ ๊ณ ๊ฐ์ผํฐ a ํ๊ทธ์ url์ ์ญ์ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ ํ๋ค.
tiles์์ ์ด๋ฆ์ ์ฐพ๋ ๊ธฐ์ค์ผ๋ก String ๊ฐ์ ๋ฆฌํดํ๊ฒ ํ๋ค. ("notice.list"์ "notice.detail")
@Controller์ผ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ @Autowired์ผ๋ก ์์กด์ฑ์ ์ฃผ์ ํ๋ฏ๋ก xml์์์ bean ์ค์ ์ ๋์ด์ ํ์์น ์๋ค. ์ญ์ ๋๋ ์ฃผ์์ฒ๋ฆฌ ํ๋ค.
์ด๋ ๊ฒ ๋๋ฉด ์ฌ๋ฌ ๊ฐ์๋ ์ปจํธ๋กค๋ฌ ์๋ฐ ํ์ผ์ด ํ๋๋ก ์ถ์๋์ด ํจ์ฌ ๊ด๋ฆฌํ๊ธฐ ํธํ๋ค.
์ง๊ธ ์ด ์ํ๋ก๋ ์คํ์ ์ ํ ๋ฌธ์ ๋ ์์ง๋ง ํ ๊ฐ์ง ๋ ์์ ํ๋ฉด ์ข์ ๊ฒ์ด ์๋ค. ์ปจํธ๋กค๋ฌ์์ url์ด ๊ณตํต๋๋ ๋ถ๋ถ์ ๋ฐ๋ก ๋ผ์ด๋ผ ์ ์๋ ๋ฐฉ๋ฒ์ด ์๋ค.
NoticeController.java
package com.newlecture.web.controller.customer;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.service.NoticeService;
@Controller
@RequestMapping("/customer/notice/") //ํด๋์ค์ ๋งคํ ์ด๋
ธํ
์ด์
์ ๋ถ์ธ๋ค.
public class NoticeController{
@Autowired
private NoticeService noticeService;
@RequestMapping("list")
//์์ "/customer/notice/"์ ์ด์ด์ ธ์ ๋งคํ๋๋ค.
public String list() throws ClassNotFoundException, SQLException {
List<Notice> list = noticeService.getList(1, "TITLE", "");
return "notice.list";
}
@RequestMapping("detail")
public String detail() {
return "notice.detail";
}
}
list ๋ฉ์๋์ ๋งคํ์ "/customer/notice/list"์ด๊ณ detail ๋ฉ์๋์ ๋งคํ์ "/customer/notice/detail"์ผ๋ก "/customer/notice/" ๋ผ๋ ๊ณตํต ๋ถ๋ถ์ด ์๋ค. ๊ทธ ๋ถ๋ถ์ ๋ผ์ด์ ํด๋์ค๋ช ์ ๋งคํํ๊ณ ๋ฉ์๋์๋ list, detail๋ง ๋จ๊ฒจ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด url ๋งคํ๋ ๋ ํด๋์ค์ ๋ถ์ "/customer/notice/"๊ณผ ์ด์ด์ ธ์ ๋งคํ์ด ๋๋ค.
๋ง์ฐฌ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก HomeController๋ ์์ ํ๋ค.
HomeController.java
@Controller
@RequestMapping("/")
public class HomeController{
@RequestMapping("index")
public String index() {
return "root.index";
}
}
ํด๋์ค์ "/"์ผ๋ก ๋งคํํ๊ณ ๋ฉ์๋์ "index"์ผ๋ก ๋งคํํ๋ฉด, ๋์ด ์ด์ด์ ธ์ "/index"์ผ๋ก ๋งคํ ๋๋ค.
'๐ปStudy > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฌธ์ ์ถ๋ ฅ ๋ฐฉ๋ฒ 4๊ฐ์ง, @ResponseBody (0) | 2021.03.03 |
---|---|
์ปจํธ๋กค๋ฌ๋ฅผ ์ํ ์ด๋ ธํ ์ด์ ๊ฐ๋ ์ ๋ฆฌ (0) | 2021.03.03 |
๊ฐ์ฒด DI๋ฅผ ์ด๋ ธํ ์ด์ ์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ (0) | 2021.03.02 |
๋ฐ์ดํฐ ์๋น์ค ํด๋์ค ์ถ๊ฐ / ์๋น์ค ๊ฒฐํฉ๋ ฅ ๋ฎ์ถ๊ธฐ / DB ์ฐ๊ฒฐ์ ๋ณด ๋ถ๋ฆฌ / Spring ์ค์ ๋ถ๋ฆฌ (0) | 2021.02.26 |
์ ์ ํ์ผ ๋งคํ / Tiles์ TilesResolver (0) | 2021.02.26 |