1.1. @RequestMapping Mapping Request
SPRINGMVC use @RequestMapping annotations to specify which URL requests can be processed by the controller
@requestMapping can be defined on classes and methods
Package Com.ibigsea.springmvc.helloworld;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic class HelloWorld {/** * configuration @requestmapping Intercept Localhost:8080/springmvc/hello Request * @return */@RequestMapping ("/hello") public String HelloWorld () { System.out.println ("Hello World"); return "HelloWorld";}}
Package Com.ibigsea.springmvc.helloworld;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; @Controller @requestmapping ("/hello") public class HelloWorld {/** * config @requestmapping intercept localhost:8080/springmvc/hello/world requests * @return */@RequestMapping ("/world ") Public String HelloWorld () {System.out.println (" Hello World "); return" HelloWorld ";}}
@RequestMapping
– class definition : Provides preliminary request mapping information. the root directory relative to the WEB app
– Method : Provides further subdivision mapping information. Relative to the URLat the class definition. If
@RequestMappingis not marked at the class definition, the URL marked at the method is relative to the
WEB app's root directory
Dispatcherservlet intercepts the request, it passes through the controller
The mapping information provided by the @RequestMapping determines the processing method that corresponds to the request.
@RequestMapping In addition to using request URL mapping requests,
You can also use request methods, request parameters, and request header mapping requests
1.2. @RequestMapping limit request method, request parameter, request header
/** * Receive a GET request * @return */@RequestMapping (value= "/get", method = requestmethod.get) public String get () { System.out.println ("get"); return "Get";} /** * Receive POST request * @return */@RequestMapping (value= "/post", method = requestmethod.post) public String post () { System.out.println ("post"); return "POST";} /** * Receive only the name parameter * @return */@RequestMapping (value= "/params", params= "name") public String params (string name) {System.out . println ("Hello" +name); return "HelloWorld";} /** * Only receive requests Content-type as Text/html;charset=utf-8 in the request header * @return */@RequestMapping (value= "/headers", headers= " Content-type:text/html;charset=utf-8 ") Public String headers () {System.out.println (" headers "); return" HelloWorld ";}
1.3. @RequestMapping matching characters
– ? : Match one of the characters in the file name
– *: matches any character in the file name
– * *:* * match multi-layer path
Example :
URL:/user/*/create
--/user/bigsea/create , /user/Sea/create and other URLs
URL:/user/* */create
--/user/big/sea/create , /user/sea/big/create and other URLs
URL:/user/create??
--/user/createaa ,/user/createBB
1.4. @PathVariableAnnotations
The URL with placeholders is a new feature of Spring3.0, which springmvc to REST Milestones in the development of goals
by @PathVariable You can bind a placeholder parameter in a URL to an entry in the Controller processing method: {xxx} in the URL placeholders can be bound to an entry in an action method by @PathVariable ("xxx").
/** * Localhost:8080/springmvc/hello/pathvariable/bigsea * Localhost:8080/springmvc/hello/pathvariable/sea * These URLs will be Execute this method and pass <b>bigsea</b>, <b>sea</b> as parameter to the Name field * @param name * @return */@RequestMapping ("/pathvariable/{name}") public string pathvariable (@PathVariable ("name") string name) {System.out.println ("Hello" + name); return "HelloWorld";}
JSP ( Specify the full path here ):
Operation result :
Hello Bigseahello Sea
1.5. @RequestParamBinding Request ParametersYou can pass the request parameter to the request method by using the @RequestParam at the processing method entry.
– value: Name of parameter
– required: Whether it is necessary. The default is true, which means that the request parameter must contain the corresponding parameter and, if not present, an exception is thrown
/** * If required = true, the field that corresponds to the request parameter must exist. Throws an exception if it does not exist <br/> * @param firstName can be null * @param lastName cannot be null. A NULL Report exception * @param age field indicates that the default value is 0 * @return */@RequestMapping ("/requestparam") public String if there is no age parameter Requestparam (@RequestParam (value= "FirstName", Required=false) String firstName, @RequestParam (value= "lastName", required = True) String LastName, @RequestParam (value= "age", required = False, defaultvalue= ' 0 ") int age) {System.out.println (" Hello my Name is "+ (FirstName = = null?" ": firstName) + LastName +", "+ Age +" years-old "); return" HelloWorld ";}
Jsp:
<a href= "Requestparam?firstname=big&lastname=sea" > Name is Bigsea, age is 0 </a><br/><a href= " Requestparam?lastname=sea&age=23 "> Name is Sea, </a><br/><a href=" Requestparam "> Thr oWS Exception </a>
Operation result :
Hello my name is bigsea,0 years old this yearhello my name was sea,23 years old this year
1.6. @RequestHeaderGET request HeaderThe request header contains several properties that the server can learn about the client and bind the attribute values in the header to the parameters of the processing method by @RequestHeader
/** * Get the information in the request header * @RequestHeader also has value, required, defaultvalue three parameters * @param useragent * @param cookie * @return */@Req Uestmapping ("/requestheader") public string Requestheader (@RequestHeader ("User-agent") string useragent,@ Requestheader ("cookie") String Cookie) {System.out.println ("useragent:[" +useragent+ "]"); System.out.println ("cookie:[" +cookie+ "]"); return "HelloWorld";}
Jsp:<a href= "Requestheader" > Requestheader </a>
Operation Result:useragent:[mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/44.0.2383.0 safari/537.36]cookie:[jsessionid= DA3B15F559349EA2C3F08BE772FCAFD8]
1.7. @CookieValueGetCookiesvalue/** * Use @cookievalue to bind cookie value <br/> * Note @cookievalue also has value, required, defaultvalue three parameters * @param session * @retur n */public String Cookievalue (@CookieValue (value = "Jsessionid", required= false) string session) {System.out.println (" jessionid:["+session+"); return "HelloWorld";}
Jsp:<a href= "Cookievalue" > Cookievalue </a>
Run resultsJESSIONID:[A4196EEDFD829B40CC1975F029A61328]
1.8. Source Code Analysis
SPRINGMVC Study Notes (ii) @RequestMapping, @PathVariable and other annotations