SpringMVC 传值

C得到数据后,跳转到V,并向V传递数据。进而V中可以渲染数据,让用户看到含有数据的页面

转发跳转:Request作用域

重定向跳转:Session作用域

Request和Session

@RequestMapping("/test1")
public String testData(HttpSession session,HttpServletRequest req,Integer id){
    session.setAttribute("user",new User());
    req.setAttribute("age", 18);
    //return "test2";
    return "forward:/WEB-INF/test2.jsp";
}

jsp中取值

//jsp中用EL表达式 取值即可
<fmt:formatDate value="${sessionScope.user.birth}" pattern="yyyy-MM-dd"/> <br/>
${sessionScope.user.birth} <br>
${requestScope.age}
<%@ taglib prefix="fmt"  uri="http://java.sun.com/jsp/jstl/fmt" %>

如果写日期形式 要在jsp引入上述标签

Model

//model中的数据,会在V渲染之前,将数据复制一份给request
@RequestMapping("/test")
public String testData(Model model){
    model.addAttribute("name", "张三");
    return "index";
}

//jsp中用EL表达式 取值即可
${requestScope.name}

ModelAndView

//modelandview 可以集中管理 跳转和数据
@RequestMapping("/test")
public ModelAndView testData(){//返回值类型为ModelAndView
    //新建ModelAndView对象
    ModelAndView mv = new ModelAndView();
    // 设置视图名,即如何跳转
    mv.setViewName("forward:/index.jsp");
    // 增加数据
    mv.addObject("age",18);
    return mv;
}

//jsp中用EL表达式 取值即可
${requestScope.age}

    @RequestMapping("/arg1")
    public String test1(Map map){
        map.put("name","李四");//相当于再request域中存值
        return "content";
    }

原生servlet

   @RequestMapping("/arg2")
    public String test2(HttpServletRequest request){//原生servlet  向域中传值
        String id = request.getParameter("id");

        System.out.println(id);
        request.setAttribute("age",18);
        return "forward:/content.jsp";
    }

@SessionAttributes

  • @SessionAttributes({"gender","name"}) :model,request中的 name和gender 会复制一份存入session中一份,但是原生servlet并不会复制,因为这个方法最终也是走原生servlet

  • SessionStatus 移除session

@Controller
@SessionAttributes({"gender","name"}) // model中的 name和gender 会存入session中
public class UserController {

    @RequestMapping("/hello")
    public String hello(Model m){
        m.addAttribute("gender",true); // 会存入session
        mv.addObject("name","zhj"); // 会存入session
        return "index";
    }
    
    @RequestMapping("/hello2")
    public String hello(SessionStatus status){
        // 移除通过SessionAttributes存入的session
        status.setComplete();
        return "index";
    }
}

 上面的 vaule是String或者Vaule是Date的都会存在session里一份。

 

 type={String.class,Date.class},第一次访问arg1时,会共享request,如李四,在发arg2时,李四也存在,先共享request在共享session

@ModelAttribute注解,里的map相当于放在implecModel这么一块空间,@ModelAttribute注解运行在/update运行之前,/update传过来的值,会吧map里的一 一覆盖,如果/update只传过来3个参数, 只能覆盖@ModelAttribute里的三个数,剩下一个用@ModelAttribute里面map的旧值

@Controller
public class UserController {


    @ModelAttribute
    public void getOne(Map map){
        System.out.println("查询数据库....");
        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setAge(18);
        user.setGender("男");

        map.put("aa",user);// implecModel   model

    }



    @RequestMapping("/update")
    public String update(@ModelAttribute("aa") User user){
        System.out.println(user);
        return "hello";
    }






}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值