Spring mvc common annotations

Common annotations of spring mvc:

an introduction.
@Controller
@Controller is responsible for registering a bean into the spring context. The bean ID defaults to the

class name starting with lowercase letters. You can also specify it yourself, as follows
Method 1:
@Controller
public class TestController {}
 
Method 2:           
@Controller("tmpController" )
public class TestController {}
 
@RequestMapping
 
1. @RequestMapping is used to define the access URL, you can define a

@RequestMapping for the entire class, or specify one for each method.
Putting @RequestMapping at the class level allows it to

work in tandem with the @RequestMapping annotation at the method level and has the effect of narrowing down the choices.
For example:
@RequestMapping("/test")
public class TestController {}
Then, all access paths under this class are under /test.
 
2. It is not necessary to use @RequestMapping for the entire class. If there is no configuration,

the access path configuration of all methods will be completely independent without any association.
 
3. The complete parameter item is: @RequestMapping(value="",method =

{"",""},headers={},params={"",""}), the description of each parameter is as follows:
value :String [] Set access address
method: RequestMethod[] Set access method, character array, view RequestMethod

class, including GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, commonly used

RequestMethod.GET, RequestMethod.POST
headers: String[] headers Generally combined with method = RequestMethod.POST, use
params: String[] to access parameter settings, character arrays such as: userId=id
 
4. The configuration of value can also be in the form of template variables, such as: @RequestMapping

(value="/owners/{ownerId }", method=RequestMethod.GET), which will be explained in detail in the

introduction to @PathVariable.
 
5. Supplementary description of @RequestMapping params, you can restrict the

access address by setting parameter conditions, such as params="myParam=myValue" expression, the parameters in the access address can only

match the specified value "myParam=myValue". on, something like "myParam"



Any), expressions such as "!myParam" indicate that the address of the current request cannot contain the specified

parameter "myParam".
 
6. One thing to pay attention to, if the access address is defined for the class as *.do, *.html, etc.,

in the method-level @RequestMapping, the value value cannot be defined, otherwise an error will be reported, such as the
Java code 
@RequestMapping ("/bbs.do") 
public class BbsController { 
    @RequestMapping(params = "method=getList") 
    public String getList() { 
     return "list"; 
    } 
@RequestMapping(value= "/spList") 
public String getSpecialList() { 
     return "splist"; 
    } 
}  As the
 
above example: /bbs.do?method=getList can access the method getList(); but

accessing /bbs.do/spList will report an error.
 
@PathVariable
1.

@PathVariable is used for parameters in methods and represents a template variable for which method parameters are bound to the address URL.
For example:
Java code 
@RequestMapping(value="/owners/{ownerId}",

method=RequestMethod.GET) 
public String findOwner(@PathVariable String ownerId, Model

model) { 
  Owner owner = ownerService.findOwner(ownerId);   
  model.addAttribute("owner" , owner);   
  return "displayOwner"; 

 
2. @PathVariable is used when the address bar uses {xxx} template variables.
If @RequestMapping does not define such a variable as "/{ownerId}", using

@PathVariable in the method will report an error.
 
 
@ModelAttribute
1. Applied to method parameters, the parameters can be obtained directly on the page, equivalent to

request.setAttribute(,)
2. Applied to methods, mark any method with a return value with @ModelAttribute,

so that return value will enter 3. When applied to
method parameters, @ModelAttribute("xx") must be associated with the data type

of Object. Basic data types such as int and String do not work.
For example :
Java code 
@ModelAttribute("items")//<——①Add an

attribute  named items to the model object
public List<String> populateItems() { 
        List<String> lists = new ArrayList<String>(); 
        lists.add ("item1"); 
        lists.add("item2"); 
        return lists; 

@RequestMapping(params = "method=listAllBoard") 
public String listAllBoard(@ModelAttribute("currUser")User user,

ModelMap model) { 
        bbtForumService. getAllBoard(); 
        //<——②Access the items property in the model here 
        System.out.println("model.items:" + ((List<String>)

model.get("items")).size( )); 
        return "listBoard"; 

 
At ①, by using the @ModelAttribute annotation, the populateItem() method will

Called before any request processing method is executed, Spring MVC will put the return value of the method

into the list of implicit model object properties under the name of "items".
Therefore, at ②, we can access the items property through the ModelMap parameter. When

the listAllBoard() request processing method is executed

, the information of "model.items:2" will be printed on the console at ②. Of course, we can also access

the items property in the model object in the requested view.
 
 
The @ResponseBody
annotation can be placed directly on the method, indicating that the return type will be output directly as the HTTP response byte

stream (not placed in the Model, nor intercepted as the view page name). Can be used for ajax.
 
@RequestParam
@RequestParam is an optional parameter, for example: @RequestParam("id") annotation, so

it will be bound with the parameter id of the URL
If the input parameter is a basic data type (such as int, long, float, etc.), There

must be corresponding parameters in the URL request parameters , otherwise an

org.springframework.web.util.NestedServletException will be thrown, indicating that null

cannot be converted to a basic data type.
 
@RequestParam contains 3 configurations @RequestParam(required = ,value ="",

defaultValue = "")
required : Whether the parameter is required, boolean type, optional, the default is true
value: passed parameter name, String type, optional, if there is a value, corresponding to the parameter of the setting

method
defaultValue: String type, when the parameter is not passed, it is the default value specified for the parameter
 
@SessionAttributes session management
Spring allows us to selectively specify Which attributes in the ModelMap need to be transferred to the

session, so that the attributes can be accessed in the attribute list of the corresponding ModelMap in the next request

. This function is achieved by annotating the @SessionAttributes annotation at the class definition

. @SessionAttributes can only be declared on classes, not methods.
 
For example
 
@SessionAttributes("currUser") // Name the attribute in ModelMap as currUser


@SessionAttributes({"attr1","attr2"})
@SessionAttributes(types = User.class)
@SessionAttributes(types = {User.class ,Dept.class})
@SessionAttributes(types = {User.class,Dept.class},value={"attr1","attr2"})

 
@CookieValue Get cookie information
@RequestHeader Get request header information

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326679831&siteId=291194637