SlideShare a Scribd company logo
스프링프레임워크 & 마이바티스
(Spring Framework, MyBatis)
6-18. Spring WEB MVC쇼핑카트(@SessionAttibutes,
@ModelAttribute)
 Spring WEB MVC를 이용하여 간단하게 장바구니를 구현하자.
 장바구니에 담길 상품항목은 name, price, cnt 정도로 하자
1. STS에서
File  New  Spring Legacy Project
Project Name : spring4cart
Spring MVC Project 선택
다음 화면의 top level package에서 a.b.spring4cart 입력
전체 프로젝트 구성은 아래 그림과 같다.
2. 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.2.4.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. /WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-
value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4. /WEB-INF/spring/appServlet/servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static
resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-
INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="onj.edu.spring4cart" />
</beans:beans>
5. 쇼핑몰에 담길 상품(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;
}
}
6. 컨트롤러[HomeController.java]
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:/";
}
}
7. /WEB-INF/views/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>
8. 실행
http://localhost:8080/spring4cart/
#31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원교육, 국비지원교육, 구로IT교육학원, IT실무교육, 오라클자바커뮤니티

More Related Content

PDF
#30.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PDF
(국비지원학원/재직자교육/실업자교육/IT실무교육_탑크리에듀)#4.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
PDF
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
PDF
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
PDF
#11.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원학원,재직자/실업자교육학원,스프링교육,마이바...
PDF
#29.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PDF
#34.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자교육,국...
PDF
Filling the flask
#30.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
(국비지원학원/재직자교육/실업자교육/IT실무교육_탑크리에듀)#4.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
#11.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원학원,재직자/실업자교육학원,스프링교육,마이바...
#29.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
#34.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자교육,국...
Filling the flask

What's hot (20)

PDF
Kyiv.py #17 Flask talk
PPT
Jsp/Servlet
KEY
CodeIgniter 3.0
PDF
Practical Protocol-Oriented-Programming
ODP
Running ms sql stored procedures in mule
ODP
Spring 4 advanced final_xtr_presentation
PDF
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
PPTX
Integration of Backbone.js with Spring 3.1
PPTX
Jsp presentation
PDF
Design patterns in Magento
PPTX
Implicit object.pptx
ODP
Spring 4 final xtr_presentation
PDF
OpenDMS - the first 2 weeks
PPT
JavaScript
PPTX
Spring Framework Petclinic sample application
ODP
Different Types of Containers in Spring
ODT
Types of Dependency Injection in Spring
PDF
TurboGears2 Pluggable Applications
PDF
Demystifying Drupal AJAX Callback Commands
Kyiv.py #17 Flask talk
Jsp/Servlet
CodeIgniter 3.0
Practical Protocol-Oriented-Programming
Running ms sql stored procedures in mule
Spring 4 advanced final_xtr_presentation
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
Integration of Backbone.js with Spring 3.1
Jsp presentation
Design patterns in Magento
Implicit object.pptx
Spring 4 final xtr_presentation
OpenDMS - the first 2 weeks
JavaScript
Spring Framework Petclinic sample application
Different Types of Containers in Spring
Types of Dependency Injection in Spring
TurboGears2 Pluggable Applications
Demystifying Drupal AJAX Callback Commands
Ad

Viewers also liked (17)

PDF
#32.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PDF
#33.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PDF
#21.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
PDF
#27.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PDF
#19.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
PDF
#22.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PDF
#20.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
PDF
#16.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
PDF
(IT실무교육/국비지원교육/자바/스프링교육추천)#15.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
PDF
(국비지원/실업자교육/재직자교육/스프링교육/마이바티스교육추천)#13.스프링프레임워크 & 마이바티스 (Spring Framework, MyB...
PDF
#17.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
PDF
(스프링교육/마이바티스교육학원추천_탑크리에듀)#10.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
PDF
[#9.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)]_재직자환급교육/실업자환급교육/구로IT학원/스프링교...
PDF
(#8.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis))스프링/자바교육/IT교육/스프링프레임워크교육/국비지...
PDF
[자바학원/스프링교육학원/마이바티스학원추천/구로IT학원_탑크리에듀]#7.스프링프레임워크 & 마이바티스 (Spring Framework, M...
PDF
(Spring Data JPA)식별자(@Id, Primary Key) 자동 생성, @GeneratedValue의 strategy 속성,Ge...
PDF
#12.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_구로IT학원, 국비지원학원,재직자/실업자교육학원,스...
#32.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
#33.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
#21.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#27.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
#19.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#22.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
#20.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#16.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
(IT실무교육/국비지원교육/자바/스프링교육추천)#15.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
(국비지원/실업자교육/재직자교육/스프링교육/마이바티스교육추천)#13.스프링프레임워크 & 마이바티스 (Spring Framework, MyB...
#17.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
(스프링교육/마이바티스교육학원추천_탑크리에듀)#10.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
[#9.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)]_재직자환급교육/실업자환급교육/구로IT학원/스프링교...
(#8.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis))스프링/자바교육/IT교육/스프링프레임워크교육/국비지...
[자바학원/스프링교육학원/마이바티스학원추천/구로IT학원_탑크리에듀]#7.스프링프레임워크 & 마이바티스 (Spring Framework, M...
(Spring Data JPA)식별자(@Id, Primary Key) 자동 생성, @GeneratedValue의 strategy 속성,Ge...
#12.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_구로IT학원, 국비지원학원,재직자/실업자교육학원,스...
Ad

Similar to #31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원교육, 국비지원교육, 구로IT교육학원, IT실무교육, 오라클자바커뮤니티 (20)

PDF
JavaDo#09 Spring boot入門ハンズオン
PDF
Ajax, JSF, Facelets, Eclipse & Maven tutorials
PDF
Java Server Faces
PDF
Spring RestFul Web Services - CRUD Operations Example
PPTX
Spring framework in depth
PDF
JavaServer Faces 2.0 - JavaOne India 2011
PDF
Toms introtospring mvc
PPTX
Skillwise-Spring framework 1
DOC
Sel study notes
PDF
AnkaraJUG Kasım 2012 - PrimeFaces
DOCX
Pom
DOCX
Pom configuration java xml
PDF
Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02
PPTX
2. 엔티티 매핑(entity mapping) 2 2 엔티티매핑 2-2-4. 식별자 자동 생성(@generated-value) part2
PDF
Jaxitalia09 Spring Best Practices
PDF
Webpack Encore Symfony Live 2017 San Francisco
KEY
Multi Client Development with Spring
KEY
Integrating Wicket with Java EE 6
PDF
Codeigniter : Two Step View - Concept Implementation
JavaDo#09 Spring boot入門ハンズオン
Ajax, JSF, Facelets, Eclipse & Maven tutorials
Java Server Faces
Spring RestFul Web Services - CRUD Operations Example
Spring framework in depth
JavaServer Faces 2.0 - JavaOne India 2011
Toms introtospring mvc
Skillwise-Spring framework 1
Sel study notes
AnkaraJUG Kasım 2012 - PrimeFaces
Pom
Pom configuration java xml
Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02
2. 엔티티 매핑(entity mapping) 2 2 엔티티매핑 2-2-4. 식별자 자동 생성(@generated-value) part2
Jaxitalia09 Spring Best Practices
Webpack Encore Symfony Live 2017 San Francisco
Multi Client Development with Spring
Integrating Wicket with Java EE 6
Codeigniter : Two Step View - Concept Implementation

More from 탑크리에듀(구로디지털단지역3번출구 2분거리) (20)

PDF
자마린.안드로이드 기본 내장레이아웃(Built-In List Item Layouts)
PDF
(스프링프레임워크 강좌)스프링부트개요 및 HelloWorld 따라하기
PDF
자마린 iOS 멀티화면 컨트롤러_네비게이션 컨트롤러, 루트 뷰 컨트롤러
PPTX
[IT교육/IT학원]Develope를 위한 IT실무교육
PPTX
[아이오닉학원]아이오닉 하이브리드 앱 개발 과정(아이오닉2로 동적 모바일 앱 만들기)
PPTX
[뷰제이에스학원]뷰제이에스(Vue.js) 프로그래밍 입문(프로그레시브 자바스크립트 프레임워크)
PPTX
[씨샵학원/씨샵교육]C#, 윈폼, 네트워크, ado.net 실무프로젝트 과정
PPTX
[정보처리기사자격증학원]정보처리기사 취득 양성과정(국비무료 자격증과정)
PPTX
[wpf학원,wpf교육]닷넷, c#기반 wpf 프로그래밍 인터페이스구현 재직자 향상과정
PDF
(WPF교육)ListBox와 Linq 쿼리를 이용한 간단한 데이터바인딩, 새창 띄우기, 이벤트 및 델리게이트를 통한 메인윈도우의 ListB...
PDF
[자마린교육/자마린실습]자바,스프링프레임워크(스프링부트) RESTful 웹서비스 구현 실습,자마린에서 스프링 웹서비스를 호출하고 응답 JS...
PPTX
[구로자마린학원/자마린강좌/자마린교육]3. xamarin.ios 3.3.5 추가적인 사항
PPTX
3. xamarin.i os 3.3 xamarin.ios helloworld 자세히 살펴보기 3.4.4 view controllers an...
PPTX
5. 서브 쿼리(sub query) 5.1 서브 쿼리(sub query) 개요 5.2 단일행 서브쿼리(single row sub query)
PPTX
3. xamarin.i os 3.1 xamarin.ios 설치, 개발환경 3.2 xamarin.ios helloworld(단일 뷰) 실습[...
PDF
(닷넷,자마린,아이폰실습)Xamarin.iOS HelloWorld 실습_멀티화면,화면전환_Xamarin교육/Xamarin강좌
PPTX
C#기초에서 윈도우, 스마트폰 앱개발 과정(c#.net, ado.net, win form, wpf, 자마린)_자마린학원_씨샵교육_WPF학원...
PPTX
자바, 웹 기초와 스프링 프레임워크 & 마이바티스 재직자 향상과정(자바학원/자바교육/자바기업출강]
PPTX
3. xamarin.i os 3.1 xamarin.ios 설치, 개발환경 3.2 xamarin.ios helloworld_자마린학원_자마린...
PPTX
3. 안드로이드 애플리케이션 구성요소 3.2인텐트 part01(안드로이드학원/안드로이드교육/안드로이드강좌/안드로이드기업출강]
자마린.안드로이드 기본 내장레이아웃(Built-In List Item Layouts)
(스프링프레임워크 강좌)스프링부트개요 및 HelloWorld 따라하기
자마린 iOS 멀티화면 컨트롤러_네비게이션 컨트롤러, 루트 뷰 컨트롤러
[IT교육/IT학원]Develope를 위한 IT실무교육
[아이오닉학원]아이오닉 하이브리드 앱 개발 과정(아이오닉2로 동적 모바일 앱 만들기)
[뷰제이에스학원]뷰제이에스(Vue.js) 프로그래밍 입문(프로그레시브 자바스크립트 프레임워크)
[씨샵학원/씨샵교육]C#, 윈폼, 네트워크, ado.net 실무프로젝트 과정
[정보처리기사자격증학원]정보처리기사 취득 양성과정(국비무료 자격증과정)
[wpf학원,wpf교육]닷넷, c#기반 wpf 프로그래밍 인터페이스구현 재직자 향상과정
(WPF교육)ListBox와 Linq 쿼리를 이용한 간단한 데이터바인딩, 새창 띄우기, 이벤트 및 델리게이트를 통한 메인윈도우의 ListB...
[자마린교육/자마린실습]자바,스프링프레임워크(스프링부트) RESTful 웹서비스 구현 실습,자마린에서 스프링 웹서비스를 호출하고 응답 JS...
[구로자마린학원/자마린강좌/자마린교육]3. xamarin.ios 3.3.5 추가적인 사항
3. xamarin.i os 3.3 xamarin.ios helloworld 자세히 살펴보기 3.4.4 view controllers an...
5. 서브 쿼리(sub query) 5.1 서브 쿼리(sub query) 개요 5.2 단일행 서브쿼리(single row sub query)
3. xamarin.i os 3.1 xamarin.ios 설치, 개발환경 3.2 xamarin.ios helloworld(단일 뷰) 실습[...
(닷넷,자마린,아이폰실습)Xamarin.iOS HelloWorld 실습_멀티화면,화면전환_Xamarin교육/Xamarin강좌
C#기초에서 윈도우, 스마트폰 앱개발 과정(c#.net, ado.net, win form, wpf, 자마린)_자마린학원_씨샵교육_WPF학원...
자바, 웹 기초와 스프링 프레임워크 & 마이바티스 재직자 향상과정(자바학원/자바교육/자바기업출강]
3. xamarin.i os 3.1 xamarin.ios 설치, 개발환경 3.2 xamarin.ios helloworld_자마린학원_자마린...
3. 안드로이드 애플리케이션 구성요소 3.2인텐트 part01(안드로이드학원/안드로이드교육/안드로이드강좌/안드로이드기업출강]

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Insiders guide to clinical Medicine.pdf
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
From loneliness to social connection charting
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Business Ethics Teaching Materials for college
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Insiders guide to clinical Medicine.pdf
Open folder Downloads.pdf yes yes ges yes
human mycosis Human fungal infections are called human mycosis..pptx
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Anesthesia in Laparoscopic Surgery in India
From loneliness to social connection charting
Cardiovascular Pharmacology for pharmacy students.pptx
Business Ethics Teaching Materials for college
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Week 4 Term 3 Study Techniques revisited.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Open Quiz Monsoon Mind Game Final Set.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Open Quiz Monsoon Mind Game Prelims.pptx
Pharma ospi slides which help in ospi learning
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf

#31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원교육, 국비지원교육, 구로IT교육학원, IT실무교육, 오라클자바커뮤니티

  • 1. 스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis) 6-18. Spring WEB MVC쇼핑카트(@SessionAttibutes, @ModelAttribute)  Spring WEB MVC를 이용하여 간단하게 장바구니를 구현하자.  장바구니에 담길 상품항목은 name, price, cnt 정도로 하자 1. STS에서 File  New  Spring Legacy Project Project Name : spring4cart Spring MVC Project 선택 다음 화면의 top level package에서 a.b.spring4cart 입력 전체 프로젝트 구성은 아래 그림과 같다.
  • 2. 2. 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>
  • 3. <servlet.version>2.5</servlet.version> <!-- Spring --> <spring-framework.version>4.2.4.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>
  • 4. <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>
  • 5. </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project> 3. /WEB-INF/web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener- class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param- value>
  • 6. </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> 4. /WEB-INF/spring/appServlet/servlet-context.xml <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB- INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" />
  • 7. </beans:bean> <context:component-scan base-package="onj.edu.spring4cart" /> </beans:beans> 5. 쇼핑몰에 담길 상품(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; } } 6. 컨트롤러[HomeController.java] package onj.edu.spring4cart; import java.util.ArrayList;
  • 8. 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:/"; } } 7. /WEB-INF/views/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>
  • 9. <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> 8. 실행 http://localhost:8080/spring4cart/