Springmvc Study Notes (11)-simple parameter binding for springmvc annotation Development
Springmvc Study Notes (11)-simple parameter binding for springmvc annotation Development
This article describes simple parameter binding for annotation development, including simple type, simple pojo, and custom binding for type conversion.
Spring parameter binding process
Request key/value data from the client and bind the key/value data to the parameters of the controller method.
In springmvc, the data submitted on the receiving page is received by using method parameters. Instead of receiving changes to controller class definition members !!!!
Supported types by default
You can use the following types of objects directly defined on the controller method parameters. During parameter binding, bind the following types directly.
HttpServletRequest: GET request information through the request object
HttpServletResponse: Process response information through response
HttpSession: Get the object stored in the session through the session object
Model/ModelMap: Model is an interface, and modelMap is an interface implementation. Purpose: Fill the model data in the request field. Simple Type
Pass@RequestParamBinds simple parameters. If you do not use@RequestParamThe request parameter name must be the same as the parameter name of the controller method before binding is successful.
If you use@RequestParamYou do not need to restrict the request parameter names to be consistent with those of the controller method.
The required attribute specifies whether the parameter must be passed in. If it is set to true, if no parameter is input, the following error is reported:
@ RequestMapping (value = "/editItems", method = {RequestMethod. POST, RequestMethod. GET}) in [email protected], specify the request input parameter name and parameter to bind. // Use the required attribute to specify whether the parameter must be passed in. // use defaultValue to set the default value. If the id parameter is not passed in, bind the default value to the parameter. Public String editItems (Model model, @ RequestParam (value = "id", required = true) Integer items_id) throws Exception {
Pojo binding
The name of the input in the page is the same as the attribute name in the pojo parameter of the controller, and the data on the page is bound to the pojo.
Note: Only names and parameters are required.Attribute nameConsistent, rather than requiringNameConsistency. Do not confuse this. The framework will automatically match the attribute name of the pojo class in the form parameter. (I didn't see the source code, but it should be implemented using reflection)
Page Definition:
Definition of pojo parameters of controller:
public class Items { private Integer id; private String name; private Float price; private String pic; private Date createtime; private String detail;
Bind custom parameters to date type
For pojo objects in controller parameters, if the attributes have date types, you need to bind custom parameters.
Transmits the request date data string to the date type. The date type to be converted must be the same as the date attribute type in pojo. In this example, the custom parameter binding converts a Date string to the java. util. Date type.
You need to inject custom parameter binding components into the processor adapter.
Custom date Type Binding
Public class CustomDateConverter implements Converter
{Public Date convert (String s) {// convert a Date String to a Date type (Format: yyyy-MM-dd HH: mm: ss) simpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); try {// convert to return simpleDateFormat directly. parse (s);} catch (ParseException e) {// TODO Auto-generated catch block e. printStackTrace ();} // If the parameter binding fails, return null ;}}
Configuration method
Differences between springmvc and struts2 1. springmvc is developed based on methods and struts2 is developed based on classes.
Springmvc maps url and controller methods. After successful ing, springmvc generates a Handler object, which contains only one method. Method execution is complete, and the data of the form parameter is destroyed. Springmvc controller development is similar to service development.
2. springmvc can be used for Singleton development. We recommend that you use Singleton development. struts2 receives parameters through the member variables of the class. You cannot use Singleton, but you can only use multiple instances.
3. After tests, struts2 is slow because struts labels are used. If struts is used, jstl is recommended.
Product Name |
|
Product Price |
|