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>
org.springframework.web.context.ContexLoaderListener
< /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”
class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
< 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>
ContextLoaderListener
---------------------------------
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
Example
---------------
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
}
}
DispatcherServlet
---------------------------
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.
Example
--------------------
package pack;
public class HellowWorldController extends AbstructController {
protected ModelAndView handleRequestInternal (HttpServletRequest reuest,
HttpServletResponse response) throws Exception {
ModelAndView mav=new ModelAndView();
Mav.setViewName(“one”);
Return mav;
}
}
Note: HellowWorldController class is the controller class here that is indirectly implemented controller interface.
No comments:
Post a Comment