Tag: Mark GET request message specify MSU via MIL param efault
First look at the following code:
@Controller @requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/findall") Private String FindAll () {return "index";}}
Here are some simple ways to use the above notes:
@Controller
@Controller: Indicates that the class is a controller class. Suppose to use this annotation. This section needs to be added to the SPRING-MVC configuration file. <context:component-scan base-package= "Com.ztz.springmvc.controller"/>
@RequestMapping
@RequestMapping: The ability to specify for the controller which URL requests processing can request, @RequestMapping can be defined on a class or method.
- Class: Provides preliminary request mapping information. The root folder relative to the WEB app
- Method: Provides further subdivision mapping information.
Relative to the URL at the class definition.
If @RequestMapping is not marked at the class definition. The URL labeled at the method is relative to the WEB app's root folder
Dispatcherservlet intercepts the request, it passes through the controller
@RequestMapping the mapping information provided to determine the appropriate processing method for the request.
@RequestMapping In addition to the ability to use request URL mapping requests, you can also use request methods, request parameters, and request header mapping requests.
@RequestMapping Limit Request method, request parameters, request header
@Controller @requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/findall", method= Requestmethod.get) Private String findAll () {System.out.println ("only accept GET request"); return "Index";}}
@Controller @requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/findall", method= Requestmethod.post) Private String findAll () {System.out.println ("only accepts POST request"); return "Index";}}
@Controller @requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/findall", params= "name") Private String FindAll () {System.out.println ("only accept name"); return "Index";}}
Suppose a request URL does not carry a name parameter, then the method rejects the interview.
@Controller @requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/findall", headers= " Content-type:text/html;charset=utf-8 ") Private String FindAll () {System.out.println (" only accepts Content-type in the request header as text/ Html;charset=utf-8 's request "); return" Index ";}}
@RequestParam Binding Request Parametersuse @RequestParam in processing methods to pass request parameters to the request method
- Value: Name of the participant
- Required: Whether it is necessary. Default is true, indicating that the corresponding parameter must be included in the request, and if it does not, throws an exception
@Controller @requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/findall") Private String FindAll (@RequestParam (value= "name", Required=true) String name,//The parameter name cannot be empty @RequestParam (value= "Sex", Required=false) string sex,//parameter sex can be empty @RequestParam (value= "age", defaultvalue= "()") string age) {//The number of participants is assumed to be empty, The default value is 20SYSTEM.OUT.PRINTLN (name); SYSTEM.OUT.PRINTLN (Sex); System.out.println (age); return "Index";}}
Browser Request: Http://127.0.0.1:8080/springmvc/user/findAll?name=123Console output:123
Null
-
@RequestHeader Get the request headeran HTTP request header consists of several attributes. The server can then learn the client's information by@RequestHeaderYou can bind attribute values in the header to the processing method's entry.
@Controller @requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/findall") Private String FindAll (@RequestHeader (value= "user-agent") string user_agent, @RequestHeader (value= "cookie") string Cookie) { System.out.println (user_agent); System.out.println (cookie); return "Index";}}
Browser Request: Http://127.0.0.1:8080/springmvc/user/findAllConsole output:mozilla/5.0 (Windows NT 6.2; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/40.0.2214.111 safari/537.36
jsessionid=0e665d80d25f097eb9eca533f07c3355
@CookieValue Get Cookie value
above we get the HTTP request header cookie. Now we go directly to get the value of the cookie.
@Controller @requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/findall") Private String FindAll (@CookieValue (value= "Jsessionid") String cookie) {System.out.println (cookie); return "Index";}}
Browser Request: Http://127.0.0.1:8080/springmvc/user/findAllConsole output:0e665d80d25f097eb9eca533f07c3355
@RequestBody Get the body of the HTTP
@Controller @requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/findall", method= Requestmethod.post) private String FindAll (@RequestBody (required=true) string body) {System.out.println (body); return "Index";}}
This is tested with fiddler. Post request Http://127.0.0.1:8080/springmvc/user/findAll, the message body is: {"name": "Test @RequestBody"}Console output:{"Name": "Test @RequestBody"}
PS: Can grasp the above several annotations, SPRINGMVC can be almost the same can be used.
Commentary on Springmvc's @controller, @RequestMapping and other annotations