Spring MVC 3.0 Framework (sesson_2)
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Spring MVC 3.0 Framework (sesson_2)

on

  • 1,317 views

 

Statistics

Views

Total Views
1,317
Views on SlideShare
1,317
Embed Views
0

Actions

Likes
2
Downloads
24
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Spring MVC 3.0 Framework (sesson_2) Presentation Transcript

  • 1. copyright © I-AdminSpring Framework 3.0 MVCPrepared By:Ravi Kant SoniSr. Software Engineer | ADS-Bangaloresession - 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. copyright © I-AdminSpring MVC Form Handling Example To develop a Dynamic Form based WebApplication using Spring MVC Framework
  • 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. copyright © I-AdminSpring MVC Form Handling cont… Student.javapublic class Student {private Integer age;private String name;private Integer id;public getter() & setter()……..}
  • 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. 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. 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. 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. 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. 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. copyright © I-AdminSpring Page Redirection Example redirect to transfer a http request to anotherpage
  • 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. 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. 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. 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. 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. 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. 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. copyright © I-AdminSpring Static pages Example Access static pages along with dynamicpages with the help of <mvc:resources> tag
  • 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. 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. 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. 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. 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. 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. 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. copyright © I-AdminSpring Exception Handling Example Simple web based application using SpringMVC Framework, which can handle one ormore exceptions raised inside its controllers
  • 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. copyright © I-AdminSpring Exception Handling cont… Student.javapublic class Student {private Integer age;private String name;private Integer id;public getter() & setter()……..}
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. copyright © I-AdminQuestionsThank Youravikant.soni@i-admin.com