SpringMVC跳转结果的方式

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

页面:视图解析器的前缀+viewname+视图解析器的后缀

转发方式

@Override    
    public ModelAndView handleRequest(HttpServletRequest req, 
    	    HttpServletResponse resp) throws Exception {
    	ModelAndView mv = new ModelAndView();
    	//封装要显示到视图中的数据
    	mv.addObject("msg","hello springmvc");
    	//视图名
    	mv.setViewName("hello"); //WEB-INF/jsp/hello.jsp
    	return mv;
      }


2.通过ServletAPI对象来实现,不需要视图解析器。

通过HttpServletResponse来进行输出。

@RequestMapping("/hello")//映射
public void hello(HttpServletRequest req, HttpServletResponse resp) throws IOException{
      resp.getWriter().println("hello spring mvc use httpservlet api");
   }	

通过HttpServletResponse实现重定向转发

@RequestMapping("/hello")//映射
public void hello(HttpServletRequest req, HttpServletResponse resp) throws IOException{
	//resp.getWriter().println("hello spring mvc use httpservlet api");
	//实现重定向
	resp.sendRedirect("index.jsp");
  }	

通过HttpServletRequest实现转发

@RequestMapping("/hello")//映射
public void hello(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
	//resp.getWriter().println("hello spring mvc use httpservlet api");
	//实现重定向
	//resp.sendRedirect("index.jsp");
	//实现转发
	req.setAttribute("msg", "sevlet api forward");
	req.getRequestDispatcher("index.jsp").forward(req,resp);
  }	


3.通过spring mvc 来实现转发和重定向----没有视图解析器

转发的实现1

@RequestMapping("/hello1")
public String hello(){
	//转发
	return "index.jsp";
  }

转发的实现2

@RequestMapping("/hello1")
public String hello(){
	//转发1
	//return "index.jsp";
	//转发2
	System.out.println("转发2");
	return "forward:index.jsp";
  }

实现重定向

@RequestMapping("/hello1")
public String hello(){
	//转发1
	//return "index.jsp";
	//转发2
	//return "forward:index.jsp";
	//重定向
	return   "redirect:index.jsp";
  }


4.通过spring mvc 来实现转发和重定向----有视图解析器

转发方式

@RequestMapping("/hello2")
public String hello2(){
	return "hello";
  }

注意:

重定向 return "redirect:index.jsp";不需要视图解析器

return "redirect:hello.do";//定向到hello.do

时间: 2024-08-10 00:04:52

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

[Spring MVC] - SpringMVC的各种参数绑定方式

SpringMVC的各种参数绑定方式 1. 基本数据类型(以int为例,其他类似):Controller代码: @RequestMapping("saysth.do") public void test(int count) { } 表单代码: <form action="saysth.do" method="post"> <input name="count" value="10" ty

ASP.NET页面跳转及传值方式

ASP.NET页面跳转相关知识 一.<a>标签 1. <a href=”test.aspx”></a> 2. 这是最常见的一种转向方法; 二.HyperLink控件 1. Asp.net 服务器端控件 属性NavigateUrl指定要跳转到的Url地址 2. NavigateUrl是可以在服务器端使用代码修改,这个区别于<a> 3. 由于HyperLink本身没有事件所以要在服务器端其它事件中设置NavigateUrl 4. 代码示例: <Asp:Hy

详解vue 路由跳转四种方式 (带参数)

详解vue 路由跳转四种方式 (带参数):https://www.jb51.net/article/160401.htm 1.  router-link ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1. 不带参数  <router-link :to="{name:'home'}"> <router-link :to="{path:'/home'}"> //name,path都行, 建议用name /

SpringMVC实战(三种控制器方式)

1.前言 上篇博客着重说了一下SpringMVC中几种处理映射的方式,这篇博客来说一下SpringMVC中几种经常使用的控制器.  2.经常使用控制器 2.1 ParameterizableViewController(參数控制器) <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"? > <beans xmlns="http

Springmvc跳转路径

forward转发地址栏不发生变化,redirect跳转地址栏变化,forward能把request域中的参数带给下一个,而redirect不会带过去,但是Springmvc的model虽然是基于request进行封装的,但是功能比request更强大,其跳转时也能把参数带过去,所以建议传参都使用model进行处理,Springmvc的转发还是跳转,前面是forward或redirect后接冒号,如果冒号后面紧跟着是/,代表是绝对路径,是从项目根下开始算,如果没加/,那么代表转发或跳转的是当前C

uploadify在火狐下上传不了的解决方式,java版(Spring+SpringMVC+MyBatis)具体解决方式

?? 因为技术选型的原因,在一个产品中.我选择了uploadify,选择它的原因是它有完好的技术文档说明(http://www.uploadify.com/documentation/),唯一不足的是官方文档上的样例是用php写的. 而对与我们这些使用Java语言的小生们而言,在遇到问题后发现找到一个适合自己的样例非常不好找.特别是当遇到浏览器兼容问题的时候,找了好久终于发现依照网上的方式进行编写了,终于还是会出现莫名其妙的问题,而继续查找资料的时候,发现解说的并不具体. 以下我就把我遇到的这些

网站开发中web页面跳转几种方式详解

在做web开发中,页面跳转的方式有很多种,然而有些时候这些跳转如何用到恰到好处却很容易被忽视. 客户端触发跳转有如下几种 使用meta元信息 <!--如下表示5秒后跳转到url指定的链接,推荐使用这种方式--> <meta http-equiv="refresh" content="5;url=http://my.oschina.net/ososchina/blog"> 2.使用javascript中的window.location对象 &l

SpringMVC基于代码的配置方式(零配置,无web.xml)

基于配置文件的web项目维护起来可能会更方便,但是有时候我们会有一些特殊的需求,比如防止客户胡乱更改配置,这时候我们需要给配置隐藏到代码中. 1.创建一个动态web项目(无需web.xml) 2.右键项目添加几个package: com.easyweb.config (保存项目配置) com.easyweb.controller (保存springMvc controller) 3.在 com.easyweb.config 新建一个类 WebApplicationStartup ,这个类实现We

springMVC的两种下载方式

1:通过httpServletResponse对象实现下载,觉得LOW的自行跳过 2:有人觉得既然使用的是MVC就要使用spring的方式