Road SSM learning --springMVC Some notes on the first day _

@RequestParam

As mentioned above, the request parameter binding, the request is received over the parameters and parameter method parameter, if exactly the same, but the front and rear end can be written by two people, the front end of the parameter passed is name, but I wrote the back-end method parameter is the username, time to find inconsistencies, the parameter to be changed, then the method which all relate to this parameter must be changed, very inconvenient. (Of course, like the front end if the parameter can be altered, oh ...)
then this @requestParam notes have used the
role:
the parameter names specified in the request to the controller parameter assignment.
Attribute:
value: name of the parameter in the request.
required: if the request parameter must provide this parameter. Default value: true. Representation must be provided if you do not provide an error.

<a href= "anno/testRequestParam?name=haha&pass=123">testRequestParam</a>
@Controller
@RequestMapping("/anno")
public class annoController {
    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(name = "username",required = false)String name, @RequestParam("pass") String password){
        System.out.println("方法执行了...");
        System.out.println(name);
        System.out.println(password);
        return "success";
    }
}

Code, the front end of the get request to pass over the parameter name and Pass, but the rear end of the parameter name and password

The result:
Here Insert Picture Description
Why name it is null?
FIG view this
can be understood as a request for a parameter to be passed to the annotation, then the annotation is assigned to the received parameter
Here Insert Picture Description
So, for the name, since the notes are written in the username, do not correspond, since require = false, it does not It will be given, but since not acquired, the username is null, the null username and then transmitted to the name, so to get the name parameter is null username passed to it, so to get the final method name is null.

For purposes of pass: pass pass requests and comments on corresponding parameters, is delivered successfully, then transferred to the annotation pass password parameter, it can be printed.

@RequestBody

Action:
the request for acquiring the content thereof. It is obtained directly using the data key = value & key = value ... structure.
get request method is not applicable.
Properties:
required: Must have a request body. The default value is: true. When the value is true, get request method error. If the value
is false, get a request is null.

Usage:
Here Insert Picture Description
print content is the post form submission of the request, such as
username =% E5% 98% BB % E5% 98% BB & age = 12
received here will be a string garbled, now I do not know how to solve, but this comment json object is usually used to post over the front desk reception, it receives the string is not done, access to a lot of information are used to receiving garbled json case, the solution is to add the above method produces = "text / html ; charset = UTF-8 ", in front of the text / html under the circumstances is application / json or other.

@PathVariable

Effect:
for binding url placeholder. For example: in a request url / delete / {id}, the {id} is url placeholder.
url support placeholder is added after spring3.0. Springmvc support is an important sign of rest style URL.
Attribute:
value: specifies a placeholder name url.
required: Must provide a placeholder.

Here Insert Picture Description
And a placeholder value to the corresponding annotations.

@RequestHeader

Location:
parameter earlier, the value of the acquired parameter passed to
action:
request for acquiring the message header.
Property:
value: providing a message header name
required: Is it necessary to have this header
Note:
generally do not use in the actual development.

@CookieValue

Location:
parameter earlier, the value of the acquired parameter passed to
effect:
for the value passed to the method to specify the name of the cookie parameter controller.
Property:
value: Specifies the name of the cookie.
required: Do you have to have this cookie.

@ModelAttribute

Role:
This annotation is SpringMVC4.3 new version later added. It can be used to modify the methods and parameters.
In the method appears on representing the current method will be executed before the method of the controller, it performs. The method does not return values which can be modified, may be
to modify the specific methods have return values.
Appears on the parameters, gets the specified data to the parameter assignment.
Attribute:
value: for acquiring key data. POJO can be a key attribute name can also be key map structure.
Scenario:
When the form is submitted the data is not complete entity class data, to ensure that the field does not submit data using database objects original data.
For example:
a user database has three fields: name, age, address, if you modify user information when, in the form modifies only the name and age, update time to address because there is no value, is set to null, so we will first the original value of the user database kept well, then cover the execution based on the updated time, so that the original data will be saved.

Return value:
Here Insert Picture Description

After @ModelAttribute have annotated method will first perform in other ways, so the previously specified username and age, then call testUpdate method, submit the names only in the case of the front desk, "hee hee" in, age has not been set to null, one problem: user objects following methods, return to where to go? A: return back to the container inside. In this case the user object out of the container, as a user parameter, the form is submitted, by requesting a username over, the received parameter and the User class inside the package, this will be the original "John Doe" overwritten, variable It has become the new "hee hee", thereby realizing retain the original data operations.

None Return Value:
by a Map, the user object to the Map loaded container, in the implementation of the update, and then to obtain the corresponding target container according @ModelAttribute key, assigned to the parameter, and then re-transmitted according to the parameter reception package to the user object, to achieve coverage.
Here Insert Picture Description

@SessionAttribute

Action:
parameters for performing a method sharing among multiple controllers.
Property:
value: for property specifies the name of deposit
type: used to specify the type of data stored.

If @SessionAttributes inside, and not only provided username password, the password can not be stored into the session domain testGet by the same method, to get the password value is null.
Here Insert Picture Description
success.jsp page to print the session inside the field through which an EL expression value pairs
Here Insert Picture Description
and jsp pages can not be set to ignore EL expressions
Here Insert Picture Description

Published 31 original articles · won praise 0 · Views 1218

Guess you like

Origin blog.csdn.net/SixthMagnitude/article/details/104267350