2013년 7월 25일 목요일

(130725) 24일차 web.xml 외 6개 (Spring을 이용한 쇼핑몰 만들기 - 상품목록 출력까지)

 - /WebContent/WEB-INF/web.xml 소스
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <servlet>
    <servlet-name>shoppingmall</servlet-name>
    <servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>shoppingmall</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- 한글 처리 -->
  <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>
  org.springframework.web.filter.CharacterEncodingFilter
  </filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>EUC-KR</param-value>
  </init-param>
  </filter>
  
  <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>


 - /WebContent/WEB-INF/shoppingmall-servlet.xml 소스
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <!-- Controller 작성 -->
  <!-- ItemList - Select * 실행 -->
  <bean id="itemlistcontroller" name="/ItemListController.do" class="controller.ItemListController"
  p:itemDao-ref="itemDao"
  >
  </bean>
 
  <bean id="itemDao" class="item.ItemDao"
  p:dataSource-ref="dataSource"
  >
</bean>
<!-- Controller 실행 후 결과를 result할 jsp 페이지를 설정 -->
<bean id = "internalResolver"
 class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name = "viewClass">
  <value>org.springframework.web.servlet.view.JstlView</value>  
 </property>
 <property name="prefix">
  <value>/jsp/</value>
 </property>
 <property name="suffix">
  <value>.jsp</value>
 </property>
</bean>
<!-- 데이터 베이스 객체를 사용할 수 있게 설정해주는 Datasource 선언 -->
<bean id = "dataSource"
 class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name = "driverClassName">
  <value>oracle.jdbc.driver.OracleDriver</value>
 </property>
 <property name="url">
  <value>jdbc:oracle:thin:@127.0.0.1:1521:XE</value>
 </property>
 <property name="username">
  <value>system</value>
 </property>
 <property name="password">
  <value>123456</value>
 </property>
</bean> 
</beans>


 - /src/item/ItemBean.java 소스
package item;

public class ItemBean {
private int item_id;
private String item_name;
private int price;
private String description;
private String picture_url;
 
public int getItem_id() {
return item_id;
}
public void setItem_id(int item_id) {
this.item_id = item_id;
}
public String getItem_name() {
return item_name;
}
public void setItem_name(String item_name) {
this.item_name = item_name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPicture_url() {
return picture_url;
}
public void setPicture_url(String picture_url) {
this.picture_url = picture_url;
}
}


 - /src/item/ItemDao.java 소스
package item;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class ItemDao {
// Statement 객체와 유사한 쿼리를 실행시켜주는 객체
SimpleJdbcTemplate template;

// shoppingmall-servlet.xml 파일에서 ItemDao가 DataSource 객체를 참조하게 선언
// 자동 매핑을 위해서 위 방식 보다는 아래 방식을 추천 (위 방식은 직접 받는 방식, 아래 방식은 인터페이스를 사용한 방식)
DataSource dataSource;

public void setDataSource(DataSource dataSource) {
template = new SimpleJdbcTemplate(dataSource);
}

// Item 게시판의 데이터를 보여주는 메소드 작성
public List<ItemBean> getAllItem(){
// 쿼리 준비
String sql = "select * from item";

// sql에 맞는 rowmap
// 멤버빈타입 속성과 item 테이블의 레코드, 컬럼명을 일치시켜주는 객체
RowMapper<ItemBean> rm = new BeanPropertyRowMapper<ItemBean>(ItemBean.class);

return template.query(sql, rm);
}
}


 - /WebContent/main.jsp 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title> 메인 </title>
</head>
<body>
<center>
<font face="휴먼아미체">
<h2><a href = "/ShoppingMall/ItemListController.do"> 시작 </a></h2>
</font>
</center>
</body>
</html>


 - /src/controller/ItemListController.java 소스 (Select* 문)
package controller;

import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import item.ItemBean;
import item.ItemDao;

public class ItemListController {
ItemDao itemDao;

public void setItemDao(ItemDao itemDao) {
this.itemDao = itemDao;
}

@RequestMapping("/ItemListController")
public ModelAndView listItem(){

// 데이터베이스에 접근해서 데이터를 받아옴
List<ItemBean> ibean = itemDao.getAllItem();

return new ModelAndView("ItemListView", "ibean", ibean);
}
}


 - /WebContent/jsp/ItemListView.jsp 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- 국제화 태그 -->
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title> 상 품 목 록 </title>
</head>
<body>
<center>
<font face="휴먼아미체">
<h2> 상 품 목 록 </h2>
  <form action = "/ShoppingMall/ItemWriteProcController.do" method = "post">
<table border = "0" width = "600" cellpadding="4" cellspacing="2">
<tr bgcolor = "FFFFFFF">
  <th width = "80"> 상품ID </th>
  <th width = "350"> 상품명 </th>
  <th width = "150"> 가격 </th>
</tr>
 
  <c:forEach var="ibean" items="${ibean}">
  <tr>
  <td width = "80" align="center"> ${ibean.item_id} </td>
<td width = "350"> <a href = "/ShoppingMall/ItemInfoController.do?item_id=${ibean.item_id}">${ibean.item_name}</a> </td>
  <td width = "150" align="center"> ${ibean.price}원 </td>
  </tr>
  </c:forEach>
    </table>

    <table border = "0" width = "600" cellpadding="4">
    <tr>
<td colspan = "5" align = "right"> <input type = "submit" value = "상풍등록"> </td>
</tr>
  </table>
  </form>
</font>
</center>
</body>
</html>


 - 결과