springMVC binding request parameters and custom type converter

1. Request Parameter Description bound
  1. binding mechanism
    data form submission is k = username v format password = 123 & haha =
    SPRINGMVC parameter binding process to form submission request parameter, as control parameters of the method of bundling
  2. supported data types
    basic data types, and strings

      1. Name and submit the form name parameter is the same

      2. The case-sensitive
    entity type

      1. Submit the form and name of the JavaBean property name needs to be consistent
      2. If an entity class contains other type of reference, the form of the name attribute need to write to: Object Properties

        For example: address.name

    Aggregate data type (List, map collections, etc.)

      The first way:

        Required parameters must be set in the type of request POJO. Request for a parameter name in the form POJO and collection of the same attribute name.
        List elements of the collection to the assignment, the subscript list [0]. Attribute
        to the elements of the collection assignment Map using pairs. map [ 'one']. property

      The second way:

        Json parameters received request is a data format, it needs to achieve a comment. @RequestBody: parameters used in the controller method

<! - basic types example -> 
< A href = "the Account / findAccount accountId = 10 & accountName = zhangsan?" > View Account </ A >
@RequestMapping("/findAccount")
public String findAccount(Integer accountId,String accountName) {
  System.out.println("查询了账户。。。。"+accountId+","+accountName);
return "success";
}
<-! POJO type of presentation -> 
< form Action = "the Account / saveAccount" Method, = "POST" > 
  Account Name: < the INPUT of the type = "text" name = "name"  > < br /> 
  the amount of accounts: < the INPUT of the type = "text" name = "Money"  > < br /> 
  account provinces: < the INPUT of the type = "text" name = "address.provinceName"  > < br /> 
  account city: <input type="text" name="address.cityName" ><br/>
<input type="submit" value=" 保存 ">
</form>
public class Account implements Serializable {
  private Integer id;
  private String name;
  private Float money;
  private Address address;
  //getters and setters
}
public class Address implements Serializable {
  private String provinceName;
  private String cityName;
  //getters and setters
}
@RequestMapping("/saveAccount")
public String saveAccount(Account account) {
  System.out.println("保存了账户。。。。"+account);
  return "success";
}
<-! POJO class contains a collection of the type of presentation -> 
< form Action = "the Account / updateAccount" Method, = "POST" > 
  User Name: < the INPUT of the type = "text" name = "username"  > < br /> 
  user password : < the INPUT of the type = "password" name = "password"  > < br /> 
  Age: < the INPUT of the type = "text" name = "Age"  > < br /> 
  account name 1: <input type="text" name="accounts[0].name" ><br/>
  账户 1 金额:<input type="text" name="accounts[0].money" ><br/>
  账户 2 名称:<input type="text" name="accounts[1].name" ><br/>
  账户 2 金额:<input type="text" name="accounts[1].money" ><br/>
  账户 3 名称:<input type="text" name="accountMap['one'].name" ><br/>
  账户 3 金额:<input type="text" name="accountMap['one'].money" ><br/>
  账户 4 名称:<input type="text" name="accountMap['two'].name" ><br/>
  账户 4 金额:<input type="text" name="accountMap['two'].money" ><br/>
  <input type="submit" value=" 保存 ">
</form>
public class User implements Serializable {
  private String username;
  private String password;
  private Integer age;
  private List<Account> accounts;
  private Map<String,Account> accountMap;
  //getters and setters
}
@RequestMapping("/updateAccount")
public String updateAccount(User user) {
  System.out.println("更新了账户。。。。"+user);
  return "success";
}

 

 

2. Request parameter Chinese garbled solve

  Spring filter class configuration provided in web.xml

<! - Configure a filter to solve the problem of Chinese garbled -> 
< filter > 
  < filter-name > characterEncodingFilter </ filter-name > 
  < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class > 
  <! - specifies the character set -> 
  < the init-param > 
    < param-name > encoding </ param-name > 
    < param-value > UTF-. 8 </ param-value > 
  </ the init-param >
  <! -is meant forceEncoding = true regardless whether the request includes the client code, all of the filter in encoding resolution request -> 
  < the init-param > 
    < param-name > forceEncoding </ param-name > 
    < param-value > to false </ param-value > 
  </ the init-param > 
</ filter > 
< filter-Mapping > 
  < filter-name > characterEncodingFilter </ filter-name > 
  < URL-pattern > / * </ URL-pattern > 
</filter-mapping>

  Springmvc can be configured in the configuration file, resources are not static filter:
    <-! LOCATION represent paths, mapping represent files, files and subdirectories ** expressed in the directory file ->
    <MVC: Resources LOCATION = "/ CSS / "Mapping =" / CSS / ** "/>
    <MVC: Resources LOCATION =" / Images / "Mapping =" / Images / ** "/>
    <MVC: Resources LOCATION =" / scripts / "= Mapping" / javascript / ** "/>

3. custom type converter

  1. Any form submission data type is a string of all types, but the background definition of type Integer, the data may be on the package,
   description of the internal frame Spring default data type conversion.
  2. If you want to customize the data type conversion can be achieved Converter interface

  The first step: the definition of a class that implements the Converter interface, which has two generics.

public  class StringToDateConverter the implements Converter <String, a Date> { // Parameter 1 : indicates the type of the received parameter 2: indicates the type of target 
  / ** 
  * String type is used to turn into a date type 
  * / 
  @Override 
  public a Date Convert (String Source ) { 
    DateFormat the format = null ;
    the try {
      IF (StringUtils.isEmpty (Source)) {
        the throw  new new a NullPointerException ( "Please enter the date to be converted" ); 
      } 
      the format = new new the SimpleDateFormat ( "the MM-dd-YYYY" ); 
      a date dATE = format.parse (Source);
      return DATE; 
    } the catch (Exception E) {
      the throw  new new a RuntimeException ( "Enter Date error" ); 
    } 
  } 
}

  Step Two: Configure type converters spring profile.

    Spring mechanism configuration type converter is custom converter is registered to type conversion services go.

<! - configuration type converter plant -> 
< the bean ID = "converterService" 
  class = "org.springframework.context.support.ConversionServiceFactoryBean" > 
  <! - to inject a new plant type converter -> 
  < Property name = "converters" > 
    < SET > 
      <-! configure a custom type converter -> 
      < the bean class = "com.fgy.web.converter.StringToDateConverter" > </ the bean > 
    </ SET > 
  </ Property > 
</ the bean >

  The third step: a reference type disposed in conversion service tag annotation-driven

<! - reference the custom type converter -> 
< MVC: Annotation-Driven Conversion-Service- = "converterService" > </ MVC: Annotation-Driven >

 

Guess you like

Origin www.cnblogs.com/roadlandscape/p/12310413.html