The first two chapters on what is the SPRINGMVC,SPRINGMVC framework principle, and will be simple to use SPRINGMVC and SSM integration, from this chapter, began to explain the various functions of SPRINGMVC to realize, slowly digest
--wh
First, parameter binding
1.1. What is parameter binding?
Customers in the browser will submit some parameters to the server side, such as user login, etc., will pass username and password come over, SPRINGMVC through the parameter binding component will request the contents of the parameter data conversion, and then the converted value to the Controller method of the parameter , this is the process of parameter binding, in fact, SPRINGMVC is to use the Controller method parameter to receive the request parameters
1.2. SPRINGMVC default supported parameter types
The following types can be used directly in the parameters of the Controller method
HttpServletRequest, HttpServletResponse, HttpSession: These three are very familiar, do not explain
Model/modelmap: populating the Model data into the request domain, such as adding a list to the previous Modelandview object, is actually adding the list to the request scope, Just the Modelandview object is not only able to add model data, but also to add views. The model object's function is to populate the model data with the request scope
, that is, using these objects in the formal parameter, you can use these objects directly in the method. With the request object, we can use the old methods we've learned to get some information like request parameters.
1.3. Parameters that bind simple types
The above is only the default supported parameter types, there are requests and other objects, it must be very good and convenient one thing, such as GET request parameters can be resolved, but SPRINGMVC provides more powerful features.
Parameter rules for binding simple types:
1.3.1, if the parameters of the request parameter key and the Controller class in the same parameter name, then the direct binding;
Request url:http://localhost:8080/ssm_test/edititems.do?id=2 The analog client sent a id=2 request parameter, here is the Get method, if the post is the same way
Controller method:
Parse: The name in the formal parameter is the same as the name of the request parameter, and the solid can be directly bound.
1.3.2, if the parameter name of the method in the key and controller class of the request argument is inconsistent, then you need to use @requestparam annotation to bind the parameter
Request url:http://localhost:8080/ssm_test/edititems.do?itemsid=2 The impersonation client sent a id=2 request parameter.
Controller method:
Analysis: Use the @requestparam ("Itemsid") annotation to assign the value of the request parameter named Itemsid to the parameter named ID in the formal parameter. Note that the annotation is preceded by the corresponding formal parameter.
1.4. Binding Pojo
When using SPRINGMVC to bind Pojo parameters, the name value of the input box in the JSP is required to match the name of the property in the Pojo object of the controller method parameter, as
JSP page
Controller method
Items class
1.5. Parameter binding date type conversion problem
The types of parameters that we pass from the JSP page are object, and we write our own specific types, such as IDs that require int, name requires string, and Springmvc helps us to convert these simple types automatically. But when it comes to the date type, it does not convert, we need to write a type converter, and then to the adapter, so that the JSP passed parameters can be converted to the date type we need, if you do not customize the type converter, the error is that the date string in the request to the Java date class Type, which is consistent with the types of date attributes in Pojo
1.5.1, custom Converter
Dateconverter.java
1.5.2, configuring the converter in Springmvc.xml
First Way (recommended): Two steps to get it done
Modify the configuration of the Mvc:annotation-driven
Configure Custom Converter Bindings
Configuring Date Converters in Springmvc.xml
The second way:
View Code
1.6, Packaging Pojo parameter binding
The difference between the Pojo parameter binding with 1.4 is that the Pojo is put into a wrapper class, such as putting the items class in the Itemsqueryvo class, and Itemsqueryvo is a wrapper pojo
Itemsqueryvo
Jsp
Controller: Directly using the packaging Pojo receive
1.7. Binding of Set parameters
1.7.1, a simple type of collection parameter binding, you can use an array or list to receive
For example, JSP page has a number of boxes, checkboxes, so submit, you need to use the binding of the collection parameters.
Jsp
Controller
Use arrays to receive
The type of the array in the formal parameter is consistent with the type of the value in the JSP, and the parameter names in the parameters are identical to the name in the JSP. That's itemsid.
Use list to receive
The generic type of the list in the parameter is the same as the type of the value in the JSP, and the parameter names in the parameters are consistent with the name in the JSP. (This is what we imagined)
Results, will be error, hey, because you can not directly define the parameters of the list type in the formal parameter, if you want to use list to receive, you need to define the list type parameters in the packaging Pojo, controller's method parameter using the package Pojo, explained below. So if you use a collection parameter that receives a simple type, it is most convenient to use an array.
1.7.2, Pojo types of collection parameter bindings, you can use an array or list to receive
Note: The collection parameter of the Pojo type is bound, the array or list that receives it is not directly defined on the controller method parameter, it needs to be defined in a wrapper pojo, how to put the package Pojo in the formal parameter
Use list.
Wraps the Pojo class Itemsqueryvo, placing the objects that need to be in the Items collection in the wrapper class.
Jsp
Note here: The tag is the Name property, do not confuse with the Value property, in Itemsqueryvo is known as the Itemslist list, so the name in the JSP needs a layer of matching to correctly load its property values to the correct location, The format of the list is: itemslist[subscript].name. Take this analysis, Itemslist can find hit Itemsqueryvo in the Itemslist, itemslist[1], you can locate the itemslist in the first items,itemslist[1]. The name can be positioned to the Name property of the first items in itemslist, which makes sense.
Controller
1.7.3, summary of the above two
Summarize the binding of the collection parameters
For a collection parameter binding of a simple type, the array is used as a formal parameter to receive the requested collection parameter
For Pojo-type collection parameter bindings, use an array or list to both, generally used list.
Note: This Pojo-type collection parameter binding must be a list or an array as a property in a wrapper class, and then use the wrapper class object as the formal parameter to receive the request parameter.
1.7.4, map collection type bindings
This use is not much, generally just use list, this also slightly understand, and so on when needed, will be used, put the key code on the line
Similarly, you need to use the wrapper Pojo class.
Itemsqueryvo
JSP: The format is as follows. will be able to match to
Controller
Second, summary
Looking at so many examples of parameters binding, I think in a word to summarize the most accurate, original aim. Having the patience to look at it will naturally be used, really simple. Only the knowledge is more thin, need to understand also not much.
SPRINGMVC (c) Parameter binding,