结果跳转方式

1. ModelAndView

设置ModelAndView对象, 根据view的名称, 和视图解析器, 跳转到指定的页面

页面的路径: {视图解析器前缀} + viewName + {视图解析器后缀}

视图解析器

<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

controller类

package com.wang.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //只要实现了Controller接口的类, 说明这就是一个控制器了
public class ControllerTest1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView(); //设置视图的属性
mv.addObject("msg", "ControllerTest1");
//设置跳转的页面
mv.setViewName("test"); return mv;
}
}

页面的路径: /WEB-INF/jsp/test.jsp

2. ServletAPI

在controller中可以使用response和request 因为SpringMVC本质也是一个servlet!

package com.wang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @Controller
public class ModelTest1 { @RequestMapping("/m1/t1")
public String test(HttpServletRequest req, HttpServletResponse resp) { System.out.println(req.getSession().getId()); return "hello"; } }

3. SpringMVC实现

1. 无需视图解析器

先将Spring配置文件中的视图解析器去掉

@Controller
public class ResultSpringMVC {
   @RequestMapping("/rsm/t1")
   public String test1(){
       //转发
       return "/index.jsp";
  }    @RequestMapping("/rsm/t2")
   public String test2(){
       //转发二
       return "forward:/index.jsp";
  }    @RequestMapping("/rsm/t3")
   public String test3(){
       //重定向
       return "redirect:/index.jsp";
  }
}

此时要写页面的全限定名

2. 使用视图解析器

在Spring的配置文件中配置视图解析器, 注意路径的前缀与后缀

@Controller
public class ResultSpringMVC2 {
   @RequestMapping("/rsm2/t1")
   public String test1(){
       //转发
       return "test";
  }    @RequestMapping("/rsm2/t2")
   public String test2(){
       //重定向
       return "redirect:/index.jsp";
       //return "redirect:hello.do"; //hello.do为另一个请求/
  } }

SpringMVC-结果跳转方式的更多相关文章

  1. Springmvc的跳转方式

    跳转到其他controller: 返回类型是String: return "forward:/log/home.action";  映射路径 跳转到本类Controller的某一个 ...

  2. springMVC学习 六 跳转方式

    SpringMVC的controller中的方法执行完之后,默认的跳转方式是请求转发 如果想要修改跳转方式,可以设置返回值字符串内容(1) 添加 redirect:资源路径 重定向 "red ...

  3. SpringMVC 04: SpringMVC中4种页面跳转方式

    转发和重定向的页面跳转方式 页面跳转方式,本质上只有2种方式:转发 + 重定向 但在SpringMVC的具体实现上,转发可以细分为:普通的页面转发 + 经由action方法的页面转发 重定向可以细分为 ...

  4. SpringMVC 三种异常处理方式

    SpringMVC 三种异常处理方式 在 SpringMVC, SpringBoot 处理 web 请求时, 若遇到错误或者异常,返回给用户一个良好的错误信息比 Whitelabel Error Pa ...

  5. JSP页面跳转方式

    JSP页面跳转方式 1.利用按钮+javascript进行跳转 <input type="button" name="button2" value=&qu ...

  6. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  7. ios中的界面跳转方式

    ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...

  8. springMvc的注解注入方式

    springMvc的注解注入方式 最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着 ...

  9. JS页面打开方式丶对话框及页面跳转方式

    一.js页面的三种打开方式 1. window.open 2. window.navigate("url") 跳转到目标页面 3. window.location.href=&qu ...

随机推荐

  1. C++ 内联(inline)函数

    目录 内联函数的使用方法 内联函数的使用规则 使用inline的时机 inline函数与宏函数的差异 inline是C++关键字,在函数声明或定义中,函数返回类型前加上关键字inline,即可以把函数 ...

  2. C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...

  3. Error:Failed to resolve: com.android.support:recyclerview-v7:28.0.0解决方法

    在使用Android Studio的过程中需要添加依赖recyclerview,出现报错: Unable to resolve dependency for ':app@debug/compileCl ...

  4. python设计模式之代理模

    python设计模式之代理模式 在某些应用中,我们想要在访问某个对象之前执行一个或多个重要的操作,例如,访问敏感信息--在允许用户访问敏感信息之前,我们希望确保用户具备足够的权限.操作系统中也存在类似 ...

  5. Enumerable 下又有新的扩展方法啦,快来一起一睹为快吧

    一:背景 1. 讲故事 前段时间将公司的一个项目从 4.5 升级到了 framework 4.8 ,编码的时候发现 Enumerable 中多了三个扩展方法: Append, Prepend, ToH ...

  6. axios 常用的几个方法

    Vue推荐使用axios 发送网络请求:最近重新开始做Vue项目,重新回归一下.从axios的几个方法开始吧. 1. 安装: 既然是Vue项目,我还是选择常用的npm的方式 $ npm install ...

  7. 准确率99.9%的离线IP地址定位库

    Ip2region是什么? ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nod ...

  8. linux系统相关参数查询(内存,磁盘,CPU)

    1.服务器型号:dmidecode -s system-product-name 出厂日期:dmidecode -s bios-release-date 2.磁盘大小:parted -l 3.物理内存 ...

  9. 关于make及makefile的工作笔记

    之前一直是用java的,最近工作中需要在Linux中写一个C++程序,之前的写法很不规范,只有一个CPP.记录一下关于makefile的相关知识 想要完整的了解相关内容,推荐看这本书<程序员的自 ...

  10. markdown插入表情

    找到了一个网站https://www.webfx.com/tools/emoji-cheat-sheet/,直接把表情对应的符号复制粘贴就行了. 比如:joy:显示为 部分截图: