Spring MVC -- 返回数据(静态页面+非静态页面+JSON对象+请求转发与请求重定向)
发布人:shili8
发布时间:2025-02-17 00:36
阅读次数:0
**Spring MVC —— 返回数据**
在 Spring MVC 中,返回数据是非常重要的一部分。我们可以通过各种方式来返回数据,如静态页面、非静态页面、JSON 对象等。在本文中,我们将详细介绍这些方面。
###1. 静态页面静态页面是指不需要经过任何逻辑处理的页面,直接从硬盘读取即可。Spring MVC 提供了两种方式来返回静态页面:一种是使用 `@RequestMapping` 注解,另一种是使用 `ViewResolver`。
####1.1 使用 @RequestMapping 注解我们可以在控制器中使用 `@RequestMapping` 注解来指定返回的静态页面。
java@Controllerpublic class IndexController { @RequestMapping("/") public String index() { return "index"; // 返回 index.html 页面 } }
####1.2 使用 ViewResolver我们可以在 Spring MVC 配置文件中配置 `ViewResolver` 来返回静态页面。
xml<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean>
然后,我们可以在控制器中使用 `ModelAndView` 来返回静态页面。
java@Controllerpublic class IndexController { @RequestMapping("/") public ModelAndView index() { ModelAndView modelAndView = new ModelAndView("index"); return modelAndView; } }
###2. 非静态页面非静态页面是指需要经过逻辑处理的页面,不能直接从硬盘读取。Spring MVC 提供了两种方式来返回非静态页面:一种是使用 `@RequestMapping` 注解,另一种是使用 `ModelAndView`。
####2.1 使用 @RequestMapping 注解我们可以在控制器中使用 `@RequestMapping` 注解来指定返回的非静态页面。
java@Controllerpublic class IndexController { @RequestMapping("/") public String index(Model model) { // 逻辑处理 model.addAttribute("message", "Hello, World!"); return "index"; // 返回 index.jsp 页面 } }
####2.2 使用 ModelAndView我们可以在控制器中使用 `ModelAndView` 来返回非静态页面。
java@Controllerpublic class IndexController { @RequestMapping("/") public ModelAndView index() { ModelAndView modelAndView = new ModelAndView("index"); modelAndView.addObject("message", "Hello, World!"); return modelAndView; } }
###3. JSON 对象JSON 对象是指使用 JSON 格式的数据。Spring MVC 提供了两种方式来返回 JSON 对象:一种是使用 `@ResponseBody` 注解,另一种是使用 `JsonViewResolver`。
####3.1 使用 @ResponseBody 注解我们可以在控制器中使用 `@ResponseBody` 注解来指定返回的 JSON 对象。
java@Controllerpublic class IndexController { @RequestMapping("/") @ResponseBody public Mapindex() { Map map = new HashMap<>(); map.put("message", "Hello, World!"); return map; } }
####3.2 使用 JsonViewResolver我们可以在 Spring MVC 配置文件中配置 `JsonViewResolver` 来返回 JSON 对象。
xml
然后,我们可以在控制器中使用 `ModelAndView` 来返回 JSON 对象。
java@Controllerpublic class IndexController { @RequestMapping("/") public ModelAndView index() { ModelAndView modelAndView = new ModelAndView("json"); Mapmap = new HashMap<>(); map.put("message", "Hello, World!"); modelAndView.addObject(map); return modelAndView; } }
###4. 请求转发与请求重定向请求转发和请求重定向是两种不同的方式来跳转到其他页面。
####4.1 请求转发我们可以在控制器中使用 `RequestDispatcher` 来实现请求转发。
java@Controllerpublic class IndexController { @RequestMapping("/") public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/views/other.jsp"); dispatcher.forward(request, response); return null; } }
####4.2 请求重定向我们可以在控制器中使用 `HttpServletResponse` 来实现请求重定向。
java@Controllerpublic class IndexController { @RequestMapping("/") public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect("/WEB-INF/views/other.jsp"); return null; } }
以上就是 Spring MVC 中返回数据的各种方式。通过这些方式,我们可以灵活地处理不同的业务逻辑并返回相应的页面或 JSON 对象。