版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons)

在是springMVC的void的返回值中,有三大方法可以运行,个人觉得比较好运用。

第一种:请求转发的页面

 @RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response){
//请求转发的页面
try {
request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("执行了...");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

这是常见的一种方式,当目标页面在/WEB-INF文件夹下面,就可以通过请求转发的页面 。

第二种:重定向

 @RequestMapping("/testVoid2")
public void testVoid2(HttpServletRequest request ,HttpServletResponse response){
//重定向
try {
response.sendRedirect(request.getContextPath()+"/index.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

有时候在请求页面的时候,采用重定向是非常有必要的,重定向需要,处理掉原来的界面,重定向时需要拼接路径。

第三种:直接响应

@RequestMapping("/testVoid3")
public void testVoid3(HttpServletRequest request ,HttpServletResponse response){
//解决乱码
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
try {
//响应
response.getWriter().write("hello");
} catch (IOException e) {
e.printStackTrace();
}
return;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

这个效果就是在浏览器中输入指定的路径,就会把值传入到页面中。

实际情况开发就根据自己需求来用相应的方法。

                                </div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
</div>

posted on
2019-07-19 13:40 
xsDao 
阅读(...) 
评论(...) 
编辑 
收藏

SpringMVC返回void的三大方法的更多相关文章

  1. CompletableFuture.allOf that doens't return Void(CompletableFuture.allOf不能返回Void的解决方法)

    import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Ti ...

  2. .NET(C#):await返回Task的async方法

    众所周知,async方法只可以返回void,Task和Task<T>. 对于返回void的async方法,它并不是awaitable,所以其他方法不能用await方法来调用它,而返回Tas ...

  3. [转]SpringMVC日期类型转换问题三大处理方法归纳

    http://blog.csdn.net/chenleixing/article/details/45190371 前言 我们在SpringMVC开发中,可能遇到比较多的问题就是前台与后台实体类之间日 ...

  4. 《SpringMVC从入门到放肆》十一、SpringMVC注解式开发处理器方法返回值

    上两篇我们对处理器方法的参数进行了分别讲解,今天来学习处理器方法的返回值. 一.返回ModelAndView 若处理器方法处理完后,需要跳转到其它资源,且又要在跳转资源之间传递数据,此时处理器方法返回 ...

  5. SpringMVC的@RequestMapping和Controller方法返回值

    本节内容: @RequestMapping Controller方法返回值 一.@RequestMapping 通过@RequestMapping注解可以定义不同的处理器映射规则. 1. URL路径映 ...

  6. 11.SpringMVC注解式开发-处理器方法的返回值

    处理器方法的返回值 使用@Controller 注解的处理器的处理器方法,其返回值常用的有四种类型 1.ModelAndView 2.String 3.void 4.自定义类型对象 1.返回Model ...

  7. 阶段3 3.SpringMVC·_04.SpringMVC返回值类型及响应数据类型_3 响应之返回值是void类型

    定义先的标签 返回void 测试默认的跳转 虽然是404但是方法执行了. 默认请求了 testVoid.jsp这个页面.请求路径叫什么 就访问哪个jsp页面. 使用request请求转发 抛出的异常 ...

  8. springmvc 注解式开发 处理器方法的返回值

    1.返回void -Ajax请求 后台: 前台: 返回object中的数值型: 返回object中的字符串型: 返回object中的自定义类型对象: 返回object中的list: 返回object中 ...

  9. 【Java EE 学习 83 上】【SpringMVC】【基本使用方法】

    一.SpringMVC框架概述 什么是SpringMVC?SpringMVC是一个和Struts2差不多的东西,他们的作用和性质几乎是相同的,甚至开发效率上也差不多,但是在运行效率上SpringMVC ...

随机推荐

  1. Java中使用队列Queue

    示例代码: Queue<Integer> queue = new LinkedList<Integer>(); for (int i = 1; i <= 100; i + ...

  2. mysql删除某一个数据库中所有的表

    SELECT concat('DROP TABLE IF EXISTS ', table_name, ';') FROM information_schema.tables WHERE table_s ...

  3. 偶尔在网上看到的,相对比较好的c#端订单号生成规则

    偶尔在网上看到的,相对比较好的c#端订单号生成规则 public class BillNumberBuilder{     private static object locker = new obj ...

  4. 【Leetcode_easy】697. Degree of an Array

    problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...

  5. css+html 实现 光晕 光圈

    <style> html { height: 100%;}body { height: 100%; background-color: #000; margin: 0; padding: ...

  6. 【ARTS】01_38_左耳听风-201900729~201900804

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  7. iOS-UIAlertView与UIActionSheet

    UIAlertView与UIActionSheet 6.11.1 常规调用 UIAlertView:调出一个模态对话框,屏幕居中显示 UIActionSheet:非模态对话框,屏幕下方弹出 Alert ...

  8. jenkins的pipeline拉取指定分支的代码

    脚本示例 pipeline { agent any options { durabilityHint 'PERFORMANCE_OPTIMIZED' timeout(time:5, unit: 'MI ...

  9. npm的问题【解决】

    1.解决npm下载慢的问题,使用该命令 npm install --registry=https://registry.npm.taobao.org 好处:比起cnpm官网解释的,这个更好,使用cnp ...

  10. nginx location 优先级

    location 顺序/优先级:     location = > location 完整路径 > location ^~ 路径 > location ~,~* 正则顺序 > ...