Friday, October 22, 2010

Spring with in a Day Part-3

Procedure to create a web application with Spring in MyEclipse:-
1. Chose File => new =>project.
2. In the new project window select web project => click on the next button.
3. In new web project window provide project name (exmp: swproj) web root folder (exmp:swap), context root URL (exmp: /swap) and check Add JSTL check box and click on Finish button.
4. Choose swproj in package explorer and choose MyEclipse => Add Spring Capabilities.
5. In Add Spring Capabilities window check all the check boxes except hibernate2, select copy checked lib contains radio button => next button.
6. Click on the Browse button and select WEB-INF (swproj/awapp/web-inf) => OK button => Finish button.
7. Create a file with the name spring-servlet.xml under WEB-INF directory by copy applicationContext.xml.
8. Add the following information in web.xml
< context-param>
< param-name>contextConfigLocation< /param-name>
< param-value>/WEB-INF/applicationContext.xml< /param-value>
< /context-param>
< listener>
< listener-class>
< /listener-class>
< /listener>
< servlet>
< servlet-name>spring< /servlet-name>
< servlet-class>org.spring.web.servlet.DispatcherServlet< /servlet-class>
< /servlet>
< servlet-mapping>
< servlet-name>spring< /servlet-name>
< url-pattern>*.htm< /url-pattern>
< /servlet-mapping>
9. Add the information shown bellow in spring-servlet.xml file
< bean id=”jspViewResolver”
< property name=”viewClass”>
< value> org.springframework.web.servlet.view.JstlView< /value>
< /property>
< property name=”prefix” value=”/pages”>< /property>
< property name=”suffix” value=”.jsp”>< /property>
< /bean>
< bean name=”/hellowWorld.htm” class=”org.students.HellowWorldController”>< /bean>

i. ContextLoaderListener implements ServletContextListner.
ii. When a spring based application is started the web container calls the ContextInitialized() of ContextLoaderListener class.
iii. The spring code of this method creates a spring container (parent container) and this container reads the info available in applicationContext.xml

public class ContextLoaderListener implements ServerContextListener {
………. contextInitialized (… … …) {
// 1) code to get the value of contextConfigLocation param
// 2) code to create spring container by passing contextConfigLocation …….
// (WEB-INF/applicationContext.xml is used as the value of
// contextConfigLocation by most of the developers)
…………. contextDestroyed(… … …) {
//code for unrefetence the listener object

i. The internal code of the DispatcherServlet creates second spring container (child container). This container reads the information from spring-servlet.xml
ii. The first container acts as the parent of the second container. The child container will be able to pick up the beans from the parent container.
iii. DispatcherServlet as the same of ActionServlet in Struts.
iv. The web container executes the spring code available in dispatcherServlet when receives the request that is send using the URL that is ends with .htm.

Controller class
i. As part of the spring based web application, several controller classes will be provided by us.
ii. A controller is a class that provides the implementation of org.springframework.web.servlet.mvc.Controller abstruct interface.

package pack;
public class HellowWorldController extends AbstructController {
protected ModelAndView handleRequestInternal (HttpServletRequest reuest,
HttpServletResponse response) throws Exception {
ModelAndView mav=new ModelAndView();
Return mav;

Note: HellowWorldController class is the controller class here that is indirectly implemented controller interface.

No comments:

Post a Comment