servlet页面跳转的几种方式,servlet为什么要跳转

首页 > 实用技巧 > 作者:YD1662024-01-15 22:24:38

转发和重定向的页面跳转方式

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>main.jsp</title> </head> <body> <h2>main......page......</h2> </body> </html>

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>index.jsp</title> </head> <body> <h2>4种转发和重定向的方式</h2> <hr> <a href="${pageContext.request.contextPath}/one.action">1.普通转发页面(对请求的默认处理方式)</a><br><br> <a href="${pageContext.request.contextPath}/two.action">2.action转发页面</a><br><br> <a href="${pageContext.request.contextPath}/three.action">3.普通重定向页面</a><br><br> <a href="${pageContext.request.contextPath}/four.action">4.action重定向页面</a> </body> </html>

package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class JumpAction { /** 1. 之前在springmvc.xml中配置了视图解析器,这是SpringMVC处理页面跳转的默认方式,属于普通转发跳转 会将页面转发到action方法的返回值和前缀后缀拼接形成的路径所对应资源页面 */ @RequestMapping("/one") public String one(){ System.out.println("one action被访问......"); return "main"; } /** 2. * 下面是我们注册的视图解析器的父类:UrlBasedViewResolver,中的几个参数 * 通过对底层源代码的解读,可知在action方法的返回值字符串中,如果以"redirect:"或者"forward:"开头则不会执行视图解析器的路径拼接 * 而是会按照redirect或forward完成页面重定向或页面跳转 * * public static final String REDIRECT_URL_PREFIX = "redirect:"; * public static final String FORWARD_URL_PREFIX = "forward:"; * private String prefix = ""; * private String suffix = ""; * * 注意:不管要使用action的页面转发或者是action的页面重定向,由于action方法是控制器内部的方法 * 所以要想访问action方法必须访问到控制SpringMVC控制器,而要访问控制器,前提是要能被SpringMVC核心处理器处理(也就是底层的servlet) * 而要想被底层servlet处理,必须满足请求路径的通配条件,这是我们在web.xml文件中配置好的"*.action" * 所以要在请求的末尾加上".action"以满足请求的通配要求,才有资格被交给SpringMVC的控制器中的方法处理 */ @RequestMapping("/two") public String two(){ System.out.println("other action被访问......"); return "forward:/one.action"; } /** * 如果是普通重定向,直接重定向到项目资源,不需要控制器中的action方法的处理,请求路径后不用跟".action",直接写重定向的资源路径即可 * */ @RequestMapping("/three") public String three(){ System.out.println("three action被访问......"); return "redirect:/admin/main.jsp"; } /** * 要在请求的末尾加上".action"以满足请求的通配要求,才有资格被交给SpringMVC的控制器中的方法执行 * */ @RequestMapping("/four") public String four(){ System.out.println("other action被访问...."); return "redirect:/three.action"; } }

servlet页面跳转的几种方式,servlet为什么要跳转(1)

servlet页面跳转的几种方式,servlet为什么要跳转(2)

servlet页面跳转的几种方式,servlet为什么要跳转(3)

servlet页面跳转的几种方式,servlet为什么要跳转(4)

servlet页面跳转的几种方式,servlet为什么要跳转(5)

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.