SpringMVC之处理响应

发布时间:2023-12-21 21:56:59

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
SpringMVC之处理响应


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:
在 SpringMVC 框架中,处理响应是 Web 应用程序开发中至关重要的一环。它决定了客户端将收到什么样的信息,无论是成功的结果、错误提示还是其他类型的响应。在这篇博客中,我们将深入探讨 SpringMVC 中处理响应的各种方式。
无论你是 SpringMVC 的新手还是有经验的开发者,这篇博客都将为你提供有用的信息和实际的示例,帮助你更好地理解和应用 SpringMVC 中响应处理的相关知识。让我们开始探索 SpringMVC 的响应世界吧!


提示:以下是本篇文章正文内容,下面案例可供参考

一、配置视图解析器

在 SpringMVC 中,配置视图解析器是为了将控制器处理后的结果与相应的视图进行关联,以便渲染并展示给用户。
视图解析器的主要作用是根据请求的信息和控制器返回的逻辑视图名称,确定实际要渲染的视图资源。它解析逻辑视图名称,并将其转换为实际的视图文件路径或 URL。
通过配置视图解析器,SpringMVC 可以根据不同的请求和逻辑视图名称,选择合适的视图来呈现响应内容。这样可以实现页面的动态渲染,根据用户请求和业务逻辑展示相应的页面或数据。
InternalResourceViewResolver是SpringMVC的默认视图解析器,用来解析JSP视图。在springmvc.xml核心配置文件中添加以下内容

<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <!-- 视图前缀 -->
  <property name="prefix" value="/" />
  <!-- 视图后缀 -->
  <property name="suffix" value=".jsp" />
</bean>

二、处理响应

返回值为void

此时会跳转到名字是 前缀+方法路径名+后缀 的jsp页面
例如:

// 路径是helloMVC,方法执行完后会跳转到/helloMVC.jsp
@RequestMapping("/helloMVC")
public void helloMVC(){
  System.out.println("hello SpringMVC!");
}

路径是helloMVC,方法执行完后会跳转到/helloMVC.jsp

返回值为void

此时会跳转到名字是 前缀+返回值+后缀 的jsp页面
例如:

// 返回值为String
@RequestMapping("/c2/hello1")
public String helloMVC1(){
  System.out.println("hello SpringMVC!");
  // 方法执行完后会跳转到/helloMVC.jsp
  return "hello";
}

路径是helloMVC,方法执行完后会跳转到/hello.jsp

返回值为ModelAndView

这是SpringMVC提供的对象,该对象可以向request域设置数据并指定跳转的页面。该对象中包含Model对象和View对象。其中,Model对象用于向request域中设置数据。View对象用于指定跳转的页面,例如,如果View里面储存的是index,则跳转的路径为/index.jsp

// 返回值为ModelAndView
@RequestMapping("/c2/hello2")
public ModelAndView useMAV(){
  System.out.println("返回值类型为ModelAndView");
  // 1.创建ModelAndView对象
  ModelAndView modelAndView = new ModelAndView();
  // 2.获取Model对象,本质是一个Map
  Map<String, Object> model = modelAndView.getModel();
  // 3.使用Model对象向request域设置数据
  model.put("name","百战程序员");
  // 4.使用View对象设置跳转的路径为/baizhan.jsp
  modelAndView.setViewName("index");
  return modelAndView;
}

方法执行完后会跳转到/index.jsp

request域设置数据

使用原生的HttpServletRequest

@RequestMapping("/c2/hello3")
public String setRequestModel(HttpServletRequest request){
  request.setAttribute("username","张三");
  return "index";
}

使用Model、ModelMap

SpringMVC提供了Model接口和ModelMap类,控制器方法添加这两个类型的参数,使用该参数设置数据,该数据就会存到request域中。

@RequestMapping("/c2/hello4")
public String setRequestModel2(Model model, ModelMap modelMap){
  // 使用Model将数据存入request域
  // model.addAttribute("username","张三");
  // 使用ModelMap将数据存入request域
  modelMap.addAttribute("username","张三");
  return "index";
}

使用Map集合

Model接口底层就是一个Map集合,我们可以给控制器方法设置Map类型的参数,向Map中添加键值对,数据也会存到request域中。

@RequestMapping("/c2/hello5")
public String setRequestModel3(Map map){
  map.put("username","张三");
  return "index";
}

session域设置数据

@RequestMapping("/c2/hello6")
public String setSeesionModel(HttpSession session){
  session.setAttribute("address","北京");
  return "index";
}

context域设置数据

context作用域表示在整个应用范围都有效。在SpringMVC中对context作用域传值,只能使用ServletContext对象来实现。但是该对象不能直接注入到方法参数中,需要通过HttpSession对象获取。

@RequestMapping("/c2/hello7")
public String setContextModel(HttpSession session){
  ServletContext servletContext = session.getServletContext();
  servletContext.setAttribute("age",10);
  return "index";
}

请求转发、重定向

SpringMVC还提供了一种简单的请求转发和重定向的写法:

@RequestMapping("/c2/hello10")
public String myForward3(HttpServletRequest request){
  request.setAttribute("name","尚学堂");
  // 请求转发
  return "forward:/c2/hello9";
  // 重定向
  // return "redirect:/c2/hello9";
}


总结

提示:这里对文章进行总结:

通过学习这篇博客,你应该对 SpringMVC 处理响应的方法有了更深入的理解。希望这对你在开发 SpringMVC 应用程序时有所帮助。如果你有任何问题或需要进一步了解,请随时在评论中提问。

文章来源:https://blog.csdn.net/liubopro666/article/details/135139663
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。