40
copyright © I-Admin Spring Framework 3.0 MVC Prepared By: Ravi Kant Soni Sr. Software Engineer | ADS-Bangalore session - 2

Spring MVC 3.0 Framework (sesson_2)

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Spring MVC 3.0 Framework (sesson_2)

copyright © I-Admin

Spring Framework 3.0 MVC

Prepared By:

Ravi Kant Soni

Sr. Software Engineer | ADS-Bangalore

session - 2

Page 2: Spring MVC 3.0 Framework (sesson_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

Page 3: Spring MVC 3.0 Framework (sesson_2)

copyright © I-Admin

Spring MVC Form Handling Example

To develop a Dynamic Form based Web

Application using Spring MVC Framework

Page 4: Spring MVC 3.0 Framework (sesson_2)

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

Page 5: Spring MVC 3.0 Framework (sesson_2)

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()……..

}

Page 6: Spring MVC 3.0 Framework (sesson_2)

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";

}

}

Page 7: Spring MVC 3.0 Framework (sesson_2)

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>

Page 8: Spring MVC 3.0 Framework (sesson_2)

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>

Page 9: Spring MVC 3.0 Framework (sesson_2)

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>

Page 10: Spring MVC 3.0 Framework (sesson_2)

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>

Page 11: Spring MVC 3.0 Framework (sesson_2)

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

Page 12: Spring MVC 3.0 Framework (sesson_2)

copyright © I-Admin

Spring Page Redirection Example

redirect to transfer a http request to another

page

Page 13: Spring MVC 3.0 Framework (sesson_2)

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

Page 14: Spring MVC 3.0 Framework (sesson_2)

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";

}

}

Page 15: Spring MVC 3.0 Framework (sesson_2)

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>

Page 16: Spring MVC 3.0 Framework (sesson_2)

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>

Page 17: Spring MVC 3.0 Framework (sesson_2)

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>

Page 18: Spring MVC 3.0 Framework (sesson_2)

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>

Page 19: Spring MVC 3.0 Framework (sesson_2)

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

Page 20: Spring MVC 3.0 Framework (sesson_2)

copyright © I-Admin

Spring Static pages Example

Access static pages along with dynamic

pages with the help of <mvc:resources> tag

Page 21: Spring MVC 3.0 Framework (sesson_2)

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

Page 22: Spring MVC 3.0 Framework (sesson_2)

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";

}

}

Page 23: Spring MVC 3.0 Framework (sesson_2)

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>

Page 24: Spring MVC 3.0 Framework (sesson_2)

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/" />

Page 25: Spring MVC 3.0 Framework (sesson_2)

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>

Page 26: Spring MVC 3.0 Framework (sesson_2)

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>

Page 27: Spring MVC 3.0 Framework (sesson_2)

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

Page 28: Spring MVC 3.0 Framework (sesson_2)

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

Page 29: Spring MVC 3.0 Framework (sesson_2)

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

Page 30: Spring MVC 3.0 Framework (sesson_2)

copyright © I-Admin

Spring Exception Handling cont…

Student.java

public class Student {

private Integer age;

private String name;

private Integer id;

public getter() & setter()……..

}

Page 31: Spring MVC 3.0 Framework (sesson_2)

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;

}

}

Page 32: Spring MVC 3.0 Framework (sesson_2)

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";

}

}

Page 33: Spring MVC 3.0 Framework (sesson_2)

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>

Page 34: Spring MVC 3.0 Framework (sesson_2)

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>

Page 35: Spring MVC 3.0 Framework (sesson_2)

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>

Page 36: Spring MVC 3.0 Framework (sesson_2)

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>

Page 37: Spring MVC 3.0 Framework (sesson_2)

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>

Page 38: Spring MVC 3.0 Framework (sesson_2)

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>

Page 39: Spring MVC 3.0 Framework (sesson_2)

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

Page 40: Spring MVC 3.0 Framework (sesson_2)

copyright © I-Admin

Questions

Thank You [email protected]