今天主要写一下响应界面跳转的几种方式

1.在注解的方式中

1.1通过HttpServletResponse的API直接输出(不需要配置渲染器)

controller类的主要代码
@Controller
public class RequestController{@RequestMapping("/resp")public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {resp.getWriter().println("hello HttpServletResponse");}

  

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>

  

dispatcher-servlet.xml主要代码

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!--作用是扫描指定包下所有的包含注解的类--><context:component-scan base-package="com.sawshaw.mvc"/></beans>

  

1.2 使用HttpServletResponse 重定向到另一个视图(其他不变 )

  @RequestMapping("/resp")public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {resp.sendRedirect("index.jsp");}
}

  

1.3 使用HttpServletRequest 转发(默认访问/下的index.jsp页面 不受渲染器的影响)

@RequestMapping("/resp")public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {req.setAttribute("message","it's forword ");req.getRequestDispatcher("index.jsp").forward(req,resp);}

  

1.4直接返回jsp页面的名称(无渲染器)

其他的配置不变

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

  

1.5当有渲染器指定

 @RequestMapping("/nice")public String hello1(){//转发方式1return "home";//转发方式2return "forward:index";//重定向方式  hello指的是requsrmappingreturn "redirect:hello";}

  

2 使用view

2.1 使用modelandview

需要视图解析器 能指定跳转页面
public class HelloController implements Controller {@Overridepublic ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest,javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {ModelAndView mv = new ModelAndView();//封装要显示到视图的数据mv.addObject("msg","hello myfirst mvc");//视图名mv.setViewName("hello");return mv;}
}

  

[servlet-name]-servlet.xml

<!--配置渲染器--><!--配置hellocontroller中页面的位置--><bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /><bean id="viewResolver"class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><!--结果视图的前缀--><property name="prefix" value="/WEB-INF/jsp/"/><!--结果视图的后缀--><property name="suffix" value=".jsp"/>
</bean><bean name="/hello.do" class="com.jsu.mvc.HelloController"></bean>

  

2.2 使用modelview

不需要视图解析器 不能指定跳转页面

 //通过modelmap方式@RequestMapping("/modelmap")public String modelHello(String name,ModelMap map){map.addAttribute("name",name);System.out.println(name);return "index.jsp";}

  

结语

与君共勉!

转载于:https://www.cnblogs.com/JAYIT/p/9366302.html

springMVC 几种页面跳转方式相关推荐

  1. mui几种页面跳转方式对比

    mui几种页面跳转方式对比 http://blog.csdn.net/uikoo9/article/details/44676963 [几种打开页面的方式] 1.初始化时创建子页面 2.直接打开新页面 ...

  2. java 转jsp_【转】JSP三种页面跳转方式

    使用JSP大约有下列三种跳转方式: 1. response.sendRedirect(); 2. response.setHeader("Location","" ...

  3. 搞定iOS的几种页面跳转方式

    前言 对于新手来说,iOS的几种跳转方式还挺繁琐的,下面就来一一介绍 1.不带任何布局文件的viewController之间的跳转 MyViewController *vc = [[MyViewCon ...

  4. JavaWeb 页面跳转方式连接数据库

      今天是JavaWeb的第三节课,今天小编主要带大家学习页面跳转方式和连接数据库操作登录,大家有什么不懂的在下方评论或者私信,看到了一定会为大家解答的. 目录 一.界面跳转 JS中的跳转 Java中 ...

  5. JavaWeb 页面跳转方式

    文章目录 一.页面跳转方式 二.JDBC API 总结 一.页面跳转方式 (1)javascript方式跳转             window.location.href = "跳转的地 ...

  6. java跳转_java servlet 几种页面跳转的方法

    Servlet: 当然,在servlet中,一般跳转都发生在doGet, doPost等方法里面. 1) redirect 方式 response.sendRedirect("/a.jsp& ...

  7. java页面跳转t赋值_java servlet 几种页面跳转的方法及传值

    java web 页面之间传值有一下这几种方式 1.form 表单传递参数 2.url地址栏传递参数 3.session 4.cookie 5.application 6.通过隐藏域传值 7.通过Ja ...

  8. Java笔记:Java的三种页面跳转方法(setHeader,SendRedirect,forward)

    重定向 / 转发 在JAVA中进行资源跳转,或者是页面跳转,从本质上来讲,有两种方式:重定向 , 转发 这两者都可以使页面进行跳转,但是两者之间有不同的区别 其中 SendRedirect,setHe ...

  9. 微信小程序页面跳转方式+跳转小程序(直接复制代码可用)

    一. 微信小程序跳转页面方法 1.跳转到 tabBar 页面 wx.switchTab({url: '/index' }) 2.跳转到其他页面(非tabBar页) //redirectTo方法(会关闭 ...

最新文章

  1. 大公司病(太现实了!)
  2. 用户偏好类结构化数据分析题参赛总结
  3. everything服务器网页设置,Everything HTTP 服务器设置
  4. weblogic部署步骤
  5. STM32开发环境搭建
  6. php创蓝253四要素认证_PHP调用创蓝253国际短信验证码
  7. Linux自动化运维部署+运维
  8. 【视点】说好的光伏政策严肃性呢?
  9. Android中白天模式与夜间模式的切换
  10. 你好,李焕英!贾玲痛哭,一句话戳痛2.3亿中国人
  11. 微信小程序 实现打卡功能
  12. 计算机视觉(六):深度学习正则化
  13. 智能充电桩开发(一):系统总体设计概述
  14. 自媒体人平台运营保姆级教程!速看!
  15. crmeb是什么意思
  16. install.img制作方式
  17. MO,MT,Linkid的关系
  18. prt文件用什么软件打开(免费手机prt文件浏览器)
  19. 中恒蚁创解读短视频相比于传统的文字媒介有什么优势
  20. 多vlan实现互访,使用NAT、DHCP、ACL等技术实现公司组网

热门文章

  1. Android O限制系统全屏进一步遏制手机勒索
  2. 梳理各算法基础应用及场景
  3. anasys hpc集群_这可能是最简单的并行方案,如何基于 AWS ParallelCluster 运行 ANSYS Fluent...
  4. BZOJ 3720 [洛谷P2137] : Gty的妹子树
  5. linux nfs时间不对,NFS挂载主机或不稳定的原因与解决方法
  6. python生成静态html_Python写静态HTML
  7. python 循环添加array_Python的备忘细节小抄
  8. nohup 带参数_广州市西门子两通阀VVF42.40-25C+SKD62带断电
  9. [WEKA]如何将英文文本数据集转换为ARFF格式
  10. BZOJ-2440-完全平方数-中山市选2011-容斥原理-莫比乌斯函数-二分查找