Sunday, April 19, 2020

[Spring] Get parameters using HttpServletRequest, @RequestParam, and @ModelAttribute

HttpServletRequest


You can receive parameters passed to the controller via HttpServletRequest. There are a total of three methods for receiving parameters.


getParameter
@Controller
public class MvcController {
 
    @RequestMapping("/mvc.do")
    public String mvcInit(HttpServletRequest req) {
        // key값을 인자로 받아 value값을 반환
     String value = req.getParameter("name");
        
     return null;
    }
}
GetParameter is a method that receives one parameter delivered in the form of a key-value. Passing the key value as a factor returns the value value.

getParameterValues
@Controller
public class MvcController {
 
    @RequestMapping("/mvc.do")
    public String mvcInit(HttpServletRequest req) {
        // key와 연결된 모든 value 값을 배열로 반환
     String[] value = req.getParameter("name");
        
     return null;
    }
}
The getParamterValues returns all value values associated with one key, unlike the getParameter, in the form of an array.

getParameterNames
@Controller
public class MvcController {
 
    @RequestMapping("/mvc.do")
    public String mvcInit(HttpServletRequest req) {
        // 모든 key 값들을 반환함
     Enumeration<String> names = req.getParameter();
        // 다음 값이 있다면 실행
        while(names.hasMoreElements()) {
            // 다음 값으로 넘어감
            String name = names.nextElement();
            System.out.println(name);
        }
        
     return null;
    }
}
getParameterNames returns all key values passed in a form similar to an array called Enumeration. In order to make the rotation as shown in the array, the hasMoreElements method verifies that the following values exist, and then the nextElement method approaches the following values and rotates them.

@RequestParam

RequestParam is an annotation that makes it easy to receive the parameters delivered.

example
@Controller
public class MvcController {
 
    @RequestMapping("/mvc.do")
    public String mvcInit(@RequestParam String str, @RequestParam HashMap<String, Object> map) {
     ...
        return null;
    }
}
For @RequestParam, paste the variable value value to the variable that contains the value value, and the variable name must be the key name that matches the value value. Also, you can receive all key-value values by pasting them to variables in the form of a map

value, required, defaultValue property
@RequestParam(value = "change", required = "true", defaultValue = "none") String str
 You can specify a key name with the value property. The required property specifies whether the parameter is required. If the property value is true, the parameter must be passed, and if not, it will cause an error. The defaultValue property allows you to specify the string to use when the parameter is not passed.


@ModelAttribute

ModelAttribute is an annotation that attaches to an object, which allows you to store parameter values by matching the value to the field of the object.

example
@Controller
public class MvcController {
 
    @RequestMapping("/mvc.do")
    public String mvcInit(@ModelAttribute BoardVO boardVO) {
     
        boardVO.getNumber(); // number값 반환
     return null;
    }
}
If a BoardVO object with @ModelAttribute has a field called number, you save the value 1 value with the parameter key=number, value=1. The BoardVO object must have a getter, setter for each field.

No comments:

Post a Comment