CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.

Slides:



Advertisements
Similar presentations
Apache Struts Technology
Advertisements

DT211/3 Internet Application Development JSP: Processing User input.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
DT211/3 Internet Application Development
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
Apache Struts Technology A MVC Framework for Java Web Applications.
ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
© Internna Technologies 1 IWebMvc Features, Possibilities & Goals.
Spring Framework Benefits…
UNIT-V The MVC architecture and Struts Framework.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
JavaScript Form Validation
CSCI 6962: Server-side Design and Programming Course Introduction and Overview.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
OpusCollege and the use of Spring and iBatis
Standalone Java Application vs. Java Web Application
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
Fall CIS 764 Database Systems Design L8. Web ….
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Opus College – Techniques Spring Annotated Controllers.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Forms with Spring MVC Handling Form.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
CSCI 6962: Server-side Design and Programming Java Server Faces Scoping and Session Handling.
INTEGRATION OF BACKBONE.JS WITH SPRING 3.1. Agenda New Features and Enhancements in Spring 3.1 What is Backbone.js and why I should use it Spring 3.1.
Getting started with ASP.NET MVC Dhananjay
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
1 Web Programming with Servlets & JSP ASSIGNMENT GUIDELINE.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
ASP-2-1 SERVER AND CLIENT SIDE SCRITPING Colorado Technical University IT420 Tim Peterson.
Implementation Struts Framework for well-architectured web applications Model-View-Controller design pattern.
Chapter 9 Web Application Design. Objectives Describe the MVC design pattern as used with Web applications Explain the role and responsibilities of each.
Apache Struts Technology A MVC Framework for Java Web Applications.
DSPACE UI Challange Roberto Suardi – Luigi Andrea Pascarelli
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Bits and Pieces of Web Programming (II) Chengyu Sun California State University, Los Angeles.
A Presentation Presentation On JSP On JSP & Online Shopping Cart Online Shopping Cart.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
Open Software Integrators, LLC 1 Spring Roo - IDE Research ● Basics of Spring Roo (Installation and Roo shell) ● Spring Roo, Maven, Tomcat works! ● Front.
Jim Fawcett CSE686 – Internet Programming Spring 2014
CS3220 Web and Internet Programming Database Access with JDBC
Chengyu Sun California State University, Los Angeles
CS520 Web Programming Declarative Security (II)
CS520 Web Programming Bits and Pieces of Web Programming (II)
A very brief introduction
Play Framework: Introduction
CS520 Web Programming Spring – Inversion of Control
J2EE Lecture 7: Spring – Spring MVC
Unit 6-Chapter 2 Struts.
CS5220 Advanced Topics in Web Programming Spring – Web MVC
CS320 Web and Internet Programming Cookies and Session Tracking
The Model Layer What is Model?
CS320 Web and Internet Programming MVC Architecture
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Secure REST API
CS4961 Software Design Laboratory Understand Aquila Backend
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles

Spring Framework

Roadmap Introduction to Spring MVC Database access Controllers Message bundle and input validation

The SpringMVC Example Spring and Hibernate from Scratch un/course_materials/cs520/sham/ un/course_materials/cs520/sham/

Add Spring MVC to A Maven Webapp Spring dependencies spring-webmvc Front controller DispatcherServlet in web.xml Bean configuration file /WEB-INF/ -servlet.xml

Request Processing in Spring Web MVC Front Controller Application Server Controller View model Request Response Controller URL Mapping URL Mapping View Resolution

Spring Controllers Spring controllers are beans annotated In -servlet.xml

Controller URL Mapping Requests are mapped to controller methods value : URL pattern(s) Mapping can be further refined by using method, params, and headers urrent/spring-framework- reference/html/mvc.html#mvc-ann- requestmapping urrent/spring-framework- reference/html/mvc.html#mvc-ann- requestmapping

View A controller method returns a view name, which will be resolved to a view implementation by a view resolver

Resolve JSP Views <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> Prefix + ViewName + Suffix = JSP File Why not just return the path to the JSP file??

View Technologies and Resolvers cs/current/javadoc- api/org/springframework/web/servlet/Vi ewResolver.html

Model Model objects are passed to view using a ModelMap put( String, Object ) attribute name attribute value

Request Processing Recap Front Controller Application Server Controller View model Request Response Controller URL Mapping URL Mapping View Resolution

Database Access Dependencies Hibernate Spring ORM Database connection pooling DBCP vs. Tomcat JDBC Database driver

Configuration Hibernate JPA persistence.xml Spring web.xml applicationContext.xml

Spring Transaction Management Transaction management code is added through AOP enables annotations enables annotations

Model Classes and Database Model classes, e.g. User JPA annotations Database SQL scripts hibernate_sequence

Database Access DAO interfaces, e.g. UserDao DAO implementations, e.g. UserDaoImpl

Spring DAO for DAO implementation injects an entity manager to the for methods that write to the database

Controller Examples List all users Display a selected user Add a new user Edit a user

Example: List All Users @ModelMap Using DAO

Example: Display A User required

Access HTTP Request, Response, and Session Simply add an argument of that type to the controller method cs/current/spring-framework- reference/html/mvc.html#mvc-ann- arguments

Example: Add A User Model attribute (a.k.a. command object, form backing object, form object) method The “redirect” view

Model Attribute Bind form fields to properties of the object Usename: Password: Login HTML Form model attribute public class User { String username; String password; … } Binding

Spring’s form Tag Library Documentation - rent/spring-framework- reference/html/view.html#view-jsp-formtaglib rent/spring-framework- reference/html/view.html#view-jsp-formtaglib Tag reference – rent/spring-framework-reference/html/spring- form.tld.html rent/spring-framework-reference/html/spring- form.tld.html

Handling Forms One controller method for Creating a model attribute Returning the form view Form view Use Spring tags to bind the model attribute to the form One controller method for Processing the model attribute (annotated ) Returning the success view

Example: Edit A User Store model attribute in SessionStatus  Use setComplete() to remove the model attribute in session

Non-Session Model Attributes Form request Create a model attribute in controller Use the model attribute to populate the form fields in view Form Submission Create a model attribute Populate the model attribute with submitted data Pass the model attribute to controller

Session Model Attributes Form request Create a model attribute in controller Use the model attribute to populate the form fields in view Form Submission Retrieve the model attribute from session Populate the model attribute with submitted data Pass the model attribute to controller Save the model attribute in session

Message Bundles Separate text messages from application code and put them into their own files E.g. messages.properties error.username.required=A username is required. error.password.short=The password is too short. error.username.taken=The username {0} is already taken.

Advantages of Using Message Bundles Change text messages without modifying the source code Internationalization (I18N) messages.properties messages_en_US.properties messages_zh_CN.properties …

Using Message Bundles with JSTL

Using Message Bundles with Spring Declare a messageSource bean tag <spring:message code="msg1" arguments="Chengyu" />

Input Validation in Spring Add error messages to the message bundle Implement a Validator and wire it to the controller Add a BindingResult parameter to the controller method Use the validator to validate the model attribute Return the form view if validation fails Display errors using

Example: Validate Add/Edit User Both username and password fields cannot be empty Exercise: Cannot use an existing username

Why Input Validation Seems So Complicated There may be multiple errors in a form submission Need to identify which error is caused by which field Internationalization Remember the correct fields Separate validation code from application logic BindingResult Model attribute and other tags Validator Message bundle

Other Validation Options Server-side vs. Client-side Validation JavaScript validation jQuery Validation plugin - plugins/jquery-plugin-validation/ plugins/jquery-plugin-validation/ Commons-validator Provide both declarative and programmatic validation

Commons-Validator Declarative Validation Example <field property="difficultyLevel" depends="required, integer">

Commons-Validator Routines -validator/javadocs/api /org/apache/commons/validator/routines /package-summary.html Independent of the declarative validation framework A set of validators to validate Date and time Numeric values, currency...

Readings Spring Framework Reference Document, Chapter cs/current/spring-framework- reference/html/mvc.html cs/current/spring-framework- reference/html/mvc.html