@Controller

The @Controller is responsible for registering a bean into the Spring context. The bean’s ID defaults to

The class name starts with a lowercase letter, which you can specify yourself, as follows

@controller public class TestController {}

@Controller(” tmpController “) public class TestController {} @requestMapping

1.@RequestMapping is used to define the URL to access. You can define @requestMapping for the entire class, or specify one for each method. Putting @RequestMapping at the class level allows it to work with @RequestMapping annotations at the method level to narrow down the selection.

For example, @requestMapping (“/test”) public class TestController {} All access paths of this class are under /test.

2. It is not necessary to use @RequestMapping for the entire class. If not configured, the access path configuration for all methods is completely independent of each other.

3. The complete parameters are as follows: @requestMapping (value=””,method ={“”,””},headers={},params={“”,””}). Value :String[] Specifies the access address. [] select * from requestMethod. GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE; Requestmethod. POST headers:String[] Headers :String params: String[

4. Value can also be configured in the form of template variables, for example: @requestMapping (value=”/owners/{ownerId}”, method= requestmethod.get), which will be specified in the plugin @pathVariable.

In addition to 5.@RequestMapping params, you can restrict access to addresses by setting parameter conditions, such as the params=”myParam=myValue” expression, “MyParam =myValue”, “myParam=myValue”, “myParam=myValue”, “myParam=myValue”, “myParam=myValue” Expressions such as “myParam” indicate that the currently requested address cannot contain the specified parameter “myParam”.

RequestMapping (@requestMapping) {RequestMapping (@requestMapping) {RequestMapping (@requestMapping) {RequestMapping (@requestMapping); For example Java @requestMapping (“/bbs.do”) public class BbsController {@requestMapping (params = “method=getList “) public String getList() {return “list”; } @requestMapping (value= “/spList”) public String getSpecialList() {return “spList”; }}

/bbs.do? Method =getList Access method getList(); /bbs.do/spList will return an error @pathvariable

1.@PathVariable is used as a parameter in a method and represents the template variable to which the method parameter is bound to the address URL.

Such as:

Java code @ RequestMapping (value = “/ market metrix / {ownerId}”, method=RequestMethod.GET) public String findOwner(@PathVariable String ownerId, Model model) { Owner owner = ownerService.findOwner(ownerId); Model. The addAttribute (” owner “, the owner); Return “displayOwner”; }

2.@PathVariable is used when {XXX} template variables are used in the address bar. If @requestMapping does not define a variable like “/{ownerId}”, then @pathVariable in the method will report an error.

@ModelAttribute

1. Apply to method parameters, which can be obtained directly on the page, equivalent to request.setattribute (,).

2. Apply to methods. Annotate @modelAttribute with any method that has a return value so that the return value will be added to the attribute list of the model object.

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.

Java code

@modelAttribute (” items “)//< — ① Add an attribute named items to the model object public List populateItems() {List lists = new ArrayList(); Lists. The add (” item1 “); Lists. The add (” item2 “); return lists; }

@requestMapping (params = “method=listAllBoard”) Public String listAllBoard(@modelAttribute (” currUser “)User User, ModelMap model) { bbtForumService.getAllBoard(); System.out.println(” model.items: “+ ((List) model.get(” items”)).size()); Return “listBoard”; }

At ①, using the @ModelAttribute annotation, the populateItem() method is called before any request-handling methods are executed, and Spring MVC puts the method return value as “items” into the implied model object property list. So at ②, we can access the items property through ModelMap, and ② will print “Model.items :2” information on the console when we execute the listAllBoard() request method. Of course, we can also access the Items property in the model object in the requested view.

@ResponseBody

This annotation can be placed directly on the method to indicate that the return type will be output directly as an HTTP response byte stream (not placed in the Model and not intercepted as a view page name). It can be used for Ajax.

@RequestParam

@requestParam is an optional parameter, such as the @requestParam (” ID “) annotation, so it will be bound to the URL parameter ID

If the input parameter is a basic data type (such as int, long, float, etc.), the URL request parameter must have a corresponding parameter, otherwise it will be thrown

Org. Springframework. Web. Util. NestedServletException abnormalities, without method converts a null to basic data types.

@requestParam contains 3 configurations @requestParam (required =,value=””, defaultValue =””)

Boolean, optional, default true value: The name of the passed parameter, String, optional, if any, corresponding to the setting method parameter

DefaultValue: A String value that is specified by default if the parameter is not passed

@ SessionAttributes session management

Spring allows us to optionally specify which attributes in ModelMap need to be dumped into the session so that the next request belongs to the corresponding ModelMap’s attribute list. This functionality is implemented through the @sessionAttributes annotation at the class definition. @sessionAttributes can only be declared on classes, not methods.

For example,

@sessionAttributes (” currUser “) // Attribute named currUser in ModelMap

@ SessionAttributes ({” attr1, attr2 “})

@SessionAttributes(types = User.class)

@SessionAttributes(types = {User.class,Dept.class})

@ SessionAttributes (types = {User. The class, Dept. Class}, value = {” attr1, attr2 “})

@cookievalue gets cookie information

@requestheader Gets the header information for the request