SpringMVC JSP page controller receives parameters of the form and details of the receiving mode Detailed note (400 error)

The controller processes the received parameters

(1) previously received parameters:

String  param = req.getParameter(name);

(2) SpringMVC simplify the operation, a method of adding only the controller parameters to give

a: the above-described method is essentially the call receiving, but did package

b: parameter type (except for special types, for example: Date) can be automatically converted

note:

(1) in the form of name attribute and consistent method a list of values , and if not, will not be given, but that the received value is null, to be noted that, int type parameter is not null, so the server and page We are given: Therefore, to avoid such errors, when the definition entity class entity class attribute type , base type package type is preferably defined as

(2) When the received data belongs to a date type , if using only Date as the reception parameter type, can not solve the problem, because the date is defined in the format, and the date format in the form must be and method parameter list the date format correspondence, or will given; the annotation can @DateTimeFormat avoid errors (pattern = "yyyy-MM- dd") date birthday, twice; in addition, in the SpringMVC, 400 types of errors are generally Since the reception parameter type is caused by the mismatch

(3) When the parameter list is a Boolean when the data submitted in the form of parameter values can be true or false, it can be received normally, and may be 1 or 0, where 1 represents true, 0 for false

婚否: <input type="radio" name="married" value="1"> 已婚

     <input type="radio" name="married" value="0"> 未婚<br>

(4) When the parameter list of a method of an object when, SpringMV parameters can also be received, but the request object name and property name must be in a form consistent values. Wherein, if the attribute object to another object, you can be in the form object name. Attribute name to the attribute assignment

地址: <select name="address.city">

(5) a method parameter list is list set , name attribute values in the form, you can be named by the following manner:

 地址1: <select name="addrList[0].city">

(6) The method can not be used as a set of parameter list map

<form action="${pageContext.request.contextPath}/demo1" method="post">
    用户名: <input type="text" name="username"><br>
    年龄: <input type="text" name="age"><br>
    生日: <input type="text" name="birthday">yyyy-MM-dd<br>
    爱好: <input type="checkbox" name="hob" value="java"> java
    <input type="checkbox" name="hob" value="C++"> C++
    <input type="checkbox" name="hob" value="PHP"> PHP<br>
    婚否: <input type="radio" name="married" value="1"> 已婚
    <input type="radio" name="married" value="0"> 未婚<br>
    地址: <select name="address.city">
    <Option value = "Beijing"> Beijing </ option>
    <Option value = "Shanghai"> Shanghai </ option>
    <Option value = "Tianjin"> Tianjin </ option>
</select><br>
    地址1: <select name="addrList[0].city">
    <Option value = "Beijing"> Beijing </ option>
    <Option value = "Shanghai"> Shanghai </ option>
    <Option value = "Tianjin"> Tianjin </ option>
</select><br>
    地址2: <select name="addrList[1].city">
    <Option value = "Beijing"> Beijing </ option>
    <Option value = "Shanghai"> Shanghai </ option>
    <Option value = "Tianjin"> Tianjin </ option>
</select><br>
    <input type="submit" value="提交">
</form>

  

package com.bjsxt.controller;

com.bjsxt.pojo.User amount;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.Date;

@Controller
public class DemoController {

    @RequestMapping("/demo1")
    public String demo1(User user) {
        System.out.println(user);
        return "index.jsp";
    }

    @RequestMapping("/demo2")
    public String demo2(String username,
                        Integer age,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday,
                        String[] hob,
                        Boolean married) {
        System.out.println("username = " + username);
        System.out.println("age = " + age);
        System.out.println("birthday = " + birthday);
        System.out.println("hob = " + Arrays.toString(hob));
        System.out.println("married = " + married);
        return "index.jsp";
    }
}

  

the User class the implements the Serializable {public 
  // getter and setter methods are omitted here, the normal need to add the test private String username; private Integer age; @DateTimeFormat(pattern = "yyyy-MM-dd") private Date birthday; private String[] hob; private Boolean married; private Address address; private List<Address> addrList = new ArrayList<>(); }

  

public class Address implements Serializable {
    private String city;
}

  

 

Guess you like

Origin www.cnblogs.com/ncl-960301-success/p/11006545.html