Presented by SeongMin Lim For the Seminar on May 1 Hanbat National University 1.
Chapter 14. SpringMVC and...
Transcript of Chapter 14. SpringMVC and...
1 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Chapter 14. SpringMVC and Hibernate
What Is SpringMVC?
Web Application
Gradle Cargo Plugin
SpringMVC Controller
2 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
What is SpringMVC?
The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications.
The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.
– The Model encapsulates the application data and in general they will consist of POJO.
– The View is responsible for rendering the model data and in general it generates HTML output that the client's browser can interpret.
– The Controller is responsible for processing user requests and building appropriate model and passes it to the view for rendering.
Spring MVC Framework Tutorial
http://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm
3 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Prepare Tomcat
Download the latest Tomcat Server from http://tomcat.apache.org/
– apache-tomcat-8.0.14-windows-x86.zip (32 bit version)
– apache-tomcat-8.0.14-windows-x64.zip (64 bit version)
Install the latest Tomcat Server
– unzip apache-tomcat-8.0.14-windows-x64.zip to D:\
– check directory name : D:\apache-tomcat-8.0.14
4 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Combining SpringMVC and Hibernate
Adding the SpringMVC as a project dependency
build.gradle :
dependencies {
compile 'org.springframework:spring-webmvc:4.+'
compile( 'org.springframework:spring-core:4.+' ) {exclude group: 'commons-logging', module: 'commons-logging'
}compile 'org.springframework:spring-context:4.+'compile 'org.springframework:spring-orm:4.+'
runtime 'org.slf4j:jcl-over-slf4j:1.+'}
5 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Setup Gradle for Web Application …
Setup gradle-cargo-plugin
build.gradle :
buildscript {repositories {jcenter()
}dependencies {classpath 'com.bmuschko:gradle-cargo-plugin:2.1.1'
}}
ext {cargoVersion = '1.+' // 1.4.10tomcatVersion = '8.0.14'
}
apply plugin: 'war' // providedCompile, providedRuntimeapply plugin: 'com.bmuschko.cargo' // cargoRunLocalapply plugin: 'eclipse-wtp' // eclipse
6 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Setup Gradle for Web Application …
Setup gradle-cargo-plugin
cargoRunLocal {dependsOn war
}
cargo {containerId = 'tomcat8x'port = 8080
deployable {//file = file("build/libs/${project.name}.war")context = 'springmvc'
}
local {homeDir = file("D:/apache-tomcat-${tomcatVersion}")outputFile = file('build/output.log')
}}
7 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Setup Gradle for Web Application …
Dependencies
dependencies {cargo "org.codehaus.cargo:cargo-core-uberjar:${cargoVersion}"cargo "org.codehaus.cargo:cargo-ant:${cargoVersion}"cargo 'org.slf4j:jcl-over-slf4j:1.+'
// Tomcat Servlet/JSP LibraryprovidedCompile "org.apache.tomcat:tomcat-jsp-api:${tomcatVersion}"
// Tomcat JSTL Libraryruntime 'org.apache.taglibs:taglibs-standard-spec:1.2.1'runtime 'org.apache.taglibs:taglibs-standard-impl:1.2.1'
compile 'org.springframework:spring-webmvc:4.+'compile 'org.springframework:spring-orm:4.+'compile 'org.springframework:spring-context-support:4.+'
runtime 'org.slf4j:jcl-over-slf4j:1.+'
compile 'org.apache.commons:commons-dbcp2:2.+'}
8 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Web Application Layout
data
– Album
– AlbumTrack
– Artist
– Track
dao
– AlbumDAO
– ArtistDAO
– TrackDAO
service
– AlbumService
web
– AlbumController
9 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
10 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
webapp/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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>ch14-1-springmvc-annotation
11 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Basic JSP in webapp/index.jsp
<?xml version="1.0"?><%@ page contentType="text/html;charset=UTF-8" language="java"%>Hello World
12 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
webapp/WEB-INF/springmvc-servlet.xml …
<beans xmlns="http://www.springframework.org/schema/beans" …
<!-- Enables the Spring MVC @Controller programming model --><mvc:annotation-driven />
<!-- Resolves views selected for rendering by @Controllers to .jsp resourcesin the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/" /><property name="suffix" value=".jsp" />
</bean>
<context:property-placeholder location="classpath:jdbc-hibernate.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" />
</bean>
13 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
webapp/WEB-INF/springmvc-servlet.xml …
<!-- Hibernate 3 SessionFactory Bean definition --><bean id="hibernate3AnnotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.oreilly.hh.data"/>
<property name="hibernateProperties"><props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props></property>
</bean>
14 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
webapp/WEB-INF/springmvc-servlet.xml
<!-- Auto scan the components --><!-- @Component, @Repository, @Service, @Controller --><context:component-scan base-package="com.oreilly.hh"/>
<!-- Process annotations on registered beans like @Autowired... --><context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="hibernate3AnnotatedSessionFactory" />
</bean>
</beans>
15 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Web View (http://localhost:8080/springmvc/albums)
16 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
SpringMVC AlbumController …
@Controllerpublic class AlbumController {
@Autowiredprivate AlbumService albumService;
public void setAlbumService(AlbumService albumService) {this.albumService = albumService;
}
@RequestMapping(value = "/albums", method = RequestMethod.GET)public String listAlbums(Model model) {
model.addAttribute("album", new Album());model.addAttribute("listAlbums", this.albumService.list());return "album";
}
17 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
SpringMVC AlbumController
//For add and update album both@RequestMapping(value= "/album/add", method = RequestMethod.POST)public String persist(@ModelAttribute("album") Album album) {
this.albumService.persist(album);
return "redirect:/albums";}
@RequestMapping("/remove/{id}")public String delete(@PathVariable("id") Integer id) {
this.albumService.delete(id);
return "redirect:/albums";}
@RequestMapping("/edit/{id}")public String editAlbum(@PathVariable("id") Integer id, Model model) {
model.addAttribute("album", this.albumService.get(id));model.addAttribute("listAlbums", this.albumService.list());
return "album";}
}
18 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Web View (http://localhost:8080/springmvc/albums)
19 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
webapp/WEB-INF/views/album.jsp …
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %><%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head><title>Album Page</title></head>
<body><h1>Add a Album
</h1>
20 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
webapp/WEB-INF/views/album.jsp …
<c:url var="addAction" value="/album/add" ></c:url>
<form:form action="${addAction}" commandName="album"><table>
<c:if test="${!empty album.title}"><tr><td><form:label path="id"><spring:message text="ID"/>
</form:label></td><td><form:input path="id" readonly="true" size="8" disabled="true" /><form:hidden path="id" />
</td></tr></c:if>
21 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
webapp/WEB-INF/views/album.jsp …
<tr><td><form:label path="title"><spring:message text="Title"/>
</form:label></td><td><form:input path="title" />
</td></tr><tr><td><form:label path="numDiscs"><spring:message text="NumDiscs"/>
</form:label></td><td><form:input path="numDiscs" />
</td></tr>
22 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
webapp/WEB-INF/views/album.jsp …
<tr><td colspan="2"><c:if test="${!empty album.title}"><input type="submit" value="<spring:message text="Edit Album"/>" /></c:if><c:if test="${empty album.title}"><input type="submit" value="<spring:message text="Add Album"/>" /></c:if>
</td></tr>
</table></form:form>
23 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
webapp/WEB-INF/views/album.jsp
<br><h3>Albums List</h3>
<c:if test="${!empty listAlbums}"><table class="tg"><tr><th width="80">Album ID</th><th width="120">Album Title</th><th width="120">Album Discs</th><th width="60">Edit</th><th width="60">Delete</th>
</tr><c:forEach items="${listAlbums}" var="album"><tr><td>${album.id}</td><td>${album.title}</td><td>${album.numDiscs}</td><td><a href="<c:url value='/edit/${album.id}' />" >Edit</a></td><td><a href="<c:url value='/remove/${album.id}' />" >Delete</a></td>
</tr></c:forEach>
</table></c:if>
</body></html>
24 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Compile and Run our SpringMVC Application
gradle war
gradle cargoRunLocal
– Open http://localhost:8080/springmvc/albums with your browser
25 / 25Prof. Youngchan KIM, Dept of Computer Engineering, Hanbat National University
Database Programming
Materials for Further Study
Hibernate Home
– http://www.hibernate.org/
Hibernate Manual
– Hibernate Getting Started Guide 3.6
• http://docs.jboss.org/hibernate/core/3.6/quickstart/en-US/html/
– Hibernate Reference Documentation 3.6
• http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/
• http://docs.jboss.org/hibernate/core/3.6/reference/en-US/pdf/hibernate_reference.pdf
– Hibernate Reference Documentation 4.3 and 5.0
SpringMVC Tutorial
– http://www.mkyong.com/