string返回值:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/11/12
  Time: 16:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h3>执行成功</h3>
${user.username}
${user.age}
</body>
</html>
package cn.mepu.service;

import cn.mepu.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @User 艾康
 * @create 2019-11-12 16:36
 */
@Controller
@RequestMapping("/service")
public class index {
    @RequestMapping("/index")
    public String indexName(Model model){
        System.out.println("查询用户名执行了");
        //用户名存入session对象中
        User user = new User();
        user.setUsername("艾康");
        user.setAge(22);
        model.addAttribute("user",user);
        return "success";
    }
}

void:默认值是请求路径文件

转发实现:

    @RequestMapping("/Forward")
    public void orwardTest(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("查询用户名执行了");
        //转发
        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
    }

重定向

    @RequestMapping("/Forward")
    public void orwardTest(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("查询用户名执行了");
        //重定向
        response.sendRedirect(request.getContextPath()+"/forward.jsp");
    }

直接写数据;

    @RequestMapping("/Forward")
    public void orwardTest(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("查询用户名执行了");//直接响应
        //1.设置中文乱码问题
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //2.写入数据
        response.getWriter().print("你好");
    }
}

ModelAndView返回值;

 @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView mv = new ModelAndView();
        //用户名存入session对象中
        User user = new User();
        user.setUsername("艾康");
        user.setAge(22);
        //存入
        mv.addObject("user",user);
        //跳转会找视图解析器
        mv.setViewName("success");
        return mv;
    }

重定向和转发:

   /**
     * 关键字方式跳转和转发
     * @return
     */
    @RequestMapping("/testModelAnd")
    public String testModelAnd(){

        //转发  return "forward:/WEB-INF/pages/success.jsp";
        //重定向
        return "redirect:/forward.jsp";

    }

springMVC相应之返回值的更多相关文章

  1. springMVC对于Controller返回值的可选类型

    2018-01-11 对于springMVC处理方法支持支持一系列的返回方式:  (1)ModelAndView (2)Model (3)ModelMap (4)Map (5)View (6)Stri ...

  2. 08.SpringMVC之方法返回值

    返回ModelAndView Controller类方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view.之前我就已讲过,在此并不过多赘述. 返回void 在Cont ...

  3. JAVA 框架 springmvc controller的返回值

    一.返回值:ModleView对象. 使用modelAndView.setViewName设置返回的页面.使用modelAndView.addObject设置返回的数据. @RequestMappin ...

  4. SpringMVC方法的返回值类型和自动装配

    1. void类型作为返回值类型 /** * 如果方法写成了void就跟原来servlet含义是差不多 的 * json */ @RequestMapping("/firstRequest& ...

  5. springMVC的controller返回值

    1.可以返回ModelAndView 2.可以返回一个String字符串:即一个jsp页面的逻辑视图名,这个在springMVC.xml中可以配置此页面逻辑视图的前缀和后缀 3.可以返回void类型: ...

  6. springmvc controller方法返回值

  7. SpringMVC核心——返回值问题

    一.SpringMVC 使用 ModelAndView 来处理返回值问题. 1.ModelAndView 官方描述: Holder for both Model and View in the web ...

  8. springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)

    HandlerMethodReturnValueHandler是用于对Controller中函数执行的返回值进行处理操作的,springMVC提供了多个HandlerMethodReturnValue ...

  9. SpringMVC-方法四种类型返回值总结,你用过几种?

    SpringMVC 现在算是 Java 领域的一个基础性框架了,很多人天天用,可是对于 SpringMVC 方法的返回值,你又是否完全清楚呢?今天松哥就来和大家聊一聊 SpringMVC 中四种不同类 ...

随机推荐

  1. 关于she'll脚本中"echo -e"使用sh命令执会显示参数"-e"-的问题

    今天尝试写了个简单的菜单shell脚本文件: clear echo echo -e "\t\t\tSys Admin Menu\n" echo -e "\t1. Disp ...

  2. pytorch中onehot编码转为普通label标签

    label转onehot的很多,但是onehot转label的有点难找,所以就只能自己实现以下,用的topk函数,不知道有没有更好的实现 one_hot = torch.tensor([[0,0,1] ...

  3. Flask+elasticsearch实现搜索引擎入门教程+Curl调试

    前几天,在github上看到了一个关于elasticsearch的小项目,有点小兴趣,于是就结合着Flask,研究了一下,分享给大家. 准备资料: 1.安装elasticsearch 参考教程:htt ...

  4. java web中的get和post(笔记)

    W3CSchool 在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST. GET - 从指定的资源请求数据. POST - 向指定的资源提交要被处理的数据 GET 方法 ...

  5. 字符串String的使用方法

    var ddd = "举头望明月,低头思故乡" document.writeln(ddd.split(''));//选择字符串中的一个标识符,将字符串分割成数组; var slic ...

  6. JAVA 实现Jacob语音播报

    准备工作:下载Jar 链接:https://pan.baidu.com/s/1edskJjYrCiefVJ7l3Ul9kQ     提取码:6dg9 ---导入jar 解压jar包,将jacob.ja ...

  7. elasticsearch Mapping使用自定义分词器

    创建索引及配置分析器 PUT /my_index { "settings": { "analysis": { "char_filter": ...

  8. Codeforces 1183H DP 计算子序列数目

    题意及思路:https://blog.csdn.net/mmk27_word/article/details/93999633 第一次见这种DP,有点像退背包的思想,如果发现有可能因为字母相同和前面算 ...

  9. Vector、ArrayList、LinkedList、CopyOnWriteArrayList区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11443907.html CopyOnWriteArrayList CopyOnWriteArrayLi ...

  10. POJ 1426 Find The Multiple (dfs??!!)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...