SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Introduction to Spring MVC
Framework
Tuna Tore
Enroll now to my udemy course
Tuna Tore
https://www.udemy.com/java-spring-mvc-framework-with-angularjs-by-google-and-html5
What is MVC?
 MVC stands for Model View Controller (architecture)
 It is a design pattern used in WEB based JAVA Enterprise
Applications
 MVC pattern is also implemented in other WEB frameworks as well
for separating the model layer from view layer. By doing that UI
programmers and back end developers can work together.
 Model Layer includes business specific logic
 View Layer is responsible for rendering(displaying) model objects
inside the user interface using different view technogies (JSP,
Facelets or Velocity) (browser)
 Controller receives user inputs and calls model objects based on
handler mappings and pass model objects to views in order to
display output inside the view layer.
What is Dependency Injection?
 This is also called as IOC (Inversion of Control Principle)
 It is a software design pattern which is really useful for designing
loosely coupled software components
 You will have more plug-gable and testable code and objects
 It is easy to reuse your code in other applications
 The dependencies won't be hard coded inside all java objects/
classes instead they will be defined in XML configuration files or
configuration classes (Java Config)
 Spring Container is responsible for injecting dependencies of objects
 There are two types of Dependency injection in Spring Framework
Setter Injection and Constructor Injection
The general information about web.xml and
Java EE
 The WEB-INF/web.xml is the Web Application
Deployment Descriptor of Java Enterprise Web
applications/
 Inside web.xml, developers can define
Servlets,
Welcome File List
Filters,
Context parameters,
Error pages
Security rights and etc.
The general information about the sample web
application
 Apache Maven Project
 Pivotal tc server
 Java Spring MVC 4.x
version
What is the concept of DispatcherServlet and
how do you configure it?
 DispatcherServlet is configured in order to dispatch
incoming HTTP request to handlers and returns response
to browsers
 Spring MVC is designed around DispatcherServlet
 DispatcherServlet is configured in web.xml file
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
What is the concept of DispatcherServlet and
how do you configure it?
 DispatcherServlet is configured in order to dispatch
incoming HTTP request to handlers and returns response
to browsers
 Spring MVC is designed around DispatcherServlet
 DispatcherServlet is configured in web.xml file
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
What is ContextLoaderListener and how do you
configure it in Spring MVC?
 ContextLoaderListener is responsible for creating a root
application context in Java Enterprise Web application
inside ServletContext
 Developers can use different controller and view layers
(For example Struts, JSF or Spring MVC can be used)
however Spring Framework will be responsible for
managing services layer or components defined
ContextLoaderListener configuration files
 You can set different configuration XML files in web.xml
http://docs.spring.io/spring/docs/2.5.x/reference/mvc.html
#mvc-servlet
How do you configure controllers in Spring
MVC?
 By extending AbstractController class and
implementing handleRequestInternal method
 By using @Controller or @RestController
annotation on class level
 Controller with bean name URL mapping
 Controller with class name mapping
 Controller with XML config with mappings
How is an incoming request mapped to a
mapped to a method in Spring MVC?
 @RequestMapping is used in order to map incoming
request to a controller using URL matching
 @RequestParam and @PathVariable is used to get
parameters or variable from HTTP request
@RequestMapping(value = "/jdbcDelete/user/{iduser}",
method=RequestMethod.GET)
public ModelAndView jdbcDelete(@PathVariable(value="iduser")
int iduser) {
@RequestMapping(value="/register", method=RequestMethod.POST)
public ModelAndView register(@RequestParam(required=true)
String email,@RequestParam(required=true) String password)
What is a View in Spring MVC? How is the
InternalResourceViewResolver configured?
 Java Spring MVC can be configured with different
view technologies such as Java Server Faces, JSP,
Velocity etc.
 Views are responsible for displaying the content of
Model objects on browsers as response output
 InternalResourceViewResolver is used to resolve
the correct view based on suffix and prefixes so the
correct output (view) is resolved based on strings
Return types from Controllers
You can return the followings from
Controllers
 ModelAndView
 Model
 Map
 View
 String
 void
 HttpHeaders and others
Scopes in Spring MVC
● Default Scope in Spring MVC is request
● Session scope can be used in order to
save the state of beans in web based
applications
● Each time a request send through HTTP a
new bean will be created with request
scope
Application Event Handling
● Developers can handle application events by
implementing
ApplicationListener<ApplicationEvent>
● Customs events can be published by using
application context and extending
ApplicationEvent class
Spring MVC File Upload
● In order to upload a file with Java Spring MVC
<!-- File Upload bean config-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartRe
solver">
<!-- set the maximum file size in bytes -->
<property name="maxUploadSize" value="1000000"/>
</bean>
@Controller
public class FileUploadController {
@RequestMapping(value="/uploadFile", method=RequestMethod.POST)
public @ResponseBody String
handleFileUpload(@RequestParam("file") MultipartFile file){
Quartz Scheduling Framework and Spring
Schedules
 Quartz is an alternative to Spring Schedules
and has more functionality
 In order to use Quartz; Triggers, Jobs, Tasks
and Scheduler have to defined in XML
configuration
 Spring Schedules can be configured using
@Scheduled annotation
 @Scheduled(cron="0/30 * * * * ?") or
@Scheduled(fixedDelay = 10000) can be
applied to schedules
Logging in Spring MVC
● LOGBack framework will be used in order to log outputs
● LOGBack is a significantly improved version of log4j
logging framework
● Both log4j and logback were founded with the same
developer
● It is easy to switch logging technologies by using
LOGBack
● The configuration is done through logback.xml
● By default Spring Framework use commons-logging
so dependencies should be excluded in pom.xml
Apache Maven
● Apache Maven is a build automation and
dependency management tool
● Apache Maven is configured using
pom.xml file
● Maven is integrated into Eclipse or STS
JPA and Hibernate ORM
● JPA stands for Java Persistence API
● Hibernate ORM is an implementation of JPA
● ORM stands for Object Relational Mapping
● Hibernate ORM uses @Entity annotation to manage
classes in ORM framework
● @Table(name="USER") is used to map Java Objects to
Database Tables
● @Id and @Column(name="USERNAME") annotations
can be used on field levels.
● @PersistenceContext has to be used on EntityManager
JDBC and JDBCTemplate
● JDBC stands for Java Database Connectivity
● It is an application programming interface API for
connecting different databases
● Spring MVC uses jdbcTemplate in order to run queries
on databases
● JdbcTemplate simplifies database access code
● JdbcTemplate handles database related exceptions and
throws DataAccessException
PDF and Excel Documents
● In order to return PDF and Excel Documents
org.springframework.web.servlet.view.XmlViewResolver
has to be configured in Spring MVC Framework
● Documents have to extend related classes named as
AbstractExcelView and AbstractPdfView
● Apache POI is used to generate PDFs inside sample
applicaions
● Itext library is used to generate Excel documents
Spring MVC Java Config
● In order to configure Spring MVC with JavaConfig;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"spring.javaconfig"})
public class JavaConfig extends WebMvcConfigurerAdapter {
● You don't need web.xml file with the new specification
Servlet API 3.0+
public class WebInitializer implements
WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
Spring MVC Email
● Spring MVC can send emails to users using Java Mail
API
● Inside the sample application Velocity Email Template is
used in order to send customized emails
● Velocity is configured in configuration files and template
locations should be set
<!-- Velocity Email Template Config Bean -->
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="/WEB-INF/email-templates/"/>
</bean>
Spring Security
● In order to activate Spring Security in Spring
MVC applications, springSecurityFilterChain has
to be configured in web.xml file with
DelegatingFilterProxy
● Spring Security annotations and custom tags
can be used in order to define java method level
security
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-
class>
</filter>
Spring Exception Handling
● Inside Spring MVC, users can define a exception
handling class by implementing
@Component
public class ExceptionHandler implements HandlerExceptionResolver{
private static final Logger logger =
LoggerFactory.getLogger(ExceptionHandler.class);
@Override
public ModelAndView resolveException(HttpServletRequest
request,HttpServletResponse response, Object object, Exception
exception) {
logger.error("Error: ", exception);
return new
ModelAndView("error/exception","exception","ExceptionHandler
message: " + exception.toString());
}
Spring REST with RestTemplate
● @RestController will add @ResponseBody annotations
to all methods inside a class
● RestTemplate is used to access Rest Based Web
Services
● @PathVariable is used in order to get variables from
URL
● ResponseEntity class is used to map response to Java
Objects
Enroll now to my udemy course
Tuna Tore
https://www.udemy.com/java-spring-mvc-framework-with-angularjs-by-google-and-html5

Weitere ähnliche Inhalte

Was ist angesagt?

Java Server Faces + Spring MVC Framework
Java Server Faces + Spring MVC FrameworkJava Server Faces + Spring MVC Framework
Java Server Faces + Spring MVC Framework
Guo Albert
 
Struts Introduction Course
Struts Introduction CourseStruts Introduction Course
Struts Introduction Course
guest764934
 
Integration of Backbone.js with Spring 3.1
Integration of Backbone.js with Spring 3.1Integration of Backbone.js with Spring 3.1
Integration of Backbone.js with Spring 3.1
Michał Orman
 

Was ist angesagt? (20)

Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVC
 
Spring MVC Architecture Tutorial
Spring MVC Architecture TutorialSpring MVC Architecture Tutorial
Spring MVC Architecture Tutorial
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Spring MVC 3.0 Framework (sesson_2)
Spring MVC 3.0 Framework (sesson_2)Spring MVC 3.0 Framework (sesson_2)
Spring MVC 3.0 Framework (sesson_2)
 
Java Server Faces + Spring MVC Framework
Java Server Faces + Spring MVC FrameworkJava Server Faces + Spring MVC Framework
Java Server Faces + Spring MVC Framework
 
Introduction to Spring MVC
Introduction to Spring MVCIntroduction to Spring MVC
Introduction to Spring MVC
 
SpringMVC
SpringMVCSpringMVC
SpringMVC
 
Jsf intro
Jsf introJsf intro
Jsf intro
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Struts Introduction Course
Struts Introduction CourseStruts Introduction Course
Struts Introduction Course
 
A Complete Tour of JSF 2
A Complete Tour of JSF 2A Complete Tour of JSF 2
A Complete Tour of JSF 2
 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVC
 
MVC on the server and on the client
MVC on the server and on the clientMVC on the server and on the client
MVC on the server and on the client
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Integration of Backbone.js with Spring 3.1
Integration of Backbone.js with Spring 3.1Integration of Backbone.js with Spring 3.1
Integration of Backbone.js with Spring 3.1
 
Spring Core
Spring CoreSpring Core
Spring Core
 
JSF Component Behaviors
JSF Component BehaviorsJSF Component Behaviors
JSF Component Behaviors
 
Annotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCAnnotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVC
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
 
Jsf2.0 -4
Jsf2.0 -4Jsf2.0 -4
Jsf2.0 -4
 

Ähnlich wie Java Spring MVC Framework with AngularJS by Google and HTML5

quickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcquickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvc
jorgesimao71
 
quickguide-einnovator-8-spring-cloud
quickguide-einnovator-8-spring-cloudquickguide-einnovator-8-spring-cloud
quickguide-einnovator-8-spring-cloud
jorgesimao71
 
Rediscovering Spring with Spring Boot(1)
Rediscovering Spring with Spring Boot(1)Rediscovering Spring with Spring Boot(1)
Rediscovering Spring with Spring Boot(1)
Gunith Devasurendra
 

Ähnlich wie Java Spring MVC Framework with AngularJS by Google and HTML5 (20)

quickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcquickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvc
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Spring mvc 2.0
Spring mvc 2.0Spring mvc 2.0
Spring mvc 2.0
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
MVC
MVCMVC
MVC
 
Spring MVC framework features and concepts
Spring MVC framework features and conceptsSpring MVC framework features and concepts
Spring MVC framework features and concepts
 
SpringBootCompleteBootcamp.pptx
SpringBootCompleteBootcamp.pptxSpringBootCompleteBootcamp.pptx
SpringBootCompleteBootcamp.pptx
 
Jinal desai .net
Jinal desai .netJinal desai .net
Jinal desai .net
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdfdokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
 
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf
dokumen.tips_rediscovering-spring-with-spring-boot1.pdfdokumen.tips_rediscovering-spring-with-spring-boot1.pdf
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Session 1
Session 1Session 1
Session 1
 
Oip presentation
Oip presentationOip presentation
Oip presentation
 
quickguide-einnovator-8-spring-cloud
quickguide-einnovator-8-spring-cloudquickguide-einnovator-8-spring-cloud
quickguide-einnovator-8-spring-cloud
 
Rediscovering Spring with Spring Boot(1)
Rediscovering Spring with Spring Boot(1)Rediscovering Spring with Spring Boot(1)
Rediscovering Spring with Spring Boot(1)
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083
 
JEE5 New Features
JEE5 New FeaturesJEE5 New Features
JEE5 New Features
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
Mvc
MvcMvc
Mvc
 

Kürzlich hochgeladen

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Java Spring MVC Framework with AngularJS by Google and HTML5

  • 1. Introduction to Spring MVC Framework Tuna Tore
  • 2. Enroll now to my udemy course Tuna Tore https://www.udemy.com/java-spring-mvc-framework-with-angularjs-by-google-and-html5
  • 3. What is MVC?  MVC stands for Model View Controller (architecture)  It is a design pattern used in WEB based JAVA Enterprise Applications  MVC pattern is also implemented in other WEB frameworks as well for separating the model layer from view layer. By doing that UI programmers and back end developers can work together.  Model Layer includes business specific logic  View Layer is responsible for rendering(displaying) model objects inside the user interface using different view technogies (JSP, Facelets or Velocity) (browser)  Controller receives user inputs and calls model objects based on handler mappings and pass model objects to views in order to display output inside the view layer.
  • 4. What is Dependency Injection?  This is also called as IOC (Inversion of Control Principle)  It is a software design pattern which is really useful for designing loosely coupled software components  You will have more plug-gable and testable code and objects  It is easy to reuse your code in other applications  The dependencies won't be hard coded inside all java objects/ classes instead they will be defined in XML configuration files or configuration classes (Java Config)  Spring Container is responsible for injecting dependencies of objects  There are two types of Dependency injection in Spring Framework Setter Injection and Constructor Injection
  • 5.
  • 6. The general information about web.xml and Java EE  The WEB-INF/web.xml is the Web Application Deployment Descriptor of Java Enterprise Web applications/  Inside web.xml, developers can define Servlets, Welcome File List Filters, Context parameters, Error pages Security rights and etc.
  • 7. The general information about the sample web application  Apache Maven Project  Pivotal tc server  Java Spring MVC 4.x version
  • 8. What is the concept of DispatcherServlet and how do you configure it?  DispatcherServlet is configured in order to dispatch incoming HTTP request to handlers and returns response to browsers  Spring MVC is designed around DispatcherServlet  DispatcherServlet is configured in web.xml file <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet- class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
  • 9. What is the concept of DispatcherServlet and how do you configure it?  DispatcherServlet is configured in order to dispatch incoming HTTP request to handlers and returns response to browsers  Spring MVC is designed around DispatcherServlet  DispatcherServlet is configured in web.xml file <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet- class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
  • 10. What is ContextLoaderListener and how do you configure it in Spring MVC?  ContextLoaderListener is responsible for creating a root application context in Java Enterprise Web application inside ServletContext  Developers can use different controller and view layers (For example Struts, JSF or Spring MVC can be used) however Spring Framework will be responsible for managing services layer or components defined ContextLoaderListener configuration files  You can set different configuration XML files in web.xml http://docs.spring.io/spring/docs/2.5.x/reference/mvc.html #mvc-servlet
  • 11. How do you configure controllers in Spring MVC?  By extending AbstractController class and implementing handleRequestInternal method  By using @Controller or @RestController annotation on class level  Controller with bean name URL mapping  Controller with class name mapping  Controller with XML config with mappings
  • 12. How is an incoming request mapped to a mapped to a method in Spring MVC?  @RequestMapping is used in order to map incoming request to a controller using URL matching  @RequestParam and @PathVariable is used to get parameters or variable from HTTP request @RequestMapping(value = "/jdbcDelete/user/{iduser}", method=RequestMethod.GET) public ModelAndView jdbcDelete(@PathVariable(value="iduser") int iduser) { @RequestMapping(value="/register", method=RequestMethod.POST) public ModelAndView register(@RequestParam(required=true) String email,@RequestParam(required=true) String password)
  • 13. What is a View in Spring MVC? How is the InternalResourceViewResolver configured?  Java Spring MVC can be configured with different view technologies such as Java Server Faces, JSP, Velocity etc.  Views are responsible for displaying the content of Model objects on browsers as response output  InternalResourceViewResolver is used to resolve the correct view based on suffix and prefixes so the correct output (view) is resolved based on strings
  • 14. Return types from Controllers You can return the followings from Controllers  ModelAndView  Model  Map  View  String  void  HttpHeaders and others
  • 15. Scopes in Spring MVC ● Default Scope in Spring MVC is request ● Session scope can be used in order to save the state of beans in web based applications ● Each time a request send through HTTP a new bean will be created with request scope
  • 16. Application Event Handling ● Developers can handle application events by implementing ApplicationListener<ApplicationEvent> ● Customs events can be published by using application context and extending ApplicationEvent class
  • 17. Spring MVC File Upload ● In order to upload a file with Java Spring MVC <!-- File Upload bean config--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartRe solver"> <!-- set the maximum file size in bytes --> <property name="maxUploadSize" value="1000000"/> </bean> @Controller public class FileUploadController { @RequestMapping(value="/uploadFile", method=RequestMethod.POST) public @ResponseBody String handleFileUpload(@RequestParam("file") MultipartFile file){
  • 18. Quartz Scheduling Framework and Spring Schedules  Quartz is an alternative to Spring Schedules and has more functionality  In order to use Quartz; Triggers, Jobs, Tasks and Scheduler have to defined in XML configuration  Spring Schedules can be configured using @Scheduled annotation  @Scheduled(cron="0/30 * * * * ?") or @Scheduled(fixedDelay = 10000) can be applied to schedules
  • 19. Logging in Spring MVC ● LOGBack framework will be used in order to log outputs ● LOGBack is a significantly improved version of log4j logging framework ● Both log4j and logback were founded with the same developer ● It is easy to switch logging technologies by using LOGBack ● The configuration is done through logback.xml ● By default Spring Framework use commons-logging so dependencies should be excluded in pom.xml
  • 20. Apache Maven ● Apache Maven is a build automation and dependency management tool ● Apache Maven is configured using pom.xml file ● Maven is integrated into Eclipse or STS
  • 21. JPA and Hibernate ORM ● JPA stands for Java Persistence API ● Hibernate ORM is an implementation of JPA ● ORM stands for Object Relational Mapping ● Hibernate ORM uses @Entity annotation to manage classes in ORM framework ● @Table(name="USER") is used to map Java Objects to Database Tables ● @Id and @Column(name="USERNAME") annotations can be used on field levels. ● @PersistenceContext has to be used on EntityManager
  • 22. JDBC and JDBCTemplate ● JDBC stands for Java Database Connectivity ● It is an application programming interface API for connecting different databases ● Spring MVC uses jdbcTemplate in order to run queries on databases ● JdbcTemplate simplifies database access code ● JdbcTemplate handles database related exceptions and throws DataAccessException
  • 23. PDF and Excel Documents ● In order to return PDF and Excel Documents org.springframework.web.servlet.view.XmlViewResolver has to be configured in Spring MVC Framework ● Documents have to extend related classes named as AbstractExcelView and AbstractPdfView ● Apache POI is used to generate PDFs inside sample applicaions ● Itext library is used to generate Excel documents
  • 24. Spring MVC Java Config ● In order to configure Spring MVC with JavaConfig; @Configuration @EnableWebMvc @ComponentScan(basePackages = {"spring.javaconfig"}) public class JavaConfig extends WebMvcConfigurerAdapter { ● You don't need web.xml file with the new specification Servlet API 3.0+ public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException {
  • 25. Spring MVC Email ● Spring MVC can send emails to users using Java Mail API ● Inside the sample application Velocity Email Template is used in order to send customized emails ● Velocity is configured in configuration files and template locations should be set <!-- Velocity Email Template Config Bean --> <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="resourceLoaderPath" value="/WEB-INF/email-templates/"/> </bean>
  • 26. Spring Security ● In order to activate Spring Security in Spring MVC applications, springSecurityFilterChain has to be configured in web.xml file with DelegatingFilterProxy ● Spring Security annotations and custom tags can be used in order to define java method level security <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter- class> </filter>
  • 27. Spring Exception Handling ● Inside Spring MVC, users can define a exception handling class by implementing @Component public class ExceptionHandler implements HandlerExceptionResolver{ private static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class); @Override public ModelAndView resolveException(HttpServletRequest request,HttpServletResponse response, Object object, Exception exception) { logger.error("Error: ", exception); return new ModelAndView("error/exception","exception","ExceptionHandler message: " + exception.toString()); }
  • 28. Spring REST with RestTemplate ● @RestController will add @ResponseBody annotations to all methods inside a class ● RestTemplate is used to access Rest Based Web Services ● @PathVariable is used in order to get variables from URL ● ResponseEntity class is used to map response to Java Objects
  • 29. Enroll now to my udemy course Tuna Tore https://www.udemy.com/java-spring-mvc-framework-with-angularjs-by-google-and-html5