There are two distinct types of spring containers as mentioned below:
1. BeanFactory Container:
This is the simplest container providing basic support for Dependency Injection and defined by the org.springframework.beans.factory.BeanFactory interface. Related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, can provide callbacks which can be configured for the different phases of the Spring Bean’s life cycle.
BeanFactory, by default lazy loads the beans, it creates the bean only when the getBean() method is called.
2. ApplicationContext Container:
This container adds more enterprise-specific functionalities such as ability to resolve textual messages from a properties file and ability to publish application events to the interested event listeners. This container is defined by org.springframework.context.ApplicationContext interface.
ApplicationContext loads all Spring Beans upon start-up unlike BeanFactory.
ApplicationContext container includes all functionality of the BeanFactory container, so it is generally recommended over the BeanFactory. BeanFactory can be used for light weight applications like mobile devices or applet based applications where data volume and speed is significant.
In Spring MVC, view resolvers enable you to render models in a browser without tying you to a specific view technology like JSP, Velocity, XML…etc.
There are two interfaces that are important to the way Spring handles views are ViewResolver and View. The ViewResolver provides a mapping between view names and actual views. The View interface addresses the preparation of the request and hands the request over to one of the view technologies.
Below are the important view resolvers provided by spring framework:
AbstractCachingViewResolver : Abstract view resolver that caches views. Often views need preparation before they can be used; extending this view resolver provides caching.
XmlViewResolver : Implementation of ViewResolver that accepts a configuration file written in XML with the same DTD as Spring’s XML bean factories. The default configuration file is /WEB-INF/views.xml.
ResourceBundleViewResolver : Implementation of ViewResolver that uses bean definitions in a ResourceBundle, specified by the bundle base name. Typically you define the bundle in a properties file, located in the classpath. The default file name is views.properties.
UrlBasedViewResolver : Simple implementation of the ViewResolver interface that effects the direct resolution of logical view names to URLs, without an explicit mapping definition. This is appropriate if your logical names match the names of your view resources in a straightforward manner, without the need for arbitrary mappings.
InternalResourceViewResolver : Convenient subclass of UrlBasedViewResolver that supports InternalResourceView (in effect, Servlets and JSPs) and subclasses such as JstlView and TilesView. You can specify the view class for all views generated by this resolver by using setViewClass(..).
VelocityViewResolver/FreeMarkerViewResolver : Convenient subclass of UrlBasedViewResolver that supports VelocityView (in effect, Velocity templates) or FreeMarkerView ,respectively, and custom subclasses of them.
ContentNegotiatingViewResolver : Implementation of the ViewResolver interface that resolves a view based on the request file name or Accept header.
This post explains how to create spring based web application.
Folder Structure:
Typical folder structure of the spring web application contains as below.
web.xml:
Below is the web.xml file which contains the spring configuration. DispatcherServlet is controller of the spring mvc application, all the requests which coming to the application will be passed through it. DispatcherServlet loads the spring-context.xml file during server start up and keeps the all the configurations in servlet context. Action path configured for this applications is *.html.
index.jsp file configured in web.xml as a welcome file, so this file will be loaded when user hit the http://localhost:8080/SpringMVC/ url in the browser.
<jsp:forward page=”sayhello.html”></jsp:forward>
HelloWorldController.java:
When ever index.jsp loaded into the browser, it will forward the request to sayhello.html. Thissayhello request is mapped to sayHello() method in HelloWorldController class. So, sayHello() method will be executed and it will return the ‘hello’ string after completion of method execution.
/**
*
* @author Javatutorials
*
*/
@Controller
public class HelloWorldController {
/**
* Controls the ‘sayhello’ and return to hello.jsp
* @return
*/
@RequestMapping(value = “/sayhello”, method = RequestMethod.GET)
public String sayHello() {
System.out.println(“Sample Spring Application”);
return “hello”;
}
}
spring-context.xml:
Package of HelloWorldController class is configured as a controller package in the spring-context file as mentioned below. This sample application is configured with the InternalResourceViewResolver, when ever sayHello() method returns the hello string, request will be redirected to hello.jsp based upon the InternalResourceViewResolver configuration as mentioned in the below file.
Finally ‘Hi, This is Sample Spring MVC Application’ message will be shown to the user as it is mentioned in the below jsp.
<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
pageEncoding=”ISO-8859-1″%>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″>
<title>Sample Spring MVC Application</title>
</head>
<body>Hi, This is Sample Spring MVC Application
</body>
</html>
Below post talks about the Spring MVC integration with the tiles framework.
You can build developer friendly and user friendly web applications using the tiles framework.
Basic Folder structure of the application:
Typical folder structure of the Spring MVC application has mentioned below:
Spring MVC Application folder structure
Web.xml:
Below is the web.xml file which contains the spring configuration. DispatcherServlet is controller of the spring mvc application, all the requests which coming to the application will be passed through it. DispatcherServlet loads the spring-context.xml file during server start up and keeps the all the configurations in servlet context. Action path configured for this applications is *.html.
index.jsp file configured in web.xml as a welcome file, so this file will be loaded when user hit the http://localhost:8080/SpringMVC/ url in the browser.
<jsp:forward page=”contacts.html”></jsp:forward>
HelloWorldController .java:
When ever index.jsp loaded into the browser, it will forward the request to contacts.html. Thiscontacts request is mapped to contollContacts() method in HelloWorldController class. So, contollContacts() method will be executed and it will return the ‘contact’ string after completion of method execution.
/**
*
* @author Javatutorials
*
*/
@Controller
public class HelloWorldController {
/**
* Controls the ‘contacts’ and return to respective layout page
* @return
*/
@RequestMapping(value = “/contacts”, method = RequestMethod.GET)
public String contollContacts() {
String message = “Hi, You are in Controller !!!”;
System.out.println(message);
return “contact”;
}
}
spring-context.xml:
As the Tiles framework has been configured in the spring-context file as below with UrlBasedViewResolver, whenever contact will be returned by contollContacts() methods, request will be reached to tiles.xml and contact tile configuration will be loaded.
Below will be the tiles configuration made for our sample application. contact tiles definition is extending to base.definition and base definition will be using layout.jsp as the layout file for our application.
<definition name=”contact” extends=”base.definition”>
<put-attribute name=”title”
value=”Sample Spring MVC With Tiles Appliacation” />
<put-attribute name=”body” value=”/jsp/contact.jsp” />
</definition>
</tiles-definitions>
layout.jsp:
This layout.jsp is divided the complete application page as header, menu, body, footer. So, once the layout.jsp called, all the other jsps will be included as per the configuration mentioned in the tiles.xml file.