springMVC - 向前台传值

1.Model

Model是spring中的一个类,内部结构是一个map,可以批量存储内容。

index.jsp页面

<div>
        <form action="/annotation/Request" method = post>
            <!--action是提交路径,method是提交方式,form里面的元素的name属性要和controller中接收的对象类里的属性名一致-->
            账号信息:<br/>
            用户名:<input type="text" name = 'name'><br/>
            密码:<input type="text" name = 'password'><br/>
            用户信息:<br/>
            用户姓名:<input type="text" name = 'user.name'><br/>
            用户年龄:<input type="text" name = 'user.age'><br/>
            用户生日(yyyy-mm-dd):<input type="text" name = 'user.birthday'><br/>
            <input type ="submit" value = "提交到Request">
        </form>
    </div>

java的controller页面

    //Request
    @RequestMapping(path = "/Request")
    public String requestTest(Model model, Account account){  //Model 是spring中的一个类,里面有一个map可以存值和传递
        model.addAttribute("account1", account);//把前台传过来的的值赋值给Account对象(自己建的,属性和前台表单属性对应),存入model中,键值(key)为account1
        return "show";
    }

跳转后的show.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 10/22/2019
  Time: 1:19 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<html>
<head>
    <title>Show</title>
</head>
<body>
    <div>
        name = ${account1.name}
    </div>
    <div>
        name = ${account1.password}
    </div>
    <div>
        name = ${account1.user.name}
    </div>
    <div>
        name = ${account1.user.age}
    </div>
    <div>
        name = ${account1.user.birthday}
    </div>
</body>
</html>

输入:

输出:

 2. SessionAttribute

java

    //set
    @RequestMapping(path = "/SetRequest")
    public String setRequestTest(Model model, Account account){  //Model 是spring中的一个类,里面有一个map可以存值和传递
        model.addAttribute("account", account);//把前台传过来的的值赋值给Account对象,存入model中,键值(key)为account1
        return "show";
    }

    //get
    @RequestMapping(path = "/GetRequest")
    public String getRequestTest(ModelMap modelMap){  //modelMap 是spring中的一个类,继承了linkedHashMap
        System.out.println("" + modelMap.get("account"));
        return "show";
    }

index.jsp

    <div>
        <form action="/annotation/SetRequest" method = post>
            <!--action是提交路径,method是提交方式,form里面的元素的name属性要和controller中接收的对象类里的属性名一致-->
            账号信息:<br/>
            用户名:<input type="text" name = 'name'><br/>
            密码:<input type="text" name = 'password'><br/>
            用户信息:<br/>
            用户姓名:<input type="text" name = 'user.name'><br/>
            用户年龄:<input type="text" name = 'user.age'><br/>
            用户生日(yyyy-mm-dd):<input type="text" name = 'user.birthday'><br/>
            <input type ="submit" value = "提交到Request">
        </form>
    </div>
    <div>
        <a href="annotation/GetRequest">getRequest</a>
    </div>

show.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 10/22/2019
  Time: 1:19 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<html>
<head>
    <title>Show</title>
</head>
<body>
    <div>
        request域
    </div>
    <div>
        name = ${requestScope.account.name}
    </div>
    <div>
        password = ${requestScope.account.password}
    </div>
    <div>
        user.name = ${requestScope.account.user.name}
    </div>
    <div>
        user.age = ${requestScope.account.user.age}
    </div>
    <div>
        user.birthday = ${requestScope.account.user.birthday}
    </div>
    <div>
        Session域
    </div>
    <div>
        name = ${sessionScope.account.name}
    </div>
    <div>
        password = ${sessionScope.account.password}
    </div>
    <div>
        user.name = ${sessionScope.account.user.name}
    </div>
    <div>
        user.age = ${sessionScope.account.user.age}
    </div>
    <div>
        user.birthday = ${sessionScope.account.user.birthday}
    </div>
</body>
</html>

输入1

输出1

 可以看到session域中没有值,request域中有值

request域中model的生命周期只有一次request

输入2

输出2

user部分有值是因为之前

https://www.cnblogs.com/clamp7724/p/11714776.html

测试常用注解的时候写的预处理方法里赋的。

在controller class的上面加上注解

@SessionAttributes(value = {"account"}, types = {Account.class})

value是参数名,types是参数类型

这个注解会把model中的account属性和值存入session域,这个值不会随着request结束而消亡。在下次request的时候,如果model即使中没有account,SessionAttributes也会把account和值塞进model中。

加入注解后的整个class

package hello_word.controller;


import hello_word.domain.Account;
import hello_word.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.Annotation;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@SessionAttributes(value = {"account"}, types = {Account.class})
@Controller
@RequestMapping(path = "/annotation")
public class AnnotationController {//比如一个网站用户注册网站旗下游戏账号时,自己的信息可以直接从网站获得,不需要填写。所以可以用modelAttribute预处理数据
    //预处理部分,会在该class下的每个controller执行之前执行
    @ModelAttribute
    public Account dealWithNullAttribute(String name){
        System.out.println("新增用户:" + name);
        //模拟查询数据库后获得了user信息
        User user = new User();
        user.setName("张三");
        user.setAge(20);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date d = new Date();
        try {
            d = simpleDateFormat.parse("1990-05-26");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setBirthday(d);
        Account account = new Account();
        account.setUser(user);
        return account;
    }



    //set
    @RequestMapping(path = "/SetRequest")
    public String setRequestTest(Model model, Account account){  //Model 是spring中的一个类,里面有一个map可以存值和传递
        model.addAttribute("account", account);//把前台传过来的的值赋值给Account对象,存入model中,键值(key)为account1
        return "show";
    }

    //get
    @RequestMapping(path = "/GetRequest")
    public String getRequestTest(ModelMap modelMap){  //modelMap 是spring中的一个类,继承了linkedHashMap
        System.out.println("" + modelMap.get("account"));
        return "show";
    }

}

输入1

输出1

输入2

输出2

 可以看到,第二次只是单纯的链接,但是还是取到了输入1 保存的值

如果想清空session

可以加方法

    //clean
    @RequestMapping("/CleanRequest")
    public String cleanRequestTest(SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "show";
    }

执行后

猜你喜欢

转载自www.cnblogs.com/clamp7724/p/11719639.html