SpringMVC page passes to Controller

Keywords: Java Attribute

I have read many posts on the Internet about SpringMVC pages passing to Controller, most of which are summarized in the following categories:

1. Use the value corresponding to the name attribute of the related element in the page form as a parameter in the Controller method.

This should be the most direct method that I used when I read the book to retrieve content from Baidu's editor:

<!--page-->
<
form action="<%=basePath%>saveUeditorContent" method="post"> <!-- Container to load editor --> <div style="padding: 0px;margin: 0px;width: 100%;height: 100%;" > <script id="container" name="content" type="text/plain"> </script> <!--why dose this use script tag here???--> </div> <input name="test_input" value="hengha"> <button type="submit"> Preservation</button> </form>

 

    //Controller
    @RequestMapping(value="/saveUeditorContent")
    public ModelAndView saveUeditor(String content, String test_input){
        ModelAndView mav = new ModelAndView("myJSP/test03");
        //addObject Method sets the object to be passed to the view
        mav.addObject("content", content);
        mav.addObject("input_content", test_input);
        //Return ModelAndView The object jumps to the corresponding view file.The set parameters are also passed to the view
        return mav;
    }

 

2. Bind the value corresponding to the name attribute of the related element in the page form to the parameter in the Controller method through @RequestParam.

For URL strips? Scenarios, /saveUeditorContent? Content=123&input_content=456, can the parameter be set if required, default value

    @RequestMapping(value="/saveUeditorContent")
    public ModelAndView saveUeditor(@RequestParam(value="content",required=true,defaultValue="123") String content, @RequestParam("test_input") String input_content){
        ModelAndView mav = new ModelAndView("myJSP/test03");
        mav.addObject("content", content);
        mav.addObject("input_content", input_content);
        return mav;
    }

 

3. Obtain the parameters in the {variable} bound Controller method brought in by the URL path in @RequestMapping through @PathVariable.

Used for URL direct pass-through scenarios, /saveUeditorContent/123/456

    @RequestMapping(value="/saveUeditorContent/{content}/{test_input}")
    public ModelAndView saveUeditor(@PathVariable("content") String content, @PathVariable("test_input") String input_content){
        ModelAndView mav = new ModelAndView("myJSP/test03");
        mav.addObject("content", content);
        mav.addObject("input_content", input_content);
        return mav;
    }

 

4. Create a POJO object with setter and getter methods that corresponds to the related elements in the page form as a parameter in the Controller method.

//Too late is not particularly ripe

5. Use the HttpServletRequest object as a parameter in the Controller method.

    @RequestMapping(value="/saveUeditorContent")
    public ModelAndView saveUeditor(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("myJSP/test03");
        mav.addObject("content", request.getParameter("content"));
        mav.addObject("input_content", request.getParameter("test_input"));
        return mav;
    }

 

Constraint Description:

A) Parameters in 1, RequestParam in 2, {parameter} in URL in 3, PathVariable("parameter"), POJO object attribute in 4, and request.getParameter in 5 all need to match values corresponding to the name attribute of the related element in the foreground page.

B) RequestParam in 2 (Parameter), PatVariable in 3 (Parameter) binds to the following parameter, and the background can change the parameter name as needed.

Posted by krupapatel on Thu, 16 May 2019 09:57:31 -0700