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";

    }

最新文章

  1. .NET判断是否为AJAX提交
  2. Windows Server 2008 R2 WEB服务器配置系列文章索引
  3. (转)对比MS Test与NUnit Test框架
  4. python课程第一周重点记录
  5. 创建第一个JBPM6项目并且运行自带的helloword例子(JBPM6学习之三)
  6. 内存中 OLTP - 常见的工作负荷模式和迁移注意事项(二)
  7. imx6 android5.1 打开 调试串口
  8. JDK version
  9. 297. Serialize and Deserialize Binary Tree
  10. 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
  11. poj2337 欧拉路径
  12. 在Linux下开始C语言的学习
  13. BZOJ 1029 建筑抢修
  14. PL/SQL Developer 与tnsnames.ora
  15. WindowsForm 增 删 查 改
  16. 【Untiy3D 游戏开发之一】Unity3D For Window/Mac最新4.2.0版本破解教程
  17. 基于MFC简单图片裁剪工具
  18. Java基础之TCP与UDP
  19. 面试真题--------spring源码解析IOC
  20. spring mybatics

热门文章

  1. VS进程附加的使用
  2. Centos7 安装vscode
  3. 使用api获取数据————小程序
  4. SET TRANSACTION - 设置当前事务的特性
  5. 图形设计 X11
  6. zabbix自带database monitor
  7. 属性class与:class
  8. fiddler 抓取手机http/https包
  9. 配置框架spring和SpringDataJpa整合----员工是爹
  10. 每天一个Linux命令:rm(5)