copyright © I-AdminSpring Framework 3.0 MVCPrepared By:Ravi Kant SoniSr. Software Engineer | ADS-Bangaloresession - 2
copyright © I-AdminObjectives Demonstrate Spring MVC with Examples– Spring MVC Form Handling Example– Spring Page Redirec...
copyright © I-AdminSpring MVC Form Handling Example To develop a Dynamic Form based WebApplication using Spring MVC Frame...
copyright © I-AdminSpring MVC Form Handling cont… Steps– Create a Dynamic Web Project– Add Spring and other libraries int...
copyright © I-AdminSpring MVC Form Handling cont… Student.javapublic class Student {private Integer age;private String na...
copyright © I-AdminSpring MVC Form Handling cont… StudentController.java@Controllerpublic class StudentController {@Reque...
copyright © I-AdminSpring MVC Form Handling cont… web.xml<display-name>Spring MVC Form Handling</display-name><servlet><s...
copyright © I-AdminSpring MVC Form Handling cont… Spring-servlet.xml<beans ……..><context:component-scan base-package="com...
copyright © I-AdminSpring MVC Form Handling cont… student.jsp<%@taglib uri="http://www.springframework.org/tags/form" pre...
copyright © I-AdminSpring MVC Form Handling cont… result.jsp<%@taglib uri="http://www.springframework.org/tags/form" pref...
copyright © I-AdminSpring MVC Form Handling cont… List of Spring and other libraries to be included in your webapplicatio...
copyright © I-AdminSpring Page Redirection Example redirect to transfer a http request to anotherpage
copyright © I-AdminSpring Page Redirection cont… Steps:– Create a Dynamic Web Project– Add Spring and other libraries int...
copyright © I-AdminSpring Page Redirection cont… WebController.java@Controllerpublic class WebController {@RequestMapping...
copyright © I-AdminSpring Page Redirection cont… web.xml<display-name>Spring Page Redirection</display-name><servlet><ser...
copyright © I-AdminSpring Page Redirection cont… Spring-servlet.xml<context:component-scan base-package="com.tutorialspoi...
copyright © I-AdminSpring Page Redirection cont… index.jsp <%@taglib uri="http://www.springframework.org/tags/form"prefi...
copyright © I-AdminSpring Page Redirection cont… final.jsp<%@taglib uri="http://www.springframework.org/tags/form"prefix=...
copyright © I-AdminSpring Page Redirection cont… List of Spring and other libraries to be included inyour web application...
copyright © I-AdminSpring Static pages Example Access static pages along with dynamicpages with the help of <mvc:resource...
copyright © I-AdminSpring Static pages cont… Steps– Create a Dynamic Web Project– Add Spring and other libraries into the...
copyright © I-AdminSpring Static pages cont… WebController.java@Controllerpublic class WebController {@RequestMapping(val...
copyright © I-AdminSpring Static pages cont… web.xml<display-name>Spring Page Redirection</display-name><servlet><servlet...
copyright © I-AdminSpring Static pages cont… Spring-servlet.xml <mvc:resources..../> tag is being used to map static pag...
copyright © I-AdminSpring Static pages cont… index.jsp <%@taglib uri="http://www.springframework.org/tags/form"prefix="f...
copyright © I-AdminSpring Static pages cont… WEB-INF/pages/final.htm<html><head><title>Spring Static Page</title></head><...
copyright © I-AdminSpring Static pages cont… List of Spring and other libraries to be included inyour web application in ...
copyright © I-AdminSpring Exception Handling Example Simple web based application using SpringMVC Framework, which can ha...
copyright © I-AdminSpring Exception Handling cont… Steps:– Create a Dynamic Web Project– Add Spring and other libraries i...
copyright © I-AdminSpring Exception Handling cont… Student.javapublic class Student {private Integer age;private String n...
copyright © I-AdminSpring Exception Handling cont… SpringException.javapublic class SpringException extends RuntimeExcept...
copyright © I-AdminSpring Exception Handling cont… StudentController.java@Controllerpublic class StudentController {@Requ...
copyright © I-AdminSpring Exception Handling cont… web.xml<display-name>Spring Exception Handling</display-name><servlet>...
copyright © I-AdminSpring Exception Handling cont… Spring-servlet.xml<context:component-scan base-package="com.tutorialsp...
copyright © I-AdminSpring Exception Handling cont… student.jsp<form:form method="POST" action="/HelloWeb/addStudent"><tab...
copyright © I-AdminSpring Exception Handling cont… Other type of exception, generic view error will takeplace error.jsp<...
copyright © I-AdminSpring Exception Handling cont… ExceptionPage.jsp ExceptionPage as an exception view in case SpringEx...
copyright © I-AdminSpring Exception Handling cont… result.jsp<h2>Submitted Student Information</h2><table><tr><td>Name</t...
copyright © I-AdminSpring Exception Handling cont… List of Spring and other libraries to be included inyour web applicati...
copyright © I-AdminQuestionsThank Youravikant.soni@i-admin.com
Upcoming SlideShare
Loading in...5
×

Spring MVC 3.0 Framework (sesson_2)

1,563

Published on

Published in: Education, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,563
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
52
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Spring MVC 3.0 Framework (sesson_2)

  1. 1. copyright © I-AdminSpring Framework 3.0 MVCPrepared By:Ravi Kant SoniSr. Software Engineer | ADS-Bangaloresession - 2
  2. 2. copyright © I-AdminObjectives Demonstrate Spring MVC with Examples– Spring MVC Form Handling Example– Spring Page Redirection Example– Spring Static pages Example– Spring Exception Handling Example
  3. 3. copyright © I-AdminSpring MVC Form Handling Example To develop a Dynamic Form based WebApplication using Spring MVC Framework
  4. 4. copyright © I-AdminSpring MVC Form Handling cont… Steps– Create a Dynamic Web Project– Add Spring and other libraries into thefolder 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 underthe WebContent/WEB-INF folder. Create a viewfiles student.jsp and result.jsp under this sub-folder
  5. 5. copyright © I-AdminSpring MVC Form Handling cont… Student.javapublic class Student {private Integer age;private String name;private Integer id;public getter() & setter()……..}
  6. 6. copyright © I-AdminSpring MVC Form Handling cont… StudentController.java@Controllerpublic 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. 7. copyright © I-AdminSpring 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. 8. copyright © I-AdminSpring 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. 9. copyright © I-AdminSpring 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. 10. copyright © I-AdminSpring 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. 11. copyright © I-AdminSpring MVC Form Handling cont… List of Spring and other libraries to be included in your webapplication 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. 12. copyright © I-AdminSpring Page Redirection Example redirect to transfer a http request to anotherpage
  13. 13. copyright © I-AdminSpring Page Redirection cont… Steps:– Create a Dynamic Web Project– Add Spring and other libraries into thefolder 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 underthe WebContent/WEB-INF folder
  14. 14. copyright © I-AdminSpring Page Redirection cont… WebController.java@Controllerpublic 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. 15. copyright © I-AdminSpring 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. 16. copyright © I-AdminSpring 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. 17. copyright © I-AdminSpring 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. 18. copyright © I-AdminSpring 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. 19. copyright © I-AdminSpring Page Redirection cont… List of Spring and other libraries to be included inyour 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. 20. copyright © I-AdminSpring Static pages Example Access static pages along with dynamicpages with the help of <mvc:resources> tag
  21. 21. copyright © I-AdminSpring Static pages cont… Steps– Create a Dynamic Web Project– Add Spring and other libraries into thefolder 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 underthe WebContent/WEB-INF folder– Create a sub-folder with a name pages underthe WebContent/WEB-INF folder. Create a staticfile final.htm under this sub-folder
  22. 22. copyright © I-AdminSpring Static pages cont… WebController.java@Controllerpublic 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. 23. copyright © I-AdminSpring 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. 24. copyright © I-AdminSpring 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. 25. copyright © I-AdminSpring 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. 26. copyright © I-AdminSpring 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. 27. copyright © I-AdminSpring Static pages cont… List of Spring and other libraries to be included inyour 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. 28. copyright © I-AdminSpring Exception Handling Example Simple web based application using SpringMVC Framework, which can handle one ormore exceptions raised inside its controllers
  29. 29. copyright © I-AdminSpring Exception Handling cont… Steps:– Create a Dynamic Web Project– Add Spring and other libraries into the folder WebContent/WEB-INF/lib– Create a Javaclasses 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. 30. copyright © I-AdminSpring Exception Handling cont… Student.javapublic class Student {private Integer age;private String name;private Integer id;public getter() & setter()……..}
  31. 31. copyright © I-AdminSpring Exception Handling cont… SpringException.javapublic 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. 32. copyright © I-AdminSpring Exception Handling cont… StudentController.java@Controllerpublic 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. 33. copyright © I-AdminSpring 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. 34. copyright © I-AdminSpring 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. 35. copyright © I-AdminSpring 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. 36. copyright © I-AdminSpring Exception Handling cont… Other type of exception, generic view error will takeplace error.jsp<html><head><title>Spring Error Page</title></head><body><p>An error occured, please contact webmaster.</p></body></html>
  37. 37. copyright © I-AdminSpring 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. 38. copyright © I-AdminSpring 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. 39. copyright © I-AdminSpring Exception Handling cont… List of Spring and other libraries to be included inyour 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
  40. 40. copyright © I-AdminQuestionsThank Youravikant.soni@i-admin.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×