An introduction.
@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
1. @RequestMapping the URL used to define the access, you can define one for the 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, if not configured, all methods
The access path configuration will be completely independent, 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 combination 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.
5. @RequestMapping the params add-on, you can set the parameter conditions to limit
Access addresses, such as params= "Myparam=myvalue" expressions, where the parameters in the access address are only
It contains the value "Myparam=myvalue" to match, like "Myparam".
Expression is also supported, indicating that the address of the current request must have this parameter (the value of the parameter can be
Any), an expression such as "!myparam" indicates that the currently requested address cannot contain a specific specified
The parameter "Myparam".
6. It is important to note that if a class is defined with access addresses such as *.do,*.html, the
At the method level @requestmapping, you can no longer define value values, otherwise 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 ();
Ask/bbs.do/splist will be the error.
@PathVariable
1. @PathVariable for a parameter in a method, a template that represents a method parameter bound to an address URL
Variable.
For example:
Java code
@RequestMapping (value= "/owners/{ownerid}",
Method=requestmethod.get)
public string Findowner (@PathVariable string ownerid, Model
2. @PathVariable used when using the {XXX} template variable for the address bar.
If @requestmapping does not have a definition like "/{ownerid}", this variable is used in the
method, @pathvariable will 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>)
At ①, by using the @ModelAttribute annotations, the Populateitem () method
Called before any request processing method executes, Spring MVC returns the value of the method with the "items
"is placed in the list of implied model object properties for the name.
So at ②, we can access the Items property through the Modelmap, when
Line Listallboard () Request processing method, the ② is printed in the console
"Model.items:2" information. Of course we can also access the model in the requested view
The Items property in the object.
@ResponseBody
This annotation can be placed directly on the method, indicating that the return type will be directly used as the HTTP response byte
Stream output (not placed in model, not blocked as 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 import parameter is a basic data type (such as int, long, float, etc.), the URL request parameter
Be sure to have the corresponding parameter, otherwise it will throw
Org.springframework.web.util.NestedServletException exception, hint no
method to convert null 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 that corresponds to the set-side
Parameters of the 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
Session so that the next request belongs to the corresponding Modelmap property list, which can also be accessed
To these properties. This feature is labeled @SessionAttributes annotations at the class definition to
Implemented. @SessionAttributes can only be declared on a class, not on a method.
For example
@SessionAttributes ("Curruser")//property named Curruser in Modelmap
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.