1. @Controller
In SPRINGMVC, controller controllers are responsible for processing requests distributed by Dispatcherservlet, which encapsulates the data requested by the user into a model after processing it through the business processing layer, and then returns the model to the corresponding view for presentation.
A very simple way to define a controller is provided in SPRINGMVC, where you do not need to inherit a particular class or implement a specific interface, just use @controller to mark a class as a controller and then use the @requestmapping And @requestparam some annotations are used to define the mapping between URL requests and Controller methods, so that the controller can be accessed by outsiders. In addition, the controller does not directly depend on HttpServlet objects such as HttpServletRequest and HttpServletResponse, which can be obtained flexibly through the controller's method parameters.
@Controller used to mark a class, the class marked with it is a Springmvc Controller object. The distribution processor scans the method of the class that used the annotation and detects whether the method uses @requestmapping annotations.
@Controller just defines a controller class,
The way to use @requestmapping annotations is to actually process the requested processor.
Simply using the @controller tag on a class does not really mean that it is a controller class for SPRINGMVC, because this time spring does not know it yet.
So how to do spring to know it. This time we need to give this controller class to spring to manage. There are two ways of doing this:
(1) Define the Mycontroller Bean object in the SPRINGMVC configuration file.
(2) in the SPRINGMVC configuration file, tell spring where to find the controller controllers marked as @Controller.
<!--Way---
<bean class= "Com.host.app.web.controller.MyController"/>
<!--mode two
-- < Context:component-scan base-package = "Com.host.app.web"/>//path is written to the upper layer of the controller (see below for a detailed description of the scan package)
2. @RequestMapping
Requestmapping is an annotation that handles request address mappings and can be used on classes or methods. On a class, the method that represents all response requests in a class is the parent path of the address.
The requestmapping annotation has six properties, and we'll describe her in three categories below (there are examples below).
1, value, method;
Value: Specifies the actual address of the request, the specified address can be the URI Template mode (which will be explained later);
Method: Specifies the type of method requested, GET, POST, PUT, delete, and so on;
2, Consumes,produces
Consumes: Specifies the type of submission to process the request (Content-type), such as Application/json, text/html;
Produces: Specifies the type of content returned, only if the specified type is included in the (Accept) type in the request header;
3, Params,headers
Params: Specifies that some parameter values must be included in the request before the method is processed.
Headers: Specifies that certain header values must be included in the request in order for the method to process requests. 3, @Resource and @autowired
Both @Resource and @autowired are used as bean injections, in fact @resource is not a spring annotation, its package is javax.annotation.Resource and needs to be imported, but spring supports the annotation injection.
1. Common Ground
Both can be written on the field and setter methods. If both are written on a field, then you do not need to write the setter method.
2, different points
(1) @Autowired: Inject only according to Bytype.
@Autowired the annotations provided for spring, the package org.springframework.beans.factory.annotation.Autowired needs to be imported and only injected as Bytype.
public class Testserviceimpl {
//below two kinds of @autowired as long as
the
use of one can be @Autowired private Userdao Userdao;//For fields @Autowired public
void Setuserdao (Userdao Userdao) {//For the method of the property
This.userdao = Userdao;
}
}
@Autowired annotation is an assembly of dependent objects by type (Bytype), by default it requires that the dependent object must exist, and if null values are allowed, you can set its Required property to false. If we want to assemble by name (byname), we can use it in conjunction with @qualifier annotations. As follows:
public class Testserviceimpl {
@Autowired
@Qualifier ("Userdao")
private Userdao Userdao;
}
(2) @Resource: byname Automatic injection
The @Resource is automatically injected by ByName by default, provided by the Java EE and needs to import the package Javax.annotation.Resource. @Resource has two important properties: Name and type, and spring resolves the name property of the @resource annotation to the name of the bean, while the type attribute resolves to the bean. Therefore, if you use the Name property, the ByName auto-injection policy is used, and the type attribute is used when you use the Bytype auto-injection policy. If neither the name nor the type attribute is established, then the ByName auto-injection strategy is used through the reflection mechanism.
public class Testserviceimpl {
//below two types of @resource
@Resource (name= "Userdao")
private Userdao as long as one is used Userdao; Used on field
@Resource (name= "Userdao") public
void Setuserdao (Userdao Userdao) {//Setter method
for properties This.userdao = Userdao;
}
}
Note: It is best to put @resource on the setter method, because it is more in line with object-oriented thinking, through set, get to manipulate properties, rather than directly to manipulate properties.
@Resource Assembly Order:
① if both name and type are specified, the uniquely matched bean is found from the spring context to assemble, and an exception is thrown if it is not found.
② If name is specified, the matching bean from the context looking for the name (ID) is assembled, and an exception is thrown if it is not found.
③ if type is specified, an exception is thrown when a unique bean similar to a match is found in the context for assembly, not found or multiple found.
④ If neither name is specified and type is not specified, the assembly is automatically byname, and if there is no match, the fallback is a match for the original type and is automatically assembled if matched.
The function of @Resource is equivalent to @autowired, but @autowired is injected automatically according to Bytype. 4. @ModelAttribute and @SessionAttributes
Represented: All methods of the controller execute this @modelattribute method before calling, can be used in annotation and method parameter, can apply this @modelattribute attribute in Basecontroller, All controllers inherit Basecontroller, enabling the @modelattribute method to be executed when the controller is called.
@SessionAttributes the value to be placed in the session scope and written on the class.
See below for specific examples: using @ModelAttribute and @SessionAttributes to pass and save data 5, @PathVariable
Used to map the template variables in the request URL to the parameters of a feature processing method, that is, the variables in the URI template are taken as parameters. Such as:
@Controller public class TestController {@RequestMapping (value="/user/{userid}/ro
Les/{roleid} ", method = requestmethod.get) public string GetLogin (@PathVariable (" userid ") string userId,
@PathVariable ("Roleid") String Roleid) {System.out.println ("User Id:" + userId);
System.out.println ("Role Id:" + Roleid);
return "Hello"; } @RequestMapping (value= "/product/{productid}", method = requestmethod.get) public String getproduct (@PathVar
Iable ("ProductId") String productId) {System.out.println ("Product Id:" + productId);
return "Hello"; } @RequestMapping (value= "/javabeat/{regexp1:[a-z-]+}", method = requestmethod.get) public Strin
G Getregexp (@PathVariable ("Regexp1") String regexp1) {System.out.println ("URI Part 1:" + REGEXP1);
return "Hello"; }
}
6. @requestParam
@requestParam is primarily used to get parameters in the SPRINGMVC background control layer, similar to Request.getparameter ("name"), which has three common parameters: DefaultValue = "0", required = False, value = "Isapp"; defaultvalue indicates the default value is set, required the copper over Boolean setting is the parameter that must be passed in, and the value represents the type of incoming parameter that is accepted. 7. @ResponseBody
Function: The annotation is used to write the object returned by the controller's method to the body data area of the response object after the appropriate httpmessageconverter is converted to the specified format. </