Spring MVC learning summary (three): page parameter transfer in Spring MVC

1、使用HttpServletRequest

(1) Use a hyperlink to access the background URL in index.jsp.

<a href="${pageContext.request.contextPath}/parameterOperation01">使用HttpServletRequest和Session</a>

(2) Define the method to process the request in the Controller.

    @RequestMapping("/parameterOperation01")
    public String parameterOperation01(HttpServletRequest request){
        //获取session对象,并存入info信息
        HttpSession session = request.getSession();
        session.setAttribute("info","hello");
        //在request域中存入msg
        request.setAttribute("msg","你好");
        return "para";

    }

(3) Get data in para.jsp.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h4>Servlet方式:</h4>
使用HttpServletRequest得到的msg:${requestScope.msg}
使用Session得到的msg:${sessionScope.info}
</body>
</html>

The execution results are as follows:

2. Use Map, Model or ModelMap in the method

(1) Use a hyperlink to access the background URL in index.jsp.

<a href="${pageContext.request.contextPath}/parameterMap">在方法上使用Map</a>
<a href="${pageContext.request.contextPath}/parameterModel">在方法上使用Model</a>
<a href="${pageContext.request.contextPath}/parameterModelMap">在方法上使用ModelMap</a>

(2) Define the method to process the request in the Controller.

    /**
     * 方法入参为map
     * @param map
     * @return
     */
    @RequestMapping("/parameterMap")
    public String parameterMap(Map<String,Object> map){
        map.put("mapInfo", "我是map");
        return "para";
    }
    /**
     * 方法入参为Model
     * @param model
     * @return
     */
    @RequestMapping("/parameterModel")
    public String parameterModel(Model model){
        model.addAttribute("modelInfo", "我是Model");
        return "para";
    }
    /**
     * 方法入参为ModelMap
     * @param modelMap
     * @return
     */
    @RequestMapping("/parameterModelMap")
    public String parameterModelMap(ModelMap modelMap){
        modelMap.addAttribute("modelMapInfo", "我是ModelMap");
        return "para";
    }

(3) Get data in para.jsp.

<h4>Spring MVC方式:</h4>
方法入参为Map:${mapInfo}
方法入参为Model:${modelInfo}
方法入参为ModelMap:${modelMapInfo}

The execution results are as follows:

About Map, Model and ModelMap instructions:

(1) Map, Model and ModelMap can be used to store data without our manual creation, and they are only stored in the request field. If four fields are used to obtain values ​​in para.jsp, only the value of mapInfo can be obtained in the request field.

pageContext:${pageScope.mapInfo}<br>
request:${requestScope.mapInfo}<br>
session:${sessionScope.mapInfo}<br>
application:${applicationScope.mapInfo}<br>

The execution results are as follows: 

(2) The bottom layer of Map, Model and ModelMap all use BindingAwareModelMap.

Here we can briefly look at the source code of these classes:

a.BindingAwareModelMap继承ExtendedModelMap。

public class BindingAwareModelMap extends ExtendedModelMap {}

b. ExtendedModelMap inherits ModelMap and implements the Model interface.

public class ExtendedModelMap extends ModelMap implements Model {}

c. ModelMap inherits LinkedHashMap.

public class ModelMap extends LinkedHashMap<String, Object> {}

 d.LinkedHashMap implements the Map interface.

public class LinkedHashMap<K,V>
    extends HashMap<K,V>
    implements Map<K,V>
{}

Therefore, the class diagram between them is as follows:

 

3. The method return value uses ModelAndView

The role of ModelAndView is as follows:

a. Return to the specified page: The ModelAndView construction method can specify the name of the returned page, or jump to the specified page through the setViewName() method.

b. Return the required value: use addObject() to set the value that needs to be returned. addObject() has several methods with different parameters. You can default and specify the name of the returned object.

The use cases are as follows:

(1) Use a hyperlink to access the background URL in index.jsp.

<a href="${pageContext.request.contextPath}/parameterModelAndView">方法返回值为ModelAndView</a>

(2) Define the method to process the request in the Controller.

    @RequestMapping("/parameterModelAndView")
    public ModelAndView parameterModelAndView(){
       // 创建对象
       ModelAndView mv = new ModelAndView();
       //设置返回的页面名称
       mv.setViewName("para");
       //设置传递给页面的数据
        mv.addObject("mvInfo", "我是ModelAndView");
        return mv;
    }

(3) Get data in para.jsp.

方法返回值为:${mvInfo}

The execution results are as follows:

4. Use @ModelAttribute annotation

The method annotated with @ModelAttribute will be run prior to the method of @RequestMapping, and they will be in the same request domain.

The usage of @ModelAttribute annotation:

a.@ModelAttribute will inject the parameters passed by the client into the specified object by name on the parameters of the method, and automatically add it to the ModelMap.

Create the form:

<form method="post" action="${pageContext.request.contextPath}/modelAttribute01">
    书名: <input type="text" name="bName"><br>
    作者: <input type="text" name="bAuthor"><br>
    价格: <input type="text" name="bPrice"><br>
    <input type="submit" value="提交"><br>
</form>

Method of processing the request:

    @RequestMapping("/modelAttribute01")
    public String modelAttribute01(@ModelAttribute Book book,Model model){
        System.out.println(book);
        model.addAttribute("book",book);
        return "bookPage";
    }

BookPage.jsp receiving parameters:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
书名:${book.bName}
作者:${book.bAuthor}
价钱:${book.bPrice}
</body>
</html>

Enter values ​​in the form, and the execution results are as follows:

b.@ModelAttribute is used in the Controller method. At this time, it will be executed before each @RequestMapping marked method. If there is a return value, the return value will be automatically added to the ModelMap.

    @RequestMapping("/modelAttribute02")
    public String modelAttribute02(Book book){
        System.out.println(book);
        return "success";
    }
    @ModelAttribute
    public void getUser(Model model){
        Book book = new Book("<<西游记>>","吴承恩",98.67);
        model.addAttribute("book", book);
    }

 

 

Guess you like

Origin blog.csdn.net/weixin_47382783/article/details/113386500