Step-by-step development of Spring MVC applications

Source: Internet
Author: User

Spring MVC Framework 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. To configure Spring MVC, you must configure Spring MVC's DispatcherServlet. Register all configurations in the web. xml file. Listing 1 shows how to configure sampleBankingServlet. Listing 1. configure 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 Spring application context from an XML file, the XML file name is followed by the servlet name-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 <servlet-mapping> <servlet-name> sampleBankingServlet <servlet-name> <url-pattern> *. jsp </url-pattern> </servlet-mapping> loads the configuration file under 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. Register 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 servlet context below. The contextConfigLocation/WEB-INF/sampleBanking-services.xml file represents the configuration and bean configuration for the sample banking application service. If you want to mount multiple configuration files, you can use commas as separators in the tag. Spring MVC sample banking 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. Under the view attribute configuration, 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 = "authenticationSe Rvice "/> </property> <property name =" accountServices "> <ref bean =" accountServices "/> </property> <property name =" formView "> <value> login </value> </property> <property name = "successView"> <value> accountdetail </value> </property> </bean> commandClass and commandName are marked in the active bean in 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 Spring MVC's view parser 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. As mentioned above, LoginBankController internally connects Spring 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. in the xml file, as discussed above. 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! To deploy the application, 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 that before, download the 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. After creating the Spring project folder, open it and copy the spring-banking Sub-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 startTomcat to start and deploy the Spring MVC sample application. To test an application, Open a Web browser and point to http: // localhost: atatport/springbanking and replace Tomcat port with the actual port running on the 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. After successfully logging on to the Spring MVC sample logon screen, the account details page shown in Figure 2 is displayed. Figure 2. spring MVC sample account details page concluding remarks I demonstrate 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.

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.