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. xampp环境下,配置Zend Studio调试php(XDebug) 转摘:http://www.cnblogs.com/tuyithief/archive/2011/06/02/2068431.html

    先说一下文件版本,xampp 1.7.4,php 5.3.5. 走了很多弯路,截止目前,ZendDebugger在php 5.3.x下,只有nts版本,既non Thread Safety(具体什么意 ...

  2. Matplotlib_key_point

    Matplotlib官方入门教程: http://www.labri.fr/perso/nrougier/teaching/matplotlib/ 本文参考教程: http://codingpy.co ...

  3. Python 第三天学习整理2

    一.常用的字符串方法 capitalize() #将字符串首字母大写 center(100,'*') #把字符串居中的 count(‘zhou’)#查询次数 endswith('.jpg ')#判断字 ...

  4. mysql协议解析

    目录 目录 1 交互过程 1.1 握手认证阶段 1.2 命令执行阶段 2 基本类型 2.1 整型值 2.2 字符串(以NULL结尾)(Null-Terminated String) 2.3 二进制数据 ...

  5. json书写格式

    1.数组方式 [ ] [{ "id" : 1 , "name" : "xiaoming" },{ "id" : 2 , ...

  6. django 在保存数据前进行数据校验

    我们想在保存用户进入数据库之前做一些字段的校验,先贴出代码: import re from django.db import models from django.db.models.signals ...

  7. NORDIC内核ARM蓝牙芯片NRF51802/NRF51822

    Nordic  nRF51 系列的IC 和协议堆栈对内存大小.封装类型.接口.周边产品及无线连接提供更多选择. 关于 nRF51 系列 多协议 2.4GHz 射频收发器拥有高性能.超低功耗以及灵活性等 ...

  8. bzoj 2015

    http://www.lydsy.com/JudgeOnline/problem.php?id=2015 裸最短路(' '     ) 不过我最初以为是mst (' '    ) #include & ...

  9. 彻底理解 JS 中 this 的指向

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...

  10. Adobe 2019 全家桶 Win 版

    Adobe Creative Cloud 2019 4月版已更新一段时间了,为了让广大Adobe的发烧友能够第一时间体验最新的产品,happy特意为大家收集到了最新的 Adobe CC 2019 全套 ...