SlideShare a Scribd company logo
copyright © I-Admin
Spring Framework 3.0 MVC
Prepared By:
Ravi Kant Soni
Sr. Software Engineer | ADS-Bangalore
session - 2
copyright © I-Admin
Objectives
 Demonstrate Spring MVC with Examples
– Spring MVC Form Handling Example
– Spring Page Redirection Example
– Spring Static pages Example
– Spring Exception Handling Example
copyright © I-Admin
Spring MVC Form Handling Example
 To develop a Dynamic Form based Web
Application using Spring MVC Framework
copyright © I-Admin
Spring MVC Form Handling cont…
 Steps
– Create a Dynamic Web Project
– Add Spring and other libraries into the
folder WebContent/WEB-INF/lib
– Create a Java classes Student and StudentController
– Create Spring configuration files Web.xml and Spring-
servlet.xml under the WebContent/WEB-INF folder
– Create a sub-folder with a name jsp under
the WebContent/WEB-INF folder. Create a view
files student.jsp and result.jsp under this sub-folder
copyright © I-Admin
Spring MVC Form Handling cont…
 Student.java
public class Student {
private Integer age;
private String name;
private Integer id;
public getter() & setter()……..
}
copyright © I-Admin
Spring MVC Form Handling cont…
 StudentController.java
@Controller
public class StudentController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public String student(ModelMap model) {
model.addAttribute( "command", new Student());
return “student”;
}
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("SpringWeb") Student student,
ModelMap model) {
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());
return "result";
}
}
copyright © I-Admin
Spring MVC Form Handling cont…
 web.xml
<display-name>Spring MVC Form Handling</display-name>
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet
</servlet-class> <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
copyright © I-Admin
Spring MVC Form Handling cont…
 Spring-servlet.xml
<beans ……..>
<context:component-scan base-package="com.tutorialspoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
copyright © I-Admin
Spring MVC Form Handling cont…
 student.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head> <title>Spring MVC Form Handling</title> </head>
<body>
<h2>Student Information</h2>
<form:form method="POST" action="/HelloWeb/addStudent">
<table>
<tr>
<td><form:label path="name">Name</form:label></td> <td><form:input path="name" /></td>
</tr> <tr>
<td><form:label path="age">Age</form:label></td> <td><form:input path="age" /></td>
</tr> <tr>
<td><form:label path="id">id</form:label></td><td><form:input path="id" /></td>
</tr> <tr>
<td colspan="2"> <input type="submit" value="Submit"/> </td>
</tr>
</table>
</form:form>
</body>
</html>
copyright © I-Admin
Spring MVC Form Handling cont…
 result.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head> <title>Spring MVC Form Handling</title> </head>
<body>
<h2>Submitted Student Information</h2>
<table>
<tr> <td>Name</td> <td>${name}</td> </tr>
<tr> <td>Age</td> <td>${age}</td> </tr>
<tr> <td>ID</td> <td>${id}</td> </tr>
</table> </body>
</html>
copyright © I-Admin
Spring MVC Form Handling cont…
 List of Spring and other libraries to be included in your web
application in WebContent/WEB-INF/lib folder
– commons-logging-x.y.z.jar
– org.springframework.asm-x.y.z.jar
– org.springframework.beans-x.y.z.jar
– org.springframework.context-x.y.z.jar
– org.springframework.core-x.y.z.jar
– org.springframework.expression-x.y.z.jar
– org.springframework.web.servlet-x.y.z.jar
– org.springframework.web-x.y.z.jar
– spring-web.jar
copyright © I-Admin
Spring Page Redirection Example
 redirect to transfer a http request to another
page
copyright © I-Admin
Spring Page Redirection cont…
 Steps:
– Create a Dynamic Web Project
– Add Spring and other libraries into the
folder WebContent/WEB-INF/lib
– Create a Java class WebController
– Create Spring configuration files Web.xml and Spring-
servlet.xml under theWebContent/WEB-INF folder
– Create a sub-folder with a name jsp under
the WebContent/WEB-INF folder
copyright © I-Admin
Spring Page Redirection cont…
 WebController.java
@Controller
public class WebController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/redirect", method =RequestMethod.GET)
public String redirect() {
return "redirect:finalPage";
}
@RequestMapping(value = "/finalPage", method = RequestMethod.GET)
public String finalPage() {
return "final";
}
}
copyright © I-Admin
Spring Page Redirection cont…
 web.xml
<display-name>Spring Page Redirection</display-name>
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
copyright © I-Admin
Spring Page Redirection cont…
 Spring-servlet.xml
<context:component-scan base-package="com.tutorialspoint" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
copyright © I-Admin
Spring Page Redirection cont…
 index.jsp
 <%@taglib uri="http://www.springframework.org/tags/form"
prefix="form"%>
 Spring Form:
<form:form method="GET" action="/HelloWeb/redirect">
<table>
<tr>
<td> <input type="submit" value="Redirect Page"/> </td>
</tr>
</table>
</form:form>
copyright © I-Admin
Spring Page Redirection cont…
 final.jsp
<%@taglib uri="http://www.springframework.org/tags/form"
prefix="form"%>
<html>
<head>
<title>Spring Page Redirection</title>
</head>
<body>
<h2>Redirected Page</h2>
</body>
</html>
copyright © I-Admin
Spring Page Redirection cont…
 List of Spring and other libraries to be included in
your web application in WebContent/WEB-
INF/lib folder
– commons-logging-x.y.z.jar
– org.springframework.asm-x.y.z.jar
– org.springframework.beans-x.y.z.jar
– org.springframework.context-x.y.z.jar
– org.springframework.core-x.y.z.jar
– org.springframework.expression-x.y.z.jar
– org.springframework.web.servlet-x.y.z.jar
– org.springframework.web-x.y.z.jar
– spring-web.jar
copyright © I-Admin
Spring Static pages Example
 Access static pages along with dynamic
pages with the help of <mvc:resources> tag
copyright © I-Admin
Spring Static pages cont…
 Steps
– Create a Dynamic Web Project
– Add Spring and other libraries into the
folder WebContent/WEB-INF/lib
– Create a Java class WebController
– Create Spring configuration files Web.xml and Spring-
servlet.xml under theWebContent/WEB-INF folder
– Create a sub-folder with a name jsp under
the WebContent/WEB-INF folder
– Create a sub-folder with a name pages under
the WebContent/WEB-INF folder. Create a static
file final.htm under this sub-folder
copyright © I-Admin
Spring Static pages cont…
 WebController.java
@Controller
public class WebController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/staticPage", method = RequestMethod.GET)
public String redirect() {
return "redirect:/pages/final.htm";
}
}
copyright © I-Admin
Spring Static pages cont…
 web.xml
<display-name>Spring Page Redirection</display-name>
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
copyright © I-Admin
Spring Static pages cont…
 Spring-servlet.xml
 <mvc:resources..../> tag is being used to map static pages
 Static pages including images, style sheets, JavaScript, and other static content
 Multiple resource locations may be specified using a comma-separated list of values
<context:component-scan base-package="com.tutorialspoint" />
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />
copyright © I-Admin
Spring Static pages cont…
 index.jsp
 <%@taglib uri="http://www.springframework.org/tags/form"
prefix="form"%>
<p>Click below button to get a simple HTML page</p>
<form:form method="GET" action="/HelloWeb/staticPage">
<table>
<tr>
<td>
<input type="submit" value="Get HTML Page"/>
</td>
</tr>
</table>
</form:form>
copyright © I-Admin
Spring Static pages cont…
 WEB-INF/pages/final.htm
<html>
<head>
<title>Spring Static Page</title>
</head>
<body>
<h2>A simple HTML page</h2>
</body>
</html>
copyright © I-Admin
Spring Static pages cont…
 List of Spring and other libraries to be included in
your web application in WebContent/WEB-
INF/lib folder
– commons-logging-x.y.z.jar
– org.springframework.asm-x.y.z.jar
– org.springframework.beans-x.y.z.jar
– org.springframework.context-x.y.z.jar
– org.springframework.core-x.y.z.jar
– org.springframework.expression-x.y.z.jar
– org.springframework.web.servlet-x.y.z.jar
– org.springframework.web-x.y.z.jar
– spring-web.jar
copyright © I-Admin
Spring Exception Handling Example
 Simple web based application using Spring
MVC Framework, which can handle one or
more exceptions raised inside its controllers
copyright © I-Admin
Spring Exception Handling cont…
 Steps:
– Create a Dynamic Web Project
– Add Spring and other libraries into the folder WebContent/WEB-
INF/lib
– Create a Java
classes Student, StudentController and SpringException
– Create Spring configuration files Web.xml and Spring-
servlet.xml under theWebContent/WEB-INF folder
– Create a sub-folder with a name jsp under the WebContent/WEB-
INF folder. Create a view files
 student.jsp
 result.jsp
 error.jsp
 ExceptionPage.jsp
copyright © I-Admin
Spring Exception Handling cont…
 Student.java
public class Student {
private Integer age;
private String name;
private Integer id;
public getter() & setter()……..
}
copyright © I-Admin
Spring Exception Handling cont…
 SpringException.java
public class SpringException extends RuntimeException{
private String exceptionMsg;
public SpringException(String exceptionMsg) {
this.exceptionMsg = exceptionMsg;
}
public String getExceptionMsg(){
return this.exceptionMsg;
}
public void setExceptionMsg(String exceptionMsg) {
this.exceptionMsg = exceptionMsg;
}
}
copyright © I-Admin
Spring Exception Handling cont…
 StudentController.java
@Controller
public class StudentController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("student", "command", new Student());
}
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
@ExceptionHandler({SpringException.class})
public String addStudent( @ModelAttribute("HelloWeb")Student student, ModelMap model) {
if(student.getName().length() < 5 ){
throw new SpringException("Given name is too short");
}else{
model.addAttribute("name", student.getName());
}
if( student.getAge() < 10 ){
throw new SpringException("Given age is too low");
}else{
model.addAttribute("age", student.getAge());
}
model.addAttribute("id", student.getId());
return "result";
}
}
copyright © I-Admin
Spring Exception Handling cont…
 web.xml
<display-name>Spring Exception Handling</display-name>
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
copyright © I-Admin
Spring Exception Handling cont…
 Spring-servlet.xml
<context:component-scan base-package="com.tutorialspoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.web.servlet.handler.
SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.iadmin.SpringException">
ExceptionPage
</prop>
</props>
</property>
<property name="defaultErrorView" value="error"/>
</bean>
copyright © I-Admin
Spring Exception Handling cont…
 student.jsp
<form:form method="POST" action="/HelloWeb/addStudent">
<table>
<tr> <td>
<form:label path="name">Name</form:label>
</td> <td>
<form:input path="name" />
</td> </tr> <tr> <td>
<form:label path="age">Age</form:label>
</td> <td>
<form:input path="age" />
</td> </tr> <tr> <td>
<form:label path="id">id</form:label>
</td> <td>
<form:input path="id" />
</td> </tr> <tr>
<td colspan="2"> <input type="submit" value="Submit"/>
</td> </tr>
</table>
</form:form>
copyright © I-Admin
Spring Exception Handling cont…
 Other type of exception, generic view error will take
place
 error.jsp
<html>
<head>
<title>Spring Error Page</title>
</head>
<body>
<p>An error occured, please contact webmaster.</p>
</body>
</html>
copyright © I-Admin
Spring Exception Handling cont…
 ExceptionPage.jsp
 ExceptionPage as an exception view in case SpringException occurs
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Exception Handling</title>
</head>
<body>
<h2>Spring MVC Exception Handling</h2>
<h3>${exception.exceptionMsg}</h3>
</body>
</html>
copyright © I-Admin
Spring Exception Handling cont…
 result.jsp
<h2>Submitted Student Information</h2>
<table>
<tr>
<td>Name</td>
<td>${name}</td>
</tr> <tr>
<td>Age</td>
<td>${age}</td>
</tr> <tr>
<td>ID</td> <td>${id}</td>
</tr>
</table>
copyright © I-Admin
Spring Exception Handling cont…
 List of Spring and other libraries to be included in
your web application in WebContent/WEB-
INF/lib folder
– commons-logging-x.y.z.jar
– org.springframework.asm-x.y.z.jar
– org.springframework.beans-x.y.z.jar
– org.springframework.context-x.y.z.jar
– org.springframework.core-x.y.z.jar
– org.springframework.expression-x.y.z.jar
– org.springframework.web.servlet-x.y.z.jar
– org.springframework.web-x.y.z.jar
– spring-web.jar
copyright © I-Admin
Questions
Thank You
ravikant.soni@i-admin.com

More Related Content

What's hot (20)

PPT
Spring 3.x - Spring MVC
Guy Nir
 
PDF
Introduction to Spring MVC
Richard Paul
 
PDF
Spring MVC
Aaron Schram
 
ODP
springmvc-150923124312-lva1-app6892
Tuna Tore
 
PDF
Jsf intro
vantinhkhuc
 
PDF
SpringMVC
Akio Katayama
 
PPT
Spring MVC
yuvalb
 
ODP
Annotation-Based Spring Portlet MVC
John Lewis
 
ODP
A Complete Tour of JSF 2
Jim Driscoll
 
PPT
Struts Introduction Course
guest764934
 
PDF
Spring MVC Annotations
Jordan Silva
 
PDF
Jinal desai .net
rohitkumar1987in
 
PDF
Spring mvc 2.0
Rudra Garnaik, PMI-ACP®
 
PPT
Java Server Faces (JSF) - advanced
BG Java EE Course
 
PPTX
Spring MVC 5 & Hibernate 5 Integration
Majurageerthan Arumugathasan
 
PPTX
Spring mvc
Harshit Choudhary
 
PDF
Sun JSF Presentation
Gaurav Dighe
 
PPTX
Introduction to jsf 2
yousry ibrahim
 
PPTX
Java server faces
owli93
 
Spring 3.x - Spring MVC
Guy Nir
 
Introduction to Spring MVC
Richard Paul
 
Spring MVC
Aaron Schram
 
springmvc-150923124312-lva1-app6892
Tuna Tore
 
Jsf intro
vantinhkhuc
 
SpringMVC
Akio Katayama
 
Spring MVC
yuvalb
 
Annotation-Based Spring Portlet MVC
John Lewis
 
A Complete Tour of JSF 2
Jim Driscoll
 
Struts Introduction Course
guest764934
 
Spring MVC Annotations
Jordan Silva
 
Jinal desai .net
rohitkumar1987in
 
Spring mvc 2.0
Rudra Garnaik, PMI-ACP®
 
Java Server Faces (JSF) - advanced
BG Java EE Course
 
Spring MVC 5 & Hibernate 5 Integration
Majurageerthan Arumugathasan
 
Spring mvc
Harshit Choudhary
 
Sun JSF Presentation
Gaurav Dighe
 
Introduction to jsf 2
yousry ibrahim
 
Java server faces
owli93
 

Viewers also liked (17)

PPTX
портфоліо на мк 2013 [автосохраненный] готовий
les1812
 
DOCX
Курсовая Сланова Н.
Socreklamanalytics
 
DOC
Диплом Никифорова А.
Socreklamanalytics
 
PDF
Padur flower presentation sujitha
sujiswetha65
 
PPTX
Pp pidato
oktavianisari
 
PPTX
Gui automation framework
Ravi Kant Soni ([email protected])
 
PPTX
Pp pidato
oktavianisari
 
PDF
Matilla Portfolio
Matilla Yuen
 
DOCX
Совершенствование методов фестивальной оценки рекламной деятельности (на при...
Socreklamanalytics
 
DOC
Курсовая Хананушан Н.
Socreklamanalytics
 
PPTX
Oktaviani sari
oktavianisari
 
PDF
Caring for your election candidates
Jo Walters
 
PDF
Padur flower presentation sujitha
sujiswetha65
 
DOCX
Диплом Пакалина Ю.
Socreklamanalytics
 
PPTX
Zed ria presentation
sujiswetha65
 
DOCX
Диплом Ярош А.
Socreklamanalytics
 
портфоліо на мк 2013 [автосохраненный] готовий
les1812
 
Курсовая Сланова Н.
Socreklamanalytics
 
Диплом Никифорова А.
Socreklamanalytics
 
Padur flower presentation sujitha
sujiswetha65
 
Pp pidato
oktavianisari
 
Gui automation framework
Ravi Kant Soni ([email protected])
 
Pp pidato
oktavianisari
 
Matilla Portfolio
Matilla Yuen
 
Совершенствование методов фестивальной оценки рекламной деятельности (на при...
Socreklamanalytics
 
Курсовая Хананушан Н.
Socreklamanalytics
 
Oktaviani sari
oktavianisari
 
Caring for your election candidates
Jo Walters
 
Padur flower presentation sujitha
sujiswetha65
 
Диплом Пакалина Ю.
Socreklamanalytics
 
Zed ria presentation
sujiswetha65
 
Диплом Ярош А.
Socreklamanalytics
 
Ad

Similar to Spring MVC 3.0 Framework (sesson_2) (20)

PDF
[Laptrinh.vn] lap trinh Spring Framework 3
Huu Dat Nguyen
 
PPTX
Organize directories for applications with front-end and back-end with yii - ...
Framgia Vietnam
 
PDF
Laravel 8 export data as excel file with example
Katy Slemon
 
PPTX
Mvc in symfony
Sayed Ahmed
 
PPT
Creating web form
mentorrbuddy
 
PPT
Creating web form
mentorrbuddy
 
ZIP
ASP.Net Presentation Part1
Neeraj Mathur
 
PPT
Ibm
techbed
 
PDF
Toms introtospring mvc
Guo Albert
 
PDF
Asp.net By Durgesh Singh
imdurgesh
 
PPT
Asp.net
Naveen Sihag
 
PPT
Asp
yuvaraj72
 
PDF
The Rails Way
Michał Orman
 
KEY
Templates
soon
 
PPT
.Net course-in-mumbai-ppt
vibrantuser
 
PPT
Training in Android with Maven
Arcadian Learning
 
PPT
Spring-training-in-bangalore
TIB Academy
 
[Laptrinh.vn] lap trinh Spring Framework 3
Huu Dat Nguyen
 
Organize directories for applications with front-end and back-end with yii - ...
Framgia Vietnam
 
Laravel 8 export data as excel file with example
Katy Slemon
 
Mvc in symfony
Sayed Ahmed
 
Creating web form
mentorrbuddy
 
Creating web form
mentorrbuddy
 
ASP.Net Presentation Part1
Neeraj Mathur
 
Ibm
techbed
 
Toms introtospring mvc
Guo Albert
 
Asp.net By Durgesh Singh
imdurgesh
 
Asp.net
Naveen Sihag
 
The Rails Way
Michał Orman
 
Templates
soon
 
.Net course-in-mumbai-ppt
vibrantuser
 
Training in Android with Maven
Arcadian Learning
 
Spring-training-in-bangalore
TIB Academy
 
Ad

Recently uploaded (20)

PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Difference between write and update in odoo 18
Celine George
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
epi editorial commitee meeting presentation
MIPLM
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 

Spring MVC 3.0 Framework (sesson_2)

  • 1. copyright © I-Admin Spring Framework 3.0 MVC Prepared By: Ravi Kant Soni Sr. Software Engineer | ADS-Bangalore session - 2
  • 2. copyright © I-Admin Objectives  Demonstrate Spring MVC with Examples – Spring MVC Form Handling Example – Spring Page Redirection Example – Spring Static pages Example – Spring Exception Handling Example
  • 3. copyright © I-Admin Spring MVC Form Handling Example  To develop a Dynamic Form based Web Application using Spring MVC Framework
  • 4. copyright © I-Admin Spring MVC Form Handling cont…  Steps – Create a Dynamic Web Project – Add Spring and other libraries into the folder WebContent/WEB-INF/lib – Create a Java classes Student and StudentController – Create Spring configuration files Web.xml and Spring- servlet.xml under the WebContent/WEB-INF folder – Create a sub-folder with a name jsp under the WebContent/WEB-INF folder. Create a view files student.jsp and result.jsp under this sub-folder
  • 5. copyright © I-Admin Spring MVC Form Handling cont…  Student.java public class Student { private Integer age; private String name; private Integer id; public getter() & setter()…….. }
  • 6. copyright © I-Admin Spring MVC Form Handling cont…  StudentController.java @Controller public class StudentController { @RequestMapping(value = "/student", method = RequestMethod.GET) public String student(ModelMap model) { model.addAttribute( "command", new Student()); return “student”; } @RequestMapping(value = "/addStudent", method = RequestMethod.POST) public String addStudent(@ModelAttribute("SpringWeb") Student student, ModelMap model) { model.addAttribute("name", student.getName()); model.addAttribute("age", student.getAge()); model.addAttribute("id", student.getId()); return "result"; } }
  • 7. copyright © I-Admin Spring MVC Form Handling cont…  web.xml <display-name>Spring MVC Form Handling</display-name> <servlet> <servlet-name>Spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
  • 8. copyright © I-Admin Spring MVC Form Handling cont…  Spring-servlet.xml <beans ……..> <context:component-scan base-package="com.tutorialspoint" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
  • 9. copyright © I-Admin Spring MVC Form Handling cont…  student.jsp <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC Form Handling</title> </head> <body> <h2>Student Information</h2> <form:form method="POST" action="/HelloWeb/addStudent"> <table> <tr> <td><form:label path="name">Name</form:label></td> <td><form:input path="name" /></td> </tr> <tr> <td><form:label path="age">Age</form:label></td> <td><form:input path="age" /></td> </tr> <tr> <td><form:label path="id">id</form:label></td><td><form:input path="id" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit"/> </td> </tr> </table> </form:form> </body> </html>
  • 10. copyright © I-Admin Spring MVC Form Handling cont…  result.jsp <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC Form Handling</title> </head> <body> <h2>Submitted Student Information</h2> <table> <tr> <td>Name</td> <td>${name}</td> </tr> <tr> <td>Age</td> <td>${age}</td> </tr> <tr> <td>ID</td> <td>${id}</td> </tr> </table> </body> </html>
  • 11. copyright © I-Admin Spring MVC Form Handling cont…  List of Spring and other libraries to be included in your web application in WebContent/WEB-INF/lib folder – commons-logging-x.y.z.jar – org.springframework.asm-x.y.z.jar – org.springframework.beans-x.y.z.jar – org.springframework.context-x.y.z.jar – org.springframework.core-x.y.z.jar – org.springframework.expression-x.y.z.jar – org.springframework.web.servlet-x.y.z.jar – org.springframework.web-x.y.z.jar – spring-web.jar
  • 12. copyright © I-Admin Spring Page Redirection Example  redirect to transfer a http request to another page
  • 13. copyright © I-Admin Spring Page Redirection cont…  Steps: – Create a Dynamic Web Project – Add Spring and other libraries into the folder WebContent/WEB-INF/lib – Create a Java class WebController – Create Spring configuration files Web.xml and Spring- servlet.xml under theWebContent/WEB-INF folder – Create a sub-folder with a name jsp under the WebContent/WEB-INF folder
  • 14. copyright © I-Admin Spring Page Redirection cont…  WebController.java @Controller public class WebController { @RequestMapping(value = "/index", method = RequestMethod.GET) public String index() { return "index"; } @RequestMapping(value = "/redirect", method =RequestMethod.GET) public String redirect() { return "redirect:finalPage"; } @RequestMapping(value = "/finalPage", method = RequestMethod.GET) public String finalPage() { return "final"; } }
  • 15. copyright © I-Admin Spring Page Redirection cont…  web.xml <display-name>Spring Page Redirection</display-name> <servlet> <servlet-name>Spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
  • 16. copyright © I-Admin Spring Page Redirection cont…  Spring-servlet.xml <context:component-scan base-package="com.tutorialspoint" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
  • 17. copyright © I-Admin Spring Page Redirection cont…  index.jsp  <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  Spring Form: <form:form method="GET" action="/HelloWeb/redirect"> <table> <tr> <td> <input type="submit" value="Redirect Page"/> </td> </tr> </table> </form:form>
  • 18. copyright © I-Admin Spring Page Redirection cont…  final.jsp <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring Page Redirection</title> </head> <body> <h2>Redirected Page</h2> </body> </html>
  • 19. copyright © I-Admin Spring Page Redirection cont…  List of Spring and other libraries to be included in your web application in WebContent/WEB- INF/lib folder – commons-logging-x.y.z.jar – org.springframework.asm-x.y.z.jar – org.springframework.beans-x.y.z.jar – org.springframework.context-x.y.z.jar – org.springframework.core-x.y.z.jar – org.springframework.expression-x.y.z.jar – org.springframework.web.servlet-x.y.z.jar – org.springframework.web-x.y.z.jar – spring-web.jar
  • 20. copyright © I-Admin Spring Static pages Example  Access static pages along with dynamic pages with the help of <mvc:resources> tag
  • 21. copyright © I-Admin Spring Static pages cont…  Steps – Create a Dynamic Web Project – Add Spring and other libraries into the folder WebContent/WEB-INF/lib – Create a Java class WebController – Create Spring configuration files Web.xml and Spring- servlet.xml under theWebContent/WEB-INF folder – Create a sub-folder with a name jsp under the WebContent/WEB-INF folder – Create a sub-folder with a name pages under the WebContent/WEB-INF folder. Create a static file final.htm under this sub-folder
  • 22. copyright © I-Admin Spring Static pages cont…  WebController.java @Controller public class WebController { @RequestMapping(value = "/index", method = RequestMethod.GET) public String index() { return "index"; } @RequestMapping(value = "/staticPage", method = RequestMethod.GET) public String redirect() { return "redirect:/pages/final.htm"; } }
  • 23. copyright © I-Admin Spring Static pages cont…  web.xml <display-name>Spring Page Redirection</display-name> <servlet> <servlet-name>Spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
  • 24. copyright © I-Admin Spring Static pages cont…  Spring-servlet.xml  <mvc:resources..../> tag is being used to map static pages  Static pages including images, style sheets, JavaScript, and other static content  Multiple resource locations may be specified using a comma-separated list of values <context:component-scan base-package="com.tutorialspoint" /> <mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />
  • 25. copyright © I-Admin Spring Static pages cont…  index.jsp  <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <p>Click below button to get a simple HTML page</p> <form:form method="GET" action="/HelloWeb/staticPage"> <table> <tr> <td> <input type="submit" value="Get HTML Page"/> </td> </tr> </table> </form:form>
  • 26. copyright © I-Admin Spring Static pages cont…  WEB-INF/pages/final.htm <html> <head> <title>Spring Static Page</title> </head> <body> <h2>A simple HTML page</h2> </body> </html>
  • 27. copyright © I-Admin Spring Static pages cont…  List of Spring and other libraries to be included in your web application in WebContent/WEB- INF/lib folder – commons-logging-x.y.z.jar – org.springframework.asm-x.y.z.jar – org.springframework.beans-x.y.z.jar – org.springframework.context-x.y.z.jar – org.springframework.core-x.y.z.jar – org.springframework.expression-x.y.z.jar – org.springframework.web.servlet-x.y.z.jar – org.springframework.web-x.y.z.jar – spring-web.jar
  • 28. copyright © I-Admin Spring Exception Handling Example  Simple web based application using Spring MVC Framework, which can handle one or more exceptions raised inside its controllers
  • 29. copyright © I-Admin Spring Exception Handling cont…  Steps: – Create a Dynamic Web Project – Add Spring and other libraries into the folder WebContent/WEB- INF/lib – Create a Java classes Student, StudentController and SpringException – Create Spring configuration files Web.xml and Spring- servlet.xml under theWebContent/WEB-INF folder – Create a sub-folder with a name jsp under the WebContent/WEB- INF folder. Create a view files  student.jsp  result.jsp  error.jsp  ExceptionPage.jsp
  • 30. copyright © I-Admin Spring Exception Handling cont…  Student.java public class Student { private Integer age; private String name; private Integer id; public getter() & setter()…….. }
  • 31. copyright © I-Admin Spring Exception Handling cont…  SpringException.java public class SpringException extends RuntimeException{ private String exceptionMsg; public SpringException(String exceptionMsg) { this.exceptionMsg = exceptionMsg; } public String getExceptionMsg(){ return this.exceptionMsg; } public void setExceptionMsg(String exceptionMsg) { this.exceptionMsg = exceptionMsg; } }
  • 32. copyright © I-Admin Spring Exception Handling cont…  StudentController.java @Controller public class StudentController { @RequestMapping(value = "/student", method = RequestMethod.GET) public ModelAndView student() { return new ModelAndView("student", "command", new Student()); } @RequestMapping(value = "/addStudent", method = RequestMethod.POST) @ExceptionHandler({SpringException.class}) public String addStudent( @ModelAttribute("HelloWeb")Student student, ModelMap model) { if(student.getName().length() < 5 ){ throw new SpringException("Given name is too short"); }else{ model.addAttribute("name", student.getName()); } if( student.getAge() < 10 ){ throw new SpringException("Given age is too low"); }else{ model.addAttribute("age", student.getAge()); } model.addAttribute("id", student.getId()); return "result"; } }
  • 33. copyright © I-Admin Spring Exception Handling cont…  web.xml <display-name>Spring Exception Handling</display-name> <servlet> <servlet-name>Spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
  • 34. copyright © I-Admin Spring Exception Handling cont…  Spring-servlet.xml <context:component-scan base-package="com.tutorialspoint" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean class="org.springframework.web.servlet.handler. SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="com.iadmin.SpringException"> ExceptionPage </prop> </props> </property> <property name="defaultErrorView" value="error"/> </bean>
  • 35. copyright © I-Admin Spring Exception Handling cont…  student.jsp <form:form method="POST" action="/HelloWeb/addStudent"> <table> <tr> <td> <form:label path="name">Name</form:label> </td> <td> <form:input path="name" /> </td> </tr> <tr> <td> <form:label path="age">Age</form:label> </td> <td> <form:input path="age" /> </td> </tr> <tr> <td> <form:label path="id">id</form:label> </td> <td> <form:input path="id" /> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit"/> </td> </tr> </table> </form:form>
  • 36. copyright © I-Admin Spring Exception Handling cont…  Other type of exception, generic view error will take place  error.jsp <html> <head> <title>Spring Error Page</title> </head> <body> <p>An error occured, please contact webmaster.</p> </body> </html>
  • 37. copyright © I-Admin Spring Exception Handling cont…  ExceptionPage.jsp  ExceptionPage as an exception view in case SpringException occurs <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC Exception Handling</title> </head> <body> <h2>Spring MVC Exception Handling</h2> <h3>${exception.exceptionMsg}</h3> </body> </html>
  • 38. copyright © I-Admin Spring Exception Handling cont…  result.jsp <h2>Submitted Student Information</h2> <table> <tr> <td>Name</td> <td>${name}</td> </tr> <tr> <td>Age</td> <td>${age}</td> </tr> <tr> <td>ID</td> <td>${id}</td> </tr> </table>
  • 39. copyright © I-Admin Spring Exception Handling cont…  List of Spring and other libraries to be included in your web application in WebContent/WEB- INF/lib folder – commons-logging-x.y.z.jar – org.springframework.asm-x.y.z.jar – org.springframework.beans-x.y.z.jar – org.springframework.context-x.y.z.jar – org.springframework.core-x.y.z.jar – org.springframework.expression-x.y.z.jar – org.springframework.web.servlet-x.y.z.jar – org.springframework.web-x.y.z.jar – spring-web.jar