The previous article introduced annotation @pathvariable, which can pass a parameter to a rest-style URL in the form of a placeholder, but this parameter is not a request parameter in the real sense. How to deal with request parameters is the main content of this article.
Spring MVC binds the HTTP request information to the corresponding ginseng in the processing method by parsing the signature of the processing method.
Spring MVC has a very loose limit on how the controller handles signature, and can sign the method almost as you like.
If necessary, the appropriate annotations (@PathVariable, @RequestParam, @RequestHeader, etc.) for the method and method entry can be annotated, and the SPRINGMVC framework binds the information of the HTTP request to the appropriate method entry. and corresponding subsequent processing is made based on the return value type of the method.
(This article originates from: http://my.oschina.net/happyBKs/blog/417032)
You 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
The Controller class and handler functions are as follows:
Package Com.happybks.springmvc.handlers;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestParam, @RequestMapping ("Class") @Controllerpublic class Rptesthandler {string page= "SUCCESSRM"; @RequestMapping ("Student") public String handle (@RequestParam (value= " Username ") String un, @RequestParam (value=" age ") int age) {System.out.println (" a student ' s request has come. Username: "+un+", Age: "+age"; return page;}}
This is used to map the request parameters using the Value property values of the @requestparam annotations.
Request Page index8.jsp:
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "pageencoding=" Iso-8859-1 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Well, the process is as follows:
Click on the super Intercept request:
The console now outputs:
A student ' s request has come. Username:happybks, age:100
You can also enter http://localhost:8080/mymvc/class/student?username=happyBKs&age=101 directly in the browser address bar
The console shows a student's request has come. Username:happybks, age:101
Question 1: What happens if our request is one less parameter age?
How should the problem be solved?
The required attribute or DefaultValue property of the @requestparam annotation is required here.
The Required property labels whether this parameter is required, is true by default, and is set to False if you want it to not exist. Note, however, that the parameter type of the handler for the parameter required set to False must be an object type, otherwise the return error is 500!
For example, if we change the controller class to:
Package Com.happybks.springmvc.handlers;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestParam, @RequestMapping ("Class") @Controllerpublic class Rptesthandler {string page= "SUCCESSRM"; @RequestMapping ("Student") public String handle (@RequestParam (value= " Username ") String un, @RequestParam (value=" age ", required=false) int age) {System.out.println (" a student ' s request have Come. Username: "+un+", Age: "+age"; return page;}}
The result will be an error, because although the age set required to false, the request parameter can not contain age, but the corresponding parameter type in the processing function is Int,int is the basic data type, cannot be empty, so error. The workaround is to change int to integer.
Operation Result:
Console output:
A student ' s request has come. Username:happybks, Age:null
If we still want to use the base type as the parameter type, we can use the DefaultValue property of the @requestparam annotation to specify the default value.
Package Com.happybks.springmvc.handlers;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestParam, @RequestMapping ("Class") @Controllerpublic class Rptesthandler {string page= "SUCCESSRM"; @RequestMapping ("Student") public String handle (@RequestParam (value= " Username ") String un, @RequestParam (value=" age ", Required=false, defaultvalue=" 0 ") int age) {System.out.println (" a Student ' s request has come. Username: "+un+", Age: "+age"; return page;}}
Operation Result:
Console A student ' s request has come. Username:happybks, age:0
Finally, make a summary: (@RequestParam annotations are very common, so it is important)
* @RequestParam to map request parameters
* value is the name of the requested parameter
* Required Whether this parameter is required. Default is True
* DefaultValue The default value of the clear autumn parameter
SPRINGMVC Note Series (8)--requestparam annotations