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 提 ...
随机推荐
- 解决GPU显存未释放问题
前言 今早我想用多块GPU测试模型,于是就用了PyTorch里的torch.nn.parallel.DistributedDataParallel来支持用多块GPU的同时使用(下面简称其为Dist). ...
- 使用node.js中遇到的一些小bug
1.BUG Cannot set headers after they are sent to the client 解决:即发出一次请求得到两次或以上的回应时会出现此警告,此时注意查看再在一些条件下 ...
- iOS 设备尺寸与系统信息
参考 http://blog.csdn.net/newbieprogrammer/article/details/50569384 http://blog.csdn.net/developer_zha ...
- [noip模拟]食物中毒<暴搜+状压优化>
问题描述 Bqc经过一段时间的研究发现,要解这种毒需要一种特殊的药物.不幸的是,这种药物在 市面上不存在,没有办法Bqc只好亲自制得这种药物.它含有M种化学物质A1,A2,…,AM.现 在Bqc的手上 ...
- Bootstrap Blazor 组件库
项目介绍 Blazor 是一个使用 .NET 生成交互式客户端 Web UI 的框架: 使用 C# 代替 JavaScript 来创建丰富的交互式 UI. 共享使用 .NET 编写的服务器端和客户端应 ...
- C、Guard the empire(贪心)
链接:https://ac.nowcoder.com/acm/contest/3570/C 来源:牛客网 题目描述 Hbb is a general and respected by the enti ...
- 面试常问的 Java 虚拟机运行时数据区
写在前面 本文描述的有关于 JVM 的运行时数据区是基于 HotSpot 虚拟机. 概述 JVM 在执行 Java 程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以 ...
- 提示要安装Python-OpenSSL
PyOpenSSL是OpenSSL的python接口,用于提供加密传输支持(SSL),如果没用该模组,会导致goagent无法生成证书而影响使用. 若系统没有openssl,先安装openssl,** ...
- 好玩Python——PIL项目实训
PIL学习总结: 1. 2,PIL库概述: pil库可以完成图像归档和图像处理两方面功能的需求: 图像归档:对图像进行批处理,生成图像预览,图像转换格式等: 图像处理:图像基本处理,像素处理,颜色处理 ...
- 游戏开服 报一些 ip 设置 数据格式的异常,但断点明明都是数字 没问题的
游戏服开始起服,结果报乱七八招的错误,先 ccs 那 ip 有问题,我给直接注释掉了:然后又 报 KeyValueDictCache 中 ips 设置有问题,都是报格式错误,结果我断点明明都是数字结 ...