Easy application Development with Spring MVC

Source: Internet
Author: User
Tags http post tomcat server
Before you begin, download the source code for this article. See Resources Access Spring Framework and Tom CAT 5.0, running the samples requires them.
  
Spring MVC Framework
  
The Spring framework provides a full-featured MVC module for building WEB applications. Using spring's pluggable MVC architecture, you can choose to use a web framework such as the built-in Spring web framework or Struts. Through the policy interface, the Spring framework is highly configurable and contains a variety of view technologies, such as JavaServer Pages (JSP) technology, Velocity, Tiles, IText, and POI. The Spring MVC Framework does not know the views used, so you will not be forced to use only JSP technology. Spring MVC separates the roles of controllers, model objects, dispatchers, and handler objects, which makes them easier to customize.
  
The Spring Web MVC Framework is designed around Dispatcherservlet, which assigns requests to handlers with configurable handler mappings, view resolution, local language, topic resolution, and upload file support. The default handler is a very simple Controller interface, with only one method Modelandview HandleRequest (request, response). Spring provides a controller hierarchy that can derive subclasses. If your application needs to process user input forms, you can inherit Abstractformcontroller. If you need to process multiple-page input to a form, you can inherit Abstractwizardformcontroller.
  
The sample application helps to visually learn these features. The bank application allows users to retrieve their account information. In building a banking application, you can learn how to configure the Spring MVC framework and the view layer of the implementation framework, which includes JSTL tags (data for displaying output) and JavaServer Pages technology.
  
Configuring Spring MVC
  
To start building the sample application, configure the Dispatcherservlet for Spring MVC. Please register all configurations in the Web.xml file. Listing 1 shows how to configure the Samplebankingservlet.
  
Listing 1. Configuring the 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 is loaded from an XML file into the Spring application context, and the name of the XML file is followed by the name of the servlet plus-servlet. In this example, Dispatcherservlet loads the application context from the Samplebankingservlet-servlet.xml file.
  
Configure the URL of an application
  
The next step is to configure the URL that you want Samplebankingservlet to process. Again, register all of this information in the Web.xml.
  
Listing 2. Configure the URL you want to process
  
<servlet-mapping>
<servlet-name> samplebankingservlet<servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
  
Mount configuration file
  
Below, mount the configuration file. To do this, register your contextloaderlistener for the Servlet 2.3 specification or register Contextloaderservlet for the servlet 2.2 and below. In order to ensure back-to compatibility, please use Contextloaderservlet. When you start a WEB application, Contextloaderservlet loads the Spring configuration file. Listing 3 registers the Contextloaderservlet.
  
Listing 3. Register 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 mount, 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 an example of the configuration and bean configuration of the Bank application service. If you want to mount more than one configuration file, you can use commas as delimiters in the <param-value> tag.
  
Spring MVC Example
  
The sample banking application allows users to view account information based on unique IDs and passwords. Although Spring MVC offers other options, I'll use JSP technology as the View page. This simple application contains a view page for user input (ID and password), and another page that displays the user's account information.
  
I started with Loginbankcontroller, which expanded the simpleformcontroller of Spring MVC. Simpleformcontoller provides the ability to display forms received from HTTP GET requests, as well as the ability to handle the same form data that is received from HTTP POST. Loginbankcontroller uses AuthenticationService and accountservices services to authenticate and perform account activities. Listing 5 in the "Configuring View Properties" section describes how to connect AuthenticationService and accountservices to Loginbankcontroller. Listing 4 shows the code for Loginbankcontroller.
  
Configure view Properties
  
Below, I must register the page that is displayed when an HTTP GET request is received. I registered this page with the FormView attribute in the Spring configuration, as shown in Listing 5. The Sucessview property represents the page that is displayed after the form data is submitted and the logic in the Dosubmitaction () method is successfully executed. Both the FormView and Sucessview attributes represent the logical names of the views that are defined, 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 which beans will be active in the view page. For example, you can access the Logincommand bean from the login.jsp page, which is the login page for the application. Once the user submits the login page, the application can retrieve the form data from the Command object in the Loginbankcontroller OnSubmit () method.
  
View Parser
  
The view parser for Spring MVC resolves each logical name into the actual resource, the JSP file that contains the account information. I'm using Spring's Internalresourceviewresolver, as shown in Listing 6.
  
Because I use the JSTL tag in the JSP page, the user's login name resolves into a resource/jsp/login.jsp, and Viewclass becomes Jstlview.
  
Authentication and Account Services
  
As mentioned earlier, Loginbankcontroller is connected to Spring's accountservices and AuthenticationService. The AuthenticationService class handles validation of bank applications. The Accountservices class deals with typical banking services, such as locating transactions and telegraphic transfers. Listing 7 shows the authentication of the banking application and the configuration of the account service.
  
Listing 7. Configuring authentication and Account Services
  
<beans>
  
<bean id= "Accountservices"
class= "Springexample.services.AccountServices" >
  
</bean>
  
<bean id= "AuthenticationService"
class= "Springexample.services.AuthenticationService" >
  
</bean>
  
</beans>
  
The above services are registered in Samplebanking-services.xml and then loaded into the Web.xml file, as discussed earlier. Once the controller and service are configured, this simple application is complete. Now let's take a look at what happens when you deploy and test it!
  
deploying applications
  
I deployed the sample application in the Tomcat servlet container. Tomcat is the servlet container used in the official reference implementations 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 anywhere you like, such as c:/tomcat5.0.
  
Next, download the sample code and release it to the drive (for example, c:/ On After you create a folder for the Spring project, open it and copy the Spring-banking subfolder to C:/tomvat5.0/webapps. The Spring-banking folder is a WEB archive containing the Spring MVC sample application. The Lib folder contains the spring framework that the application needs, the MVC libraries associated with Spring, and the JSTL tag libraries and jar files.
  
To start the TOMCAT server, use the following command:
  
CD bin C:/tomcat 5.0/bin> catalina.bat start
Tomcat should start and deploy the Spring MVC sample application.
  
Testing the Application
  
To test your application, open your Web browser, point to http://localhost:tomcatport/springbanking, and replace Tomcatport with the port that the Tomcat server actually runs. You should see the login screen shown in Figure 1. Enter the user ID "admin" and the password "password" and press the login button. Other user IDs or passwords can cause errors from the authentication service.
  
Figure 1. Spring MVC Sample Login screen


After the login is successful, you will see the Account Details page shown in Figure 2.
  
Figure 2. Spring MVC Sample Account Detail Page
  


Conclusion
  
In this article, I introduce the characteristics of the Spring MVC framework. I demonstrated how to configure and develop the Spring MVC application, how to configure the Spring MVC controller and insert dependencies into it, how to develop an application view with JavaServer pages technology, and how to integrate your own pages with the view layer of Spring mvc. In summarizing this article, 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.