2014년 8월 18일 월요일

[오라클자바커뮤니티]Spring4 MVC(쇼핑카트,장바구니예제,@SessionAttributes, @ModelAttribute, 스프링으로 구현한 미니장바구니,쇼핑카트)

[오라클자바커뮤니티]Spring4 MVC(쇼핑카트,장바구니예제,@SessionAttributes, @ModelAttribute, 스프링으로 구현한 미니장바구니,쇼핑카트)

1. Spring MVC로 spring4cart라고 프로젝트 하나 만들자. 전체 프로젝트 구성은 아래 그림과 같다.




2. spring4 mvc 템플릿에서 필요한 파일만 만들자.

pom.xml은 다음과 같다.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.samples.service.service</groupId>
  <artifactId>spring4rest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
    <properties>

<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>2.5</servlet.version>

<!-- Spring -->
<spring-framework.version>4.0.2.RELEASE</spring-framework.version>

<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>

<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>

<!-- Test -->
<junit.version>4.11</junit.version>

</properties>
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Other Web dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>

<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>

<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>

<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>




3. 쇼핑몰에 담길 상품(Goods.java)

package onj.edu.spring4cart;

public class Goods {
private String name;  //상품명
private int cnt;    //개수
private int price;  //가격
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCnt() {
return cnt;
}
public void setCnt(int cnt) {
this.cnt = cnt;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}



4. 컨트롤러

package onj.edu.spring4cart;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("/")
@SessionAttributes({"cart"})
public class HomeController {
@RequestMapping(method = RequestMethod.GET)
public String getCart(Model model) {
if (!model.containsAttribute("cart")) {
model.addAttribute("cart", new ArrayList<Goods>());
}
return "home";
}
@RequestMapping(value="add", method = RequestMethod.POST)
public String add(@ModelAttribute Goods goods,
         @ModelAttribute("cart") List<Goods> cart) {
cart.add(goods);
return "redirect:/";
}
}



5. home.jsp


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<title>OracleJava Community Spring4 MVC Cart</title>
</head>
<body>
<h1>
OracleJava Community Spring4 MVC Cart
</h1>
<c:forEach items="${cart}" var="goods">
Name : ${goods.name}, Price : ${goods.price}, Cnt : ${goods.cnt }  
<br/>
</c:forEach>
<br><br>
<h1>Add Cart...</h1>
<form method="post" action="add">
Name  : <input type="text" name="name"><br>
Price : <input type="text" name="price"><br>
Cnt   : <input type="text" name="cnt"> <br><br>
<button>Add Goods...</button>
</form>
</body>
</html>


6. 실행
http://localhost:8080/spring4cart/


댓글 없음:

댓글 쓰기