SpringMVC is an MVC framework similar to Struts2. In actual development, it receives the browser's request response, processes the data, and then returns the page for display, but it is much easier to get started than Struts2. . And due to the security issues exposed by Struts2, SpringMVC has become the preferred framework for most enterprises.
There are many encapsulated annotations in springMVC. Using these annotations can simplify our development. The following are some commonly used annotations:
1. @Controller
@Controller is used to mark a class, and the class marked with it is a SpringMVCController object. The dispatch processor will scan the method of the class using this annotation and detect whether the method is annotated with @RequestMapping. @Controller just defines a controller class, and the method annotated with @RequestMapping is the processor that actually handles the request. Simply using the @Controller mark on a class cannot truly say that it is a controller class of SpringMVC, because Spring does not recognize it at this time. So how can Spring recognize it? At this time, we need to hand over this controller class to Spring for management. There are two ways:
(1) Define the bean object of MyController in the SpringMVC configuration file.
(2) In the SpringMVC configuration file, tell Spring where to find the Controller marked @Controller.
2. @RequestMapping
The RequestMapping annotation has six attributes. Below we divide it into three categories for explanation
(1)value, method
value: specify The actual address of the request, the specified address can be URI Template mode;
method: Specify the method type of the request, GET, POST, PUT, DELETE, etc.;
value uri value can be the following three categories: ordinary specific Value; a type of value containing a certain variable; a type of value containing a regular expression;
(2)consumes, produces
consumes: Specifies the submission content type (Content-Type) for processing the request, for example application/json, text/html;
produces: Specifies the content type to be returned. It will be returned only if the (Accept) type in the request header contains the specified type;
(3)params, headers
params: This method will only process the request if it must contain certain parameter values.
headers: The specified request must contain certain specified header values in order for this method to process the request.
3.@Resource and @Autowired
@Resource and @Autowired are both used for bean injection. In fact, @Resource is not an annotation of Spring. Its package is javax. .annotation.Resource needs to be imported, but Spring supports the injection of this annotation, and both can be written on fields and setter methods. If both are written on the fields, then there is no need to write setter methods.
4.@PathVariable
is used to map the template variables in the request URL to the parameters of the function processing method, that is, take out the variables in the uri template as parameters.
5.@ResponseBody
Function: This annotation is used to convert the object returned by the Controller method into the specified format through the appropriate HttpMessageConverter, and then write it to the body of the Response object. data area.
Usage timing: Use it when the returned data is not a page with html tags, but data in some other format (such as json, xml, etc.)
6.@RestController
We often see some controllers implementing the REST API, just to serve JSON, XML or other custom type content. @RestController is used to create REST type controllers and @Controller types. @RestController is such a type, which prevents you from repeatedly writing @RequestMapping and @ResponseBody.
The above is the detailed content of What are springmvc annotations?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor