每天学点SpringMVC-目标方法入参、"输出"以及赋值

1. Spring MVC会按请求参数名和POJO属性名称进行自动匹配,自动为该对象填充属性值,并且支持级联属性,代码实例如下:

    1.1 添加测试页面

 @RequestMapping("/page/test.do")
    public String getTest(){
        return "test" ;
    }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%--
  Created by IntelliJ IDEA.
  User: pawn
  Date: 2017/9/18
  Time: 13:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <base href="<%=basePath%>">
    <title>Test</title>
</head>
<body>

    <form action="test/testPojo.do">

        Account: <input name="account"> <br>
        Password : <input name="password">

        <input type="submit">

    </form>

</body>
</html>

     1.2 添加Controller类

/**
 * Created by pawn on 2017/9/18.
 */
@RequestMapping("/test")
@Controller
public class TestController {

    @RequestMapping("/testPojo")
    public void testPojo(User user){
        System.out.println("user : " + user);
    }
}

2  可以使用Servlet原生的API作为目标方法的参数,具体支持一下类型

    HttpServletRequest

  HttpServletResponse

    HttpSession

 Principal

 Locale

 InputStream

    OutputStream

    Reader

 Writer

3 目标方法的返回值可以是ModelAndView类型

   其中可以含视图和模型信息

   SpringMVC会把ModelAndView的model中的数据放入到requestScope中

4 目标方法的参数可以是Map类型或Model或ModelMap类型

   最终SpringMVC会把这些数据放入到requestScope中

5 @SessionAttributes 注解能实现将放入requestScope中的数据copy到sessonScope中

   5.1 value 属性指定对应的属性名

  5.2 type 属性指定对应数据的数据类型  

原文地址:https://www.cnblogs.com/xpawn/p/7542717.html