SpringMVC-结果跳转方式
结果跳转方式
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-结果跳转方式的更多相关文章
- Springmvc的跳转方式
跳转到其他controller: 返回类型是String: return "forward:/log/home.action"; 映射路径 跳转到本类Controller的某一个 ...
- springMVC学习 六 跳转方式
SpringMVC的controller中的方法执行完之后,默认的跳转方式是请求转发 如果想要修改跳转方式,可以设置返回值字符串内容(1) 添加 redirect:资源路径 重定向 "red ...
- SpringMVC 04: SpringMVC中4种页面跳转方式
转发和重定向的页面跳转方式 页面跳转方式,本质上只有2种方式:转发 + 重定向 但在SpringMVC的具体实现上,转发可以细分为:普通的页面转发 + 经由action方法的页面转发 重定向可以细分为 ...
- SpringMVC 三种异常处理方式
SpringMVC 三种异常处理方式 在 SpringMVC, SpringBoot 处理 web 请求时, 若遇到错误或者异常,返回给用户一个良好的错误信息比 Whitelabel Error Pa ...
- JSP页面跳转方式
JSP页面跳转方式 1.利用按钮+javascript进行跳转 <input type="button" name="button2" value=&qu ...
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- ios中的界面跳转方式
ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...
- springMvc的注解注入方式
springMvc的注解注入方式 最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着 ...
- JS页面打开方式丶对话框及页面跳转方式
一.js页面的三种打开方式 1. window.open 2. window.navigate("url") 跳转到目标页面 3. window.location.href=&qu ...
随机推荐
- 使用 VMware Workstation Pro 让 PC 提供云桌面服务——学习笔记(一)
最终效果: 能够通过xshell等终端设备, 远程访问虚拟机 操作步骤 1. 安装VMware 网上自行下载VMware 软件,这里不给出详细操作 2. 下载安装系统 这里使用 Centos 的lin ...
- 5年Java程序员,五面蚂蚁险拿offer定级P7,大厂面试不过如此?
当时面试能记下的就这些了,可能不太全请见谅: 一面 1.HashMap和ConcurrentHashMap: 2.再谈谈一致hash算法? 3.乐观锁还有悲观锁: 4.可重入锁和Synchronize ...
- 图论算法(一)存图与STL第六弹——vector容器
图论算法(一)存图 我发现我的博客阅读量贼低,问小伙伴们,ta们都说这些博客太长了QAQ! 今天来个短亿点的(也短不了多少……) 进入正题,图论究竟是什么? 图论就是给你一张图,让你在这张图上进行各种 ...
- 输入url后的加载过程~
1)查找域名对应的IP地址: 2)建立连接(TCP的三次握手): 3)构建网页: 4)断开连接(TCP的四次挥手): TCP的三次握手:为了准确无误的把数据送到目标处,TCP协议采用了三次握手策略,用 ...
- linux系统相关参数查询(内存,磁盘,CPU)
1.服务器型号:dmidecode -s system-product-name 出厂日期:dmidecode -s bios-release-date 2.磁盘大小:parted -l 3.物理内存 ...
- js 常用业务工具方法 (es5,es6)持续更新
数组去重 数组去重最原始的方法就是使用双层循环. es5: // 使用indexOf function unique(array) { var res = []; for (var i = 0, le ...
- 使用vim的妙招
使用F1执行文件 Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器. 我们Linux运维经常在Linux中使用到Vim编辑器,当使用Vim写shell脚本或者python脚本的时候,想要 ...
- Galera Cluster for MySQL 集群恢复
node1: 1.rm -rf grastate.dat 2.mysqld_safe --wsrep-recover 3.galera_new_cluster node2: systemctl res ...
- chrome浏览器截图
1.F12 打开开发者工具台 2.ctrl + shift + p,弹出搜索框之后输入: full 3.选中Mobile-- Capture fullsize screenshot ,成功保存图片.
- Ubuntu 下查看CPU 信息命令
from: http://hi.baidu.com/hermitinhistory/blog/item/ce64d5fb6b23b71b6d22eb95.html 查看当前操作系统内核信息 uname ...