基础环境搭建请参考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转发及重定向的更多相关文章

  1. SpringMVC转发和重定向区别!

    在servlet中,转发和重定向是由request和response完成的.两者之间的区别请看我之前的文章.那么在springMVC中是如何完成的呢? /**转发**/ @RequestMapping ...

  2. SpringMVC 转发、重定向

    转发.重定向到其它业务方法 @org.springframework.stereotype.Controller @RequestMapping("/userController" ...

  3. springmvc转发与重定向

    摘自http://elf8848.iteye.com/blog/875830 (1)我在后台一个controller跳转到另一个controller,为什么有这种需求呢,是这样的.我有一个列表页面,然 ...

  4. springMVC之一(页面<--->控制器 互相传值,转发和重定向)

    #页面--->控制器1.request:不建议使用2.使用属性传值(建议使用)@RequestParam("name") String username3.使用Bean对象传 ...

  5. SpringMVC中的重定向和转发的实现

    1.请求转发和重定向的区别 请求重定向和请求转发都是web开发中资源跳转的方式. 请求转发是服务器内部的跳转 地址栏比发生变化 只有一个请求相应 可以通过request域对跳转目标的请求 请求重定向是 ...

  6. SpringMVC中使用forward和redirect进行转发和重定向以及重定向时如何传参详解

    转自:http://blog.51cto.com/983836259/1877188 2016-11-28 09:45:59   如题所示,在SpringMVC中可以使用forward和redirec ...

  7. 【drp 12】再识转发和重定向:SpringMVC无法跳转页面

    最近再使用SpringMVC进行页面跳转的时候,不知道发生了什么,始终都无法正确跳转.后来问题解决了,发现是对于转发和重定向没有能很好的理解,以此写篇博客,权当做积累了! 声明:本博客的所有代码,均为 ...

  8. SpringMVC框架——转发与重定向

    网上摘取一段大神总结的转发与重定向的区别,如下: 转发(服务端行为) 形式:request.getRequestDispatcher().forward(request,response) 转发在服务 ...

  9. Spring MVC温故而知新 – 参数绑定、转发与重定向、异常处理、拦截器

    请求参数绑定 当用户发送请求时,根据Spring MVC的请求处理流程,前端控制器会请求处理器映射器返回一个处理器,然后请求处理器适配器之心相应的处理器,此时处理器映射器会调用Spring Mvc 提 ...

随机推荐

  1. Apache服务器故障排除攻略

    Apache服务器故障排除攻略 应用服务器Apache浏览器配置管理网络应用  随着网络技术的普及.应用和Web技术的不断完善,Web服务已经成为互联网上重要的服务形式之一.原有的客户端/服务器模式正 ...

  2. 使用Keras构建深度图像搜索引擎

    动机 想象一下,如果有数十万到数百万张图像的数据集,却没有描述每张图像内容的元数据.我们如何建立一个系统,能够找到这些图像的子集来更好地回答用户的搜索查询? 我们基本上需要的是一个搜索引擎,它能够根据 ...

  3. Error response from daemon: rpc error: code = AlreadyExists desc = name conflicts with an existing object: service myweb already exists

    主机环境 centos7.2 执行 docker service create --replicas 6 --name myweb -p 80:80 nginx:latest 时 报 Error re ...

  4. Fiddler抓取数据包分析案例

    案例:利用Fiddler抓取苏宁易购网站数据包分析 抓包软件:Fiddler4 请求名字:www.suning.com 详细内容: 一.了解数据包区域的字段含义 图1数据包区域 #:顺序号,按照抓包的 ...

  5. flask中的表单文件上传

    在表单中上传文件时,一定要加入 enctype="multipart/form-data" 属性 为什么要加入这个属性呢: enctype就是encodetype就是编码类型的意思 ...

  6. BZOJ 4472 salesman 题解

    题目 某售货员小T要到若干城镇去推销商品,由于该地区是交通不便的山区,任意两个城镇之间都只有唯一的可能经过其它城镇的路线.小T可以准确地估计出在每个城镇停留的净收益.这些净收益可能是负数,即推销商品的 ...

  7. JS面向对象介绍

    JS面向对象介绍 首先面向对象是什么?为什么要使用面向对象. 因为JavaScript对每个创建的对象都会自动设置一个原型(谷歌火狐中是proto),指向它的原型对象prototype,举个例子: f ...

  8. JDK12不包含JAXB-API

    ##用JDK12环境下 做EUREKA的的时候 报错如下 java.lang.TypeNotPresentException: Type javax.xml.bind.JAXBContext not ...

  9. Pointer Lock API(3/3):一个Demo

    简单的Demo演练 点击跳转至Code Pen以查看演示和源码 完整代码 <!DOCTYPE HTML> <html lang="en-US"> <h ...

  10. Linux命令后面加 & 的作用

    在命令的后面加一个 & 的作用是,将这个任务放到后台执行.看下面的例子. 输入gedit回车,可以看到,打开了Linux的文本编辑器,但是命令窗口执行不了其他命令了,只有退出文本编辑器才能继续 ...