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 customize.
Spring's Web MVC framework is designed around dispatcherservlet, which distributes requests to the processing program, it also supports configurable handler ing, view parsing, local language, topic parsing, and file uploading. The default handler is a very simple Controller Interface, with only one method modelandview 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 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 tag (used to display output data) and the 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 XML file name 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 you start a web application, contextloaderservlet loads the spring configuration file. Listing 3 registers contextloaderservlet.
Listing 3. Registering contextloaderservlet
Context> servlet-Name> org. springframework. Web. Context. contextloaderservlet 1
The contextconfiglocation parameter defines the spring configuration file to be loaded, as shown in the following servlet context.
Contextconfiglocation/WEB-INF/sampleBanking-services.xml
The sampleBanking-services.xml file represents the configuration and bean configuration of the sample banking application service. If you want to mount multiple configuration files, you can use commas as separators 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.
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 attributes" section describes how to set authenticationservice
And accountservices connect 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
Jakarta-tomcat-5.0.28.exe and run it to install Tomcat to any location 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-folder to C: \ tomvat5.0 \ webapps. Spring-Banking
A 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 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
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. 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.
Download Sample Code