|
<Tr Valign = "TOP"> <TD width = "8"> Src = "// www.ibm.com/ I /c.gif"/> </TD> <TD width = "16"> Height = "16" src = "// www.ibm.com/ I /c.gif"/> </TD> <TD class = "small" Width = "122"> <p> <SPAN class = "Ast"> Javascript is not displayed </Span> </P> </TD> </tr>
|
|
Print this page
|
|
|
Send this page as an email
|
|
|
Discussion
|
|
|
Sample Code
|
|
|
Level: Intermediate Naveen balani Naveenbalani@rediffmail.com ), Technical architect, webify Solutions October 13, 2005
In Part 1 of Naveen balani's Spring Series, learn how to use the Spring framework to develop MVC-based applications.
In Spring Series
In section 3rd, I will introduce the spring MVC framework. Just as in my previous article, I used banking examples to show you how to model and build simple applications. The sample application contains some technologies (such as dependency injection) that have been learned, but mainly demonstrates the features of spring MVC. Download the source code of this article before you start. . See references Visit the Spring framework and tomcat 5.0. They are required for running examples. Spring MVC Framework
Spring The framework provides a full-featured MVC module for Building Web applications. You can choose to use the built-in spring Web The framework is also a Web framework like struts. Through the policy interface, the Spring framework is highly configurable and contains multiple view technologies, such as assumerver Pages (JSP) technology, velocity, tiles, itext and poi. Spring MVC The framework does not know the view used, so it does not force you to only use JSP technology. Spring MVC The roles of controllers, model objects, dispatchers, and handler objects are separated, making them easier to customize. Spring web MVC framework is centered aroundDispatcherServlet Designed to distribute requests to the processing program, with configurable handler ing, view resolution, local language, topic resolution, and file upload support. The default handler is very simple.Controller Interface, only one methodModelAndView handleRequest(request, response) . Spring provides a controller hierarchy that can be derived from child classes. If the application needs to process the user input form, it can inheritAbstractFormController . If you need to process multi-page input to a form, you can inheritAbstractWizardFormController . Display The sample application helps you learn these features intuitively. The Banking application allows users to retrieve their account information. You can learn how to configure spring MVC when building a bank application. The view layer of the framework and implementation framework. The view layer includes the jstl mark (used to display output data) and the assumerver Pages technology.
Configure spring MVC
To start building the sample application, configure the spring MVCDispatcherServlet . Please goWeb. xml File. Listing 1 shows how to configuresampleBankingServlet . Listing 1. configuring spring MVC dispatcherservlet
<servlet> <servlet-name>sampleBankingServlet</servlet-name> <servlet-class> org.springframework.we.servlet.DispatcherServlet <servlet-class> <load-on-startup>1<load-on-startup> <servlet>
|
DispatcherServlet Load the spring application context from an XML file. The XML file name is added after the servlet name.-Servlet . In this example,DispatcherServlet FromSampleBankingServlet-servlet.xml File Load application context.
Configure the application URL
The next step is to configuresampleBankingServlet The URL to be processed. SimilarlyWeb. xml . List 2. Configure the URL to be processed
<servlet-mapping> <servlet-name> sampleBankingServlet<servlet-name> <url-pattern>*.jsp</url-pattern> </servlet-mapping>
|
Load the configuration file
Next, load the configuration file. To achieve this, register for the servlet 2.3 StandardContextLoaderListener Or register for containers of servlet 2.2 or lower.ContextLoaderServlet . To ensure backward compatibility, useContextLoaderServlet . When starting a web application,ContextLoaderServlet Load the spring configuration file. Listing 3 registeredContextLoaderServlet . Listing 3. Registering contextloaderservlet
<servlet> <servlet-name>context>servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>
|
contextConfigLocation The parameter defines the spring configuration file to be loaded, as shown in the servlet context below.
<context-param> <param-value>contextConfigLocation</param-value> <param-value>/WEB-INF/sampleBanking-services.xml</param-value> </context-param>
|
SampleBanking-services.xml The file represents the configuration of the sample banking Application Service and bean configuration. If you want to mount multiple configuration files, you can<param-value> Use a comma as the Separator in the tag.
Spring MVC example
The example bank application allows you to view account information based on a unique ID and password. Although spring MVC provides other options, I will use JSP technology as the view page. This simple application contains a view page for user input (ID and password), and the other page displays the user's account information. FromLoginBankController It expands spring MVC'sSimpleFormController .SimpleFormContoller ProvidesHTTP GET The function of the form received by the request, and the process fromHTTP POST The function of receiving the same form data.LoginBankController UseAuthenticationService AndAccountServices Service to verify and execute account activities. "Configure view attributes "Listing 5 in the Section Describes howAuthenticationService AndAccountServices ConnectLoginBankController . Listing 4 ShownLoginBankController . Configure view attributes
Below, I must registerHTTP GET Page displayed during request. Used in spring ConfigurationformView Property registration page, as shown in listing 5.sucessView Attribute indicates that the form data is submitted anddoSubmitAction() Page displayed after the logic in the method is successfully executed.formView AndsucessView All attributes represent the logical names of the defined views. The logical names are mapped to the actual view pages. Listing 5. Register loginbankcontroller
<bean id="loginBankController" class="springexample.controller.LoginBankController"> <property name="sessionForm"><value>true</value></property> <property name="commandName"><value>loginCommand</value></property> <property name="commandClass"> <value>springexample.commands.LoginCommand</value> </property> <property name="authenticationService"> <ref bean="authenticationService" /> </property> <property name="accountServices"> <ref bean="accountServices" /> </property> <property name="formView"> <value>login</value> </property> <property name="successView"> <value>accountdetail</value> </property> </bean>
|
commandClass AndcommandName Tag determines the bean to be active in the view page. For example, you can useLogin. jsp Page accessloginCommand Bean, which is the login page of the application. Once the user submits the login page, the application canLoginBankController OfonSubmit() Method.
View parser
Spring MVCView parser Resolve each logic name to the actual resource, that is, the JSP file containing account information. I use spring.InternalResourceViewResolver , As shown in Listing 6. . Because I used the jstl tag in the JSP page, the user login name is parsed into a resource/JSP/login. jsp , AndviewClass BecomeJstlView . Verification and Account Service
As mentioned above,LoginBankController Internally connected to springAccountServices AndAuthenticationService .AuthenticationService Class handles the verification of bank applications.AccountServices Class handles typical banking services, such as searching for transactions and wire transfers. Listing 7 shows the authentication of the banking application and the configuration of the Account Service. Listing 7. Configuration verification and Account Service
<beans> <bean id="accountServices" class="springexample.services.AccountServices"> </bean> <bean id="authenticationService" class="springexample.services.AuthenticationService"> </bean> </beans>
|
The above services are available inSampleBanking-services.xml And then loadWeb. xml File, as discussed earlier . After the controller and service are configured, this simple application is complete. Now let's take a look at what will happen when deploying and testing it!
Deploy applications
I deploy the sample application in the Tomcat servlet container. Tomcat is the servlet container used in implementation for official reference of Java Servlet and Java serverpagest technology. If you haven't done this before, download the jakarta-tomcat-5.0.28.exe And run it to install Tomcat to any location you like, suchc:/tomcat5.0 . Next, download the sample code And release it to the drive (for exampleC :/ . After creating the spring project folder, open it andSpring-Banking Copy subfoldersC:/tomvat5.0/webapps . The spring-banking folder is a Web file containing the spring MVC sample application. The LIB folder contains the Spring framework required by the application, spring-related MVC libraries, jstl tag libraries, and jar files. To start the Tomcat server, run the following command: cd bin C:/Tomcat 5.0/bin> catalina.bat start
Tomcat should start and deploy the spring MVC sample application.
Test the application
To test the application, Open a Web browser and point to http: // localhost:Tomcatport /Springbanking and replace it with the actual running port of the Tomcat serverTomcatport . The logon screen shown in Figure 1 is displayed. Enter the user ID "admin" and password "password", and press the logon button. Other user IDs or passwords may cause errors from the authentication service. Figure 1. Spring MVC sample logon screen
After successful logon, the account details page shown in Figure 2 is displayed. Figure 2. Spring MVC sample account details page
Conclusion
In Spring Series
In the third article, I introduced the features of spring MVC framework. I demonstrated how to configure and Develop Spring MVC applications and how to configure spring MVC controller and insert dependencies into it, how to use the assumerver Pages technology to develop application views, and how to integrate your pages with spring MVC View layer integration. To sum up this article, I demonstrated how to deploy an application in a Tomcat servlet container and how to test it in a browser. Continue to followSpring Series . In the next article, I will introduce how to integrate JMS-based applications with the Spring framework. For more information, see Learn more about Spring framework and spring MVC.
Download
Description |
Name |
Size |
Download Method |
Example source code, spring files, Jar files |
Wa-spring3-SpringProjectPart3.zip |
1966 KB |
HTTP
|
|
|
Information about the Download Method |
|
References
Learning
- For more information, see the original article on the developerworks global site.
.
- "Spring Series, Part 1: Spring framework Introduction
"(Developerworks, 2nd) and" Spring Series, Part 1: When hibernate encounters spring "(Developerworks, August 2005): describes how to use spring technology to build lightweight, robust J2EE applications and how to integrate hibernate transactions with spring's Aspect-oriented programming (AOP) integrated to form a reliable persistent framework.
- "AOP @ work: Comparison of AOP tools, part 1
"(Mik Kersten, developerworks, February 2005): Compares Spring AOP and AOP frameworks.
- "The secret to the success of lightweight Development, Part 1: Spring exposed
"(Bruce Tate, developerworks, June 2005): demonstrates the significance of spring as a lightweight container.
- "Ruby on Rails and J2EE: Can they coexist?
"(Aaron rustad, developerworks, September July 2005): Compares the less-known J2EE framework alternatives, such as EJB and spring.
- Java Technology Zone
: There are hundreds of articles covering Java Web-based solutions.
Obtain products and technologies
- Spring Homepage
: Download the Spring framework.
- Ant Homepage
: Download Apache ant.
Discussion
- Participate in Forum Discussion
.
- Developerworks
Blogs : Join the developerworks community.
|