[刘阳Java]_SpringMVC中ModelAndView的用途_第3讲版权声明

原创
小哥 3年前 (2022-11-11) 阅读数 7 #大杂烩

我们从前两个博客中了解到SpringMVC框架快速构建@RequestMapping今天我们将介绍注释的用法。SpringMVC框架中的ModelAndView。就我个人而言,我理解这一点,以便快速介绍SpringMVC你可以从以下几方面学习

  • SpringMVC快速构建框架环境。
  • @RequestMapping的用法
  • ModelAndView的用法
  • 整合Spring+SpringMVC+MyBatis
  • 然后在学习中SpringMVC框架高级部分
  1. ModelAndView它是什么,它做什么
  • 简单地理解,它是传递后台返回的数据。View层,它还包含View层的URL地址
  • 当控制器处理请求时,控制器通常会包含视图名称和一些模型属性。ModelAndView对象返回到DispatcherServlet因此,在控制器中构造一个。ModelAndView对象
  • ModelAndView作用
    • 设置转向地址
    • 存储(或封装)基础采集数据
    • 最后,将数据传递给View
  1. ModelAndView第一次使用,第一次创建。ModelAndView对象,然后通过其方法设置数据并转发视图名称。

package com.gxa.spmvc.controller;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;

import com.gxa.spmvc.entity.Student;

/**

  • SpringMVC的控制器(业务控制人)
  • 定义的方法是请求处理方法。
  • @author caleb
  • */ @Controller @RequestMapping("/user") public class TestController {

    /**

    • 利用ModelAndView转发数据,提供前端视图
    • @return */ @RequestMapping("/m06") public ModelAndView m06() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("m06"); modelAndView.addObject("message", "Hello World, Hello Kitty"); return modelAndView; }

}

  • setViewName(String viewName):Set a view name for this ModelAndView, to be resolved by the DispatcherServlet via a ViewResolver
  • addObject(String attributeName, Object attributeValue):通过key/value绑定数据的方法
  1. ModelAndView第二种方法可以直接通过带参数的施工方法。 ModelAndView(String viewName, String attributeName, Object attributeValue) 返回带有转发视图名称的数据。

package com.gxa.spmvc.controller;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;

import com.gxa.spmvc.entity.Student;

/**

  • SpringMVC的控制器(业务控制人)
  • 定义的方法是请求处理方法。
  • @author caleb
  • */ @Controller @RequestMapping("/user") public class TestController {

    /**

    • 利用ModelAndView转发数据,提供前端视图
    • @return */ @RequestMapping("/m07") public ModelAndView m07() { return new ModelAndView("m07", "message", "Hello World"); }

}

  1. ModelAndView第三种用法,设置重定向。

package com.gxa.spmvc.controller;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;

import com.gxa.spmvc.entity.Student;

/**

  • SpringMVC的控制器(业务控制人)
  • 定义的方法是请求处理方法。
  • @author caleb
  • */ @Controller @RequestMapping("/user") public class TestController {

    /**

    • ModelAndView默认转发
    • ModelAndView也可以设置重定向。
      1. 重定向另一个控制器
      1. 重定向特定jsp页面
    • @param name
    • @return */ @RequestMapping("/{name}/m07") public ModelAndView m07(@PathVariable String name) { if (!"admin".equals(name)) { return new ModelAndView("redirect:/m07.jsp"); } return new ModelAndView("m07"); }

}

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除