728x90
1. pom.xml
<!-- 오라클 연동 위해 -->
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc10 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc10</artifactId>
<version>19.7.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
2. Service 분리
-JDBCNoticeService.java
-JPANoticeService.java
-NoticeService.java (interface)
3. Controller
package com.newlecture.web.controller.notice;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.service.NoticeService;
public class ListController implements Controller {
private NoticeService noticeService;
public void setNoticeService(NoticeService noticeService) {
this.noticeService = noticeService;
System.out.println("Hello");
}
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
/* ModelAndView mv = new ModelAndView("notice/list"); */
/* tiles.xml위해 */
ModelAndView mv = new ModelAndView("notice.list");
List<Notice> list = noticeService.getList(1, "TITLE", ""); /*int page, String field, String query*/
mv.addObject("list",list);
return mv;
}
}
4. dispatcher-servlet.xml
IOC Conrainer에 담겨진다.
<!-- listController에 property 추가 -->
<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" />
<!-- listController의 name=setter명(set생략,소문자시작), ref=매개변수명 -->
<!-- 인터페이스명으로 바뀔 수 있다 -->
</bean>
<bean id="/notice/detail" class="com.newlecture.web.controller.notice.DetailController"/>
<bean id="noticeService" class="com.newlecture.web.service.jdbc.JDBCNoticeService" >
<property name="dataSource" ref="dataSource"></property>
</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:@localhost:1521/xepdb1"/>
<property name="username" value="NEWLEC"/>
<property name="password" value="11111"/>
</bean>
5. view (list.jsp)
<!-- listController에서 담은 list -->
<c:forEach var="n" items="${list }">
<tr>
<td>${n.id}</td>
<td class="title indent text-align-left"><a href="detail">${n.title }</a></td>
<td>${n.writerId }</td>
<td>
${n.regDate }
</td>
<td>${n.hit }</td>
</tr>
</c:forEach>
728x90
'SPRING' 카테고리의 다른 글
[Spring] Annotation 어노테이션 설정 (0) | 2022.05.11 |
---|---|
[Spring] 서블릿 분리 & 환경설정 (0) | 2022.05.11 |
[Spring] tiles ViewResolver 설정하기 (0) | 2022.05.11 |
[Spring] tiles 지시서 작성하기2 (와일드카드이용) (0) | 2022.05.11 |
[Spring] tiles 지시서 작성하기1 (Eclipse) (0) | 2022.05.11 |