This is the 12th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021 “.

👨🎓 author: Java Academic Conference

🏦 Storage: Github, Gitee

✏️ blogs: CSDN, Nuggets, InfoQ, Cloud + Community

💌 public account: Java Academic Party

🚫 special statement: the original is not easy, shall not be reproduced or copied without authorization, if you need to reproduce can contact xiaobian authorization.

🙏 Copyright notice: part of the text or pictures in the article come from the Internet and Baidu Encyclopedia, if there is infringement, please contact xiaobian as soon as possible. Wechat search public Java academic party contact xiaobian.

☠️ Daily toxic chicken soup: smile embrace every day, be like sunflower warm woman.

👋 Hello! I’m your old friend Java Academic Party. Recently xiaobian in the whole Spring family bucket notes, notes will be issued regularly every day, like the big guys welcome to collect points like attention yo. Xiaobian will share it every day. Today we bring you a new framework technology, SpringMVC.

Spring MVC is a successor to the Spring Framework and has been integrated into Spring Web Flow. The Spring framework provides a full-featured MVC module for building Web applications. Use Spring’s pluggable MVC architecture so that you can choose to use Spring’s Spring MVC framework or integrate with other MVC development frameworks when using Spring for WEB development.

Chapter 2 SpringMVC Annotation Development (key)

2.1@requestMapping Defines request rules

2.1.1 Specifying the module name

  • The @requestMapping annotation defines how the processor maps requests. This annotation can be annotated on methods or classes, but the meaning is different. Value The attribute value usually starts with a slash (/).
  • The value attribute of @requestMapping is used to define the URI of the matched request. However, the URI specified by the value attribute of an annotation has a different meaning in terms of method and class.
  • A class annotated by @Controller can define multiple processor methods. Of course, different processor methods match different URIs. These different URIs are specified in the @requestMapping value attribute annotated on the method. But if these requests have the same URI part, then those same URIs can be extracted from the @RequestMapping value attribute annotated on top of the class. This URI represents the name of the module. The request for the URI is relative to the root of the Web.
  • To put it another way, to access a processor-specified method, you must precede the method’s specified URI with the module name that precedes the processor class

Requestmapping-modelname project: RequestMapping-ModelName

Step 1: Modify the processor class MyController.

Step 2: Add the view page

  • Add some. JSP and other. JSP pages to/web-INF/JSP and delete the original welcome.jsp page. The interface in the WEB-INF directory is secure. You cannot enter the interface name in the URL address bar to obtain the interface name.

2.1.2 Definition of request submission method

  • In the case of @RequestMapping, there is a method attribute that limits the way the annotated method can submit requests. That is, the annotated method will only execute requests that satisfy the submission method specified by the method attribute.
  • The value of the Method attribute is a RequestMethod enumeration constant. Requestmethod. GET and requestMethod. POST are commonly used, indicating that the matching rules for the submission modes are GET and POST, respectively.

  • The above processor methods can only handle requests submitted through POST. The common request and submission modes of the client browser are as follows:

  • That is, as long as you specify that the handler method matches the request submission as POST, you specify how the request is sent: either a form request or an AJAX request. Other request methods are disabled.

The hyperlink request is GET, and only forms and AJAX can use POST requests.

  • Of course, if method is not specified, both GET and POST submissions will match. That is, there is no requirement on how the request is submitted.

Step 1: Modify the processor class MyController

Step 2: Modify the Index page

2.2 Processor method parameters

  • Processor methods can contain four classes of parameters that are automatically assigned by the system at system call time, meaning that the programmer can use them directly within the method.
  • HttpServletRequest
  • HttpServletResponse
  • HttpSession
  • The request parameters carried in the request

2.2.1 Receiving Parameters one by one

Just make sure the request parameter name is the same as the request processing method parameter name. You can get it directly at this point, and if it’s different, there’s a way to do it later.

Step 1: Modify the Index page

Step 2: Modify the processor class MyController

Step 3: Add the Show page

Add the show.jsp page under/web-INF/JSP.

2.2.2 Request Parameters are Garbled in Chinese

Chinese garbled characters exist only in POST requests, but not in GET requests.

  • If the previously received request parameters contain Chinese characters, Chinese garbled characters will occur. Spring provides a special character set filter for Chinese garbled characters in request parameters: Spring – web – 5.2.5. RELEASE. The jar org. Springframework. Web. CharacterEncodingFilter class under the filter bag.

(1) Solutions

  • By registering character set filters in web.xml, Spring’s request parameters can be garbled in Chinese. However, it is best to register this filter before other filters. Because filters are executed in the order in which they were registered.
  • This filter can be created by ourselves or by using the CharacterEncodingFilter provided in the framework.

(2) Source code analysis

Character set core method:

2.2.3 Correct the request parameter name @requestParam

  • RequestParam(” request parameter name “) specifies the name of the parameter in the REQUEST URL if the parameter name is not the same as the parameter name specified in the processing method. This annotation modifies the processor method parameters. The value property specifies the name of the request parameter.

Step 1: Modify the Index page

Change the parameter names in the form from the original ones.

Step 2: Modify the processor class MyController

The required attribute:

2.2.4 Receiving Object Parameters

  • Define the argument to a processor method as an object, as long as the request parameter name is the same as the attribute of the object.

Step 1: Define the entity class Student, which encapsulates the user’s request parameter values

Note: This entity class requires the presence of a no-argument constructor as well as set and GET methods.

Step 2: Modify the processor class MyController

Step 3: Modify the Show page

2.3 Return value of the processor method

Processor methods that use the @Controller annotation typically return values of one of four types:

  • The first: ModelAndView
  • The second type: String
  • Third: no return value void
  • Fourth: return a custom type object

Different return values are used depending on the situation.

2.3.1 returns ModelAndView

  • If, after processing, the processor method needs to jump to another resource, and data needs to be passed between the jumped resources, it is better for the processor method to return ModelAndView. Of course, to return a ModelAndView, you need to define a ModelAndView object in the processor method.
  • When used, if the processor method simply jumps without passing data, or just passes data without jumping to any resource (such as an Ajax asynchronous response to a page), then returning a ModelAndView will always have a surplus: either the Model or the View is redundant. That is, returning ModelAndView would not be appropriate at this point.

Returns the String 2.3.2

  • The string returned by the processor method can specify the logical view name, which can be translated into a physical view address by the view parser

Returns the logical view name of the internal resource

  • If you want to jump for internal resources, the parser can use InternalResourceViewResolver internal resources views the parser. The string returned by the processor method is the name of the page to jump to without the extension. This string is combined with prefix and suffix in the view parser to form the URI to access.

Modify the processor class MyController directly

Important: You can also return the physical view name of the resource directly. At this point, however, you no longer need to configure dropouts and dropouts in the view parser.

SpringMVC, MyBatis(SSM), MyBatis(SSM)

For the source code of the above project, click planet for free accessplanet(Github address) If you don’t have a Github partner. You can follow my wechat official account:Java academic lie prone, send SpringMVC, free to send everyone the project source code, the code is personally tested by the small editor, absolutely reliable. It’s free to use.