Easy Application Development with spring MVC

Source: Internet
Author: User
Spring MVC framework. The bank example shows 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.

Before you start, download the source code of this article. For more information, see accessing the Spring framework and tomcat 5.0.
  
Spring MVC Framework
  
The Spring framework provides a full-featured MVC module for Building Web applications. Using spring-pluggable MVC Architecture, you can choose whether to use a built-in spring Web framework or a Web framework such as struts. Through the policy interface, the Spring framework is highly configurable and contains multiple view technologies, such as assumerver pages (JSP), velocity, tiles, itext, and poi. The spring MVC Framework does not know the view used, so it does not force you to only use JSP technology. Spring MVC separates the roles of controllers, model objects, schedulers, and processing program objects, making them easier to define.
  
Spring's Web MVC framework is designed around dispatcherservlet, which distributes requests to the processing program, supports configurable handler ing, view parsing, local language, topic parsing, and file uploading at the same time. The default handler is a very simple Controller Interface, with only one method modelandview handlerequest (request, response ). Spring provides a controller hierarchy that can dispatch child classes. If the application needs to process the user input form, it can inherit abstractformcontroller. If you need to process multi-page input to a form, you can inherit abstractwizardformcontroller.
  
The sample application helps you learn these features intuitively. The Banking application allows users to retrieve their account information. When building a bank application, you can learn how to configure the spring MVC Framework and the view layer of the implementation framework. The view layer includes the jstl mark (used to display the output data) and assumerver Pages technology.
  
Configure spring MVC
  
To start building the sample application, configure spring MVC's dispatcherservlet. Register all configurations in the web. xml file. Listing 1 shows how to configure samplebankingservlet.
  
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 loads the spring application context from an XML file. The name of the XML file is the servlet name followed by-servlet. In this example, dispatcherservlet loads the application context from the samplebankingservlet-servlet. xml file.
  
Configure the application URL
  
The next step is to configure the URL to be processed by samplebankingservlet. Similarly, you must register all the information in Web. 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 do this, register the contextloaderlistener for the servlet 2.3 specification or register the contextloaderservlet for the container of servlet 2.2 and below. To ensure backward compatibility, use contextloaderservlet. When the web application is started, contextloaderservlet loads the spring configuration file. Listing 3 registers contextloaderservlet.
  
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>
  
The contextconfiglocation parameter defines the spring configuration file to be loaded, as shown in the following servlet context.
  
<Context-param>
<Param-value> contextconfiglocation </param-value>
<Param-value>/WEB-INF/sampleBanking-services.xml </param-value>
</Context-param>
  
The sampleBanking-services.xml file represents the configuration and bean configuration of the sample banking application service. To load multiple configuration files, use a comma as the Separator in the <param-value> 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.
  
I started from loginbankcontroller and it expanded spring MVC simpleformcontroller. Simpleformcontoller provides the function of displaying the form received from the http get request and processing the same form data received from the http post. Loginbankcontroller authenticates with authenticationservice and accountservices and executes account activities. Listing 5 in the "Configure View Properties" section describes how to connect authenticationservice and accountservices to loginbankcontroller. Listing 4 shows the code of loginbankcontroller.
  
Configure view attributes
  
Below, I must register the page displayed when an HTTP GET request is received. Register this page with the formview attribute in spring configuration, as shown in listing 5. The sucessview attribute indicates the page displayed after the form data is submitted and the logic in the dosubmitaction () method is successfully executed. The formview and sucessview attributes both represent the logical names of the defined views, and 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>
  
The commandclass and commandname tags determine the bean that will be active on the view page. For example, you can access logincommand bean through the login. jsp page, which is the login page of the application. Once the user submits the logon page, the application can retrieve form data from the command object in the onsubmit () method of loginbankcontroller.
  
View parser
  
The view parser of spring MVC parses each logic name into actual resources, that is, JSP files containing account information. I use spring internalresourceviewresolver, as shown in Listing 6.
  
Because I used the jstl tag on the JSP page, the user's login name is parsed into resource/JSP/login. jsp, and viewclass is jstlview.
  
Verification and Account Service
  
As mentioned above, loginbankcontroller internally connects spring's accountservices and authenticationservice. Authenticationservice class handles the verification of bank applications. Accountservices handles typical banking services, such as transaction searches 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 registered in the sampleBanking-services.xml and then loaded into the web. 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 wherever you like, such as C:/tomcat5.0.
  
Next, download the sample code and release it to the drive (for example, C. After creating the spring project folder, open it and copy the spring-Banking Sub-file folder to C:/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: atatport/springbanking and replace Tomcat port with the actual running port of Tomcat server. 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 the third article of the three-part Spring Series, I introduced the features of the spring MVC framework. I demonstrated how to configure and Develop Spring MVC applications, how to configure spring MVC controllers and insert dependencies into them, and how to develop application views using the assumerver Pages technology, and how to integrate your page with the view layer of spring MVC. In conclusion, I demonstrated how to deploy an application in the Tomcat servlet container and how to test it in a browser.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.