@Controller @Controller is responsible for registering a bean into the spring context, the Bean ID defaults to
The class name starts with lowercase letters, and you can specify them as follows
Method One:
@Controller
public class TestController {}
Method Two:
@Controller ("Tmpcontroller")
public class TestController {}
@RequestMapping [email protected] is used to define the URL of the access, you can define an entire class
@RequestMapping, or specify one for each method.
Put the @requestmapping at the class level, which allows it to be at the method level
@RequestMapping annotations work together to achieve the effect of narrowing the selection.
For example:
@RequestMapping ("/test")
public class TestController {}
, all access paths under the class are under/test.
2. It is not necessary to use @requestmapping for the entire class, and if not configured, the access path configuration of all the methods will be completely independent and without any association.
3. The complete parameter is: @RequestMapping (value= "", method =
{"", ""},headers={},params={"", ""}), each parameter is described as follows:
Value:string[] Set access address
Method:requestmethod[] Set access mode, character array, view Requestmethod
class, including Get, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, common
Requestmethod.get,requestmethod.post headers:string[] Headers General union method = Requestmethod.post Use
Params:string[] Access parameter settings, character arrays such as: Userid=id
The 4.value configuration can also be in the form of a template variable, for example: @RequestMapping
(value= "/owners/{ownerid}", Method=requestmethod.get), which
Detailed description of the @pathvariable in Shaoxing.
[Email protected] params adds that you can restrict access to an address by setting a parameter condition, such as params= "myparam=myvalue" expression, where the parameter in the access address contains only the value of that provision "myparam=myvalue "In order to match, expressions like" Myparam "are also supported, indicating that the address of the current request must have this parameter (the value of the parameter can be any), and that an expression such as"!myparam "indicates that the currently requested address cannot contain the specified parameter" Myparam ".
6. One thing to note is that if you define a class with access addresses such as *.do,*.html, then at the method level @requestmapping, you can no longer define value values, or you will get an error, such as
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 in the example above:/bbs.do?method=getlist can access the method GetList (), and the Access/bbs.do/splist will report an error.
@PathVariable [email protected] is used for parameters in the method, which represents the template variable that the method parameter binds 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";
}
[email protected] used when using {XXX} template variables for the address bar. If @requestmapping does not define a variable like "/{ownerid}", then using @pathvariable in the method will cause an error.
@ModelAttribute 1. Apply to method parameters, parameters can be obtained directly from the page, equivalent to
Request.setattribute (,)
2. Apply to the method to label any one method that has a return value @ModelAttribute, so
Its return value will be entered into the list of properties for the model object.
3. @modelattribute ("XX") when applied to method parameters, must be associated to the data type of object
, the basic data type such as: Int,string does not work
For example:
Java code
@ModelAttribute ("items") to add a//<--① to the model object named items.
Property
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 ();
<--② the Items property in this access model
System.out.println ("Model.items:" + (list<string>)
Model.get ("items")). Size ());
return "Listboard";
}
At ①, by using @ModelAttribute annotations, the Populateitem () method is called before any request processing method executes, and Spring MVC puts the method return value in the Hidden Model object property list with the "items" name.
So at ②, we can access the Items property through Modelmap, and when the Listallboard () Request processing method is executed, the "model.items:2" message is printed on the console at ②. 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, indicating that the return type will be output directly as an HTTP response byte stream (not being placed in the model or blocked as the View page name). Can be used for Ajax.
@RequestParam @RequestParam is an optional parameter, such as @RequestParam ("id") annotation, so
It will be bound with the parameter ID of the URL if the entry is a basic data type (such as int, long, float, etc.), the URL request parameter must have the corresponding parameter, otherwise it will throw Org.springframework.web.util.NestedServletException exception indicating that null cannot be converted to the base data type.
@RequestParam consists of 3 configuration @RequestParam (required =, value= "", DefaultValue = "")
Required: Whether the parameter must be, Boolean type, optional, default to True
Value: passed the parameter name, string type, optional, if there is a value, corresponding to the parameter of the Set method
Defaultvalue:string type, the value that is specified by default for the parameter when the parameter is not passed
@SessionAttributes Session Management Spring allows us to selectively specify which attributes in the modelmap need to be dumped into the session so that the next request belongs to the corresponding Modelmap property list, which can also be accessed. This function is implemented by annotating @SessionAttributes annotations at the class definition. @SessionAttributes can only be declared on a class, not on a method.
For example
@SessionAttributes ("Curruser")//property named Curruser in Modelmap
@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 header information for the request
Annotations commonly used by Spring MVC