SpringMVC转发及重定向
基础环境搭建请参考SringMVC入门程序
1:springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.applesnt.controller"/>
<!-- 让Spring MVC不处理静态资源 -->
<mvc:default-servlet-handler />
<!--annotation-driven配置帮助我们完成处理器映射器和处理器适配器-->
<mvc:annotation-driven />
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2:controller
com\applesnt\controller\HelloController.java
package com.applesnt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class HelloController {
/*走视图解析器转发*/
@RequestMapping("/t1")
public String t1(HttpServletRequest request, HttpServletResponse response, Model model){
HttpSession session = request.getSession();
model.addAttribute("msg","走视图解析器 sessionID = "+session.getId());
//无视图解析器 就要写全路径
return "hello";
}
/*不走视图解析器转发 关键字:forward*/
@RequestMapping("/t5")
public String t5(HttpServletRequest request, HttpServletResponse response, Model model){
HttpSession session = request.getSession();
model.addAttribute("msg","不走视图解析器 sessionID = "+session.getId());
//无视图解析器 就要写全路径
return "forward:/WEB-INF/jsp/hello.jsp";
}
/*不走视图解析器重定向 关键字:redirect*/
@RequestMapping("/t6")
public String t6(HttpServletRequest request, HttpServletResponse response, Model model){
//无视图解析器 就要写全路径
return "redirect:/index.jsp";
}
/*serlvet方式转发 */
@GetMapping("/t2")
public void t2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
request.setAttribute("msg","serlvet方式转发 sessionID = "+session.getId());
request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request,response);
}
/*servlet方式输出*/
@GetMapping("/t3")
public void t3(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
response.getWriter().print("servlet方式输出 sessionID = "+session.getId());
}
/*servlet方式重定向*/
@GetMapping("/t4")
public void t4(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.sendRedirect("/index.jsp");
}
}
SpringMVC转发及重定向的更多相关文章
- SpringMVC转发和重定向区别!
在servlet中,转发和重定向是由request和response完成的.两者之间的区别请看我之前的文章.那么在springMVC中是如何完成的呢? /**转发**/ @RequestMapping ...
- SpringMVC 转发、重定向
转发.重定向到其它业务方法 @org.springframework.stereotype.Controller @RequestMapping("/userController" ...
- springmvc转发与重定向
摘自http://elf8848.iteye.com/blog/875830 (1)我在后台一个controller跳转到另一个controller,为什么有这种需求呢,是这样的.我有一个列表页面,然 ...
- springMVC之一(页面<--->控制器 互相传值,转发和重定向)
#页面--->控制器1.request:不建议使用2.使用属性传值(建议使用)@RequestParam("name") String username3.使用Bean对象传 ...
- SpringMVC中的重定向和转发的实现
1.请求转发和重定向的区别 请求重定向和请求转发都是web开发中资源跳转的方式. 请求转发是服务器内部的跳转 地址栏比发生变化 只有一个请求相应 可以通过request域对跳转目标的请求 请求重定向是 ...
- SpringMVC中使用forward和redirect进行转发和重定向以及重定向时如何传参详解
转自:http://blog.51cto.com/983836259/1877188 2016-11-28 09:45:59 如题所示,在SpringMVC中可以使用forward和redirec ...
- 【drp 12】再识转发和重定向:SpringMVC无法跳转页面
最近再使用SpringMVC进行页面跳转的时候,不知道发生了什么,始终都无法正确跳转.后来问题解决了,发现是对于转发和重定向没有能很好的理解,以此写篇博客,权当做积累了! 声明:本博客的所有代码,均为 ...
- SpringMVC框架——转发与重定向
网上摘取一段大神总结的转发与重定向的区别,如下: 转发(服务端行为) 形式:request.getRequestDispatcher().forward(request,response) 转发在服务 ...
- Spring MVC温故而知新 – 参数绑定、转发与重定向、异常处理、拦截器
请求参数绑定 当用户发送请求时,根据Spring MVC的请求处理流程,前端控制器会请求处理器映射器返回一个处理器,然后请求处理器适配器之心相应的处理器,此时处理器映射器会调用Spring Mvc 提 ...
随机推荐
- 洛谷5026 Lycanthropy 差分套差分
题目链接 https://www.luogu.com.cn/problem/P5026 题意 在一个长度为m的序列中,每次给一个下标x,和一个权值v,然后从x-v*3到x-v*2单调递增,从x-v*2 ...
- 在Windows中安装OpenCV-Python |四
目标 在本教程中 我们将学习在你的Windows系统中设置OpenCV-Python. 下面的步骤在装有Visual Studio 2010和Visual Studio 2012的Windows 7- ...
- 深度学习vs机器学习 | 这些本质区别你知道多少?
目录: 数据相关性 硬件依赖性 特征工程 解决问题方法 执行时间 可解释性 一.数据相关性 深度学习与传统机器学习最重要的区别是,随着数据量的增加,其性能也随之提高.当数据很小的时候,深度学习算法并不 ...
- 使用vue-cli脚手架创建vue项目
使用vue-cli脚手架创建vue项目 首先,你已经安装了node. 使用vue-cli@2 创建项目 执行 命令: npm i -g vue-cli@2 //全局安装vue-cli@2脚手架 : v ...
- Unity 游戏框架搭建 2019 (二十三) 备份与版本号&危险的操作
先列出上一篇的总结: 要做的事情: 备份:导出文件,并取一个合理的名字. 遗留问题: 第八个示例与之前的示例代码重复,功能重复. 约定和规则: 每个示例在 QFramework 目录下创建一个文件夹, ...
- Vertica的这些事(十二)——-vertica备份与恢复
最近在使用vertica,上网找了很多资料都没有,只有自己看官方文档动手搞一下了,今天搞了vertica的备份与恢复 以下是整理的过程,分享给大家,如有问题欢迎大家指正~ 可加QQ群交流:412191 ...
- 构建LNMP
构建LNMP 案例1:部署LNMP环境 案例2:构建L ...
- 搭建mariadb数据库系统《一》
搭建mariadb数据库系统 案例3:搭建mariadb数据库系统 3 ...
- Java第二十六天,多线程等待换新机制(严格执行化)
代码: 1.老板类: package com.lanyue.day26; public class bossRunnable implements Runnable { public myLock l ...
- JS 浏览器BOM-->open() 方法
1.定义和用法 open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口. 语法: window.open(URL,name,specs,replace) 参数: URL:打开指定的页面的U ...