Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring-MVC

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use Spring-MVC. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Introduction

1.MVC: Model-View-Control

The main tasks of the C layer of the framework are as follows: encapsulating the web request as a data object, calling the business logic layer to deal with the data object, returning the processing data results and the corresponding view to the user.

two。 A brief overview of springmvc

At the heart of the Spring C-tier framework is DispatcherServlet, which distributes requests to different back-end processors, using a pattern called Front Controller (which is briefly described later). Spring's C-tier framework uses a back-end controller, a mapping processor and a view parser to complete the main work of the C-tier framework. And the C-tier framework of spring really combines the data results processed by the business layer with the corresponding views into an object, that is, the ModelAndView object that we will often use later.

I. an example of getting started

1. Build an environment

In the official API document of spring, give an overview of the functions of all packages, and now list the commonly used packages and related functions:

Org.springframework.aop-3.0.5.RELEASE.jar: packages related to Aop programming

Org.springframework.beans-3.0.5.RELEASE.jar: provides a simple interface to operate bean

Org.springframework.context-3.0.5.RELEASE.jar: built on the beans package to handle resource files and internationalization.

Org.springframework.core-3.0.5.RELEASE.jar: spring core package

Org.springframework.web-3.0.5.RELEASE.jar: web core package, which provides web layer interface

Org.springframework.web.servlet-3.0.5.RELEASE.jar: a concrete implementation package of the web layer, DispatcherServlet is also located in this package.

For convenience, it is recommended to import all jar packages of spring3.0 in the build environment (all jar packages are located in the dist directory).

two。 Write a HelloWorld instance

Step 1. Create the name springMVC_01_helloword and import the jar package listed above.

Step 2. Write the web.xml configuration file with the following code:

Java code

Xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> spmvc Class > org.springframework.web.servlet.DispatcherServlet class > 1 spmvc * .do

Brief description: DispatcherServlet is a Servlet and the core Servlet for forwarding requests. In this case, all .do requests will be processed by DispatcherServlet first, and DispatcherServlet's job is to distribute the request (that is, forward the request to a specific Controller). It can be simply thought of as a master processor, but in fact, in addition to the ability of the master processor to distribute requests, it is also fully integrated with spring's IOC container, so that it can make better use of other functions of spring. We still need to pay attention to here.

< servlet-name >

Spmvc, which will be used in step 3 below.

Step 3, establish the spmvc-servlet.xml file, its naming rule: servlet-name-servlet.xml. Its main code is as follows:

Xml code

> helloControlprop > props > property > bean > bean > beans >

Note: the request for hello.do will be processed by the bean named helloControl.

Step 4. Complete the writing of HelloWord.java. The code is as follows:

Java code

Package com.asm; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloWord implements Controller {public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws Exception {ModelAndView mav = new ModelAndView ("hello.jsp"); mav.addObject ("message", "Hello World!") Return mav;}}

Note: ModelAndView object is a mixed object containing view and business data, that is, through this object, we can know the corresponding page returned (for example, the hello.jsp page is returned here), and we can also obtain the business data contained in this object in the corresponding page (such as message-hello worrld here).

Step 5. Write hello.jsp under the web root directory of the current project. The main code is as follows:

< body >

Hello, World!

Get value: ${message}

Step 6: enter... / hello.do to test.

4. A brief Analysis of the working principle of spring mvc

(1) start the server and load the front-end controller (also known as the master controller) DispatcherServlet according to the configuration of web.xml. During loading, a series of initialization actions are completed.

(2) according to the mapping request of servlet (for the .do request in the above helloWorld example), and referring to the "controller configuration file" (that is, the configuration file such as spmvc-servlet.xml), distribute the specific request to a specific backend controller for processing (for example, the above example will be distributed to the HelloWorld controller for processing)

(3) the back-end controller calls the corresponding logic layer code, completes the processing and returns the view object (ModelAndView) to the front-end processor.

(4) the front-end controller returns a corresponding page to the client according to the ModelAndView object returned by the back-end controller and combined with some configurations (described later).

Summary: this Front Controller mode is often used in mainstream web frameworks, such as the typical struts1.x framework .Front Controller mode: all requests are first handed over to a front-end processor (master processor), and then the front-end processor will refer to some configuration files and send specific requests to the corresponding back-end processors. The back-end processor calls the logic layer code and returns the corresponding view object to the front-end controller according to the logic. The front-end controller then returns the specific page to the client based on the view object (hint: like spring mvc, the front-end controller is Servlet in struts1.x and Filter in struts2). An overview of Front Controller mode: the front-end controller preprocesses and distributes requests to the back-end controller, the back-end controller carries out real logical processing and returns the view object, and the front-end controller returns the specific page to the client according to the view object.

5. Get to know the view of spring mvc

In the previous HelloWorld example, the ModelAndView mav = new ModelAndView ("hello.jsp") parameter returned in HelloWorld.java is hello.jsp, which corresponds to the hello.jsp page under the current project root. However, spring mvc provides us with a special way to locate views. The following improvements are made to the previous HelloWord example:

Improvement 1: add the following code to spmvc-servlet.xml:

Xml code

Bean >

Improvement 2: redefine the returned ModelAndView object in HelloWorld.java, that is, change ModelAndView mav = new ModelAndView ("hello.jsp") to ModelAndView mav = new ModelAndView ("hello")

Improvement 3: create a hello.jsp page in the / WEB-INF/page directory

After making the above three improvements, revisiting hello.do will access the WEB-INF/page/hello.jsp page.

Parse the view positioning: when the ModelAndView object name is hello, the hello will be prefixed to become

/ WEB-INF/page/hello.jsp . Therefore, when assigning a value to a prefix, you should pay special attention to whether it and the returned ModelAndView object can form a correct full path to the file. In the previous "A brief Analysis of how spring mvc works (4)" point, it is mentioned that some configurations will be combined when returning a page based on a ModelAndView object. Here is the combination of the view positioning method, to viewName with a suffix for positioning.

Thank you for reading! This is the end of the article on "how to use Spring-MVC". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report