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. SpringMVC学习(11):表单标签

    本篇我们来学习Spring MVC表单标签的使用,借助于Spring MVC提供的表单标签可以让我们在视图上展示WebModel中的数据更加轻松. 一.首先我们先做一个简单了例子来对Spring MV ...

  2. IntelliJ IDEA 常用快捷键和技巧

    IntelliJ Idea 常用快捷键列表 Alt+回车 导入包,自动修正Ctrl+N  查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码Ctrl+Alt+O 优化导入的类和 ...

  3. 【学习总结】Python-3-风格各异的数值类型实例

    菜鸟教程-Python3-基本数据类型 可能是考点的各种形态的数值类型 int型:正数负数,八进制0开头,十六进制0x开头 float型:小数点的前后都可以没有数字,自动补全 complex型:虚部的 ...

  4. html input标签 要求只能输入纯数字

    在input标签添加以下代码即可 oninput = "value=value.replace(/[^\d]/g,'')" <input type="text&qu ...

  5. WordPress 页面点击显不同颜色爱心

    在主题的页脚添加以下js即可实现 <script type="text/javascript"> /* * https://www.xianjieo.cn */ !fu ...

  6. 一、bootstrap-datepicker

    一.bootstrap-datepicker <!DOCTYPE html> <html> <head> <title></title> & ...

  7. vue 如何读取编译携带的参数

    vue 环境有很多套,我们需要根据不同环境设置不同的一些参数,如何不装任何依赖的情况下获取参数 下面是我制作官网,需要根据开发还是生产环境配置不同CDN,用vue-cli2+webpack,配置是再: ...

  8. 程序猿必备的10款web前端动画插件

    1.基于jQuery的瀑布流图片筛选插件 瀑布流的展现方式在目前的网页中用得越来越广泛,特别是图片和首页文章的动态加载. 今天分享的这款就是基于jQuery的瀑布流图片筛选插件,我们可以点击图片分类名 ...

  9. web storage 简单的网页留言版

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  10. foobar2000 频谱给我的win10 任务栏添加一点会动背景

    在任务栏右键,"任务栏设置",颜色 -> 透明效果, 然后 foobar2000 的 view -> layout -> Quick Setup,选择带有Visu ...