SpringMVC 页面重定向

本文介绍了一个Spring MVC框架下的重定向示例,通过创建控制器实现从一个页面跳转到另一个页面的功能,并展示了如何配置DispatcherServlet以正确解析视图。

学习参考自易百教程

在这个例子springMVC static静态资源访问中修改完成

1、在controller包下添加RedirectController控制器

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/jsp")	//该类下所有方法的父级路径
public class RedirectController {
	
	@RequestMapping(value="/index")	//匹配为/jsp/index
	public String first(){
		return "index";
	}
	
	@RequestMapping(value="/redirect")
	public String two(){
		return "redirect:finalpage"; //将访问路径/jsp/redirect重定向为/jsp/finalpage
	}
	
	@RequestMapping(value="/finalpage")
	public String three(){
		return "final";		//响应返回final.jsp
	}
}

2、修改dispatcher-servlet.xml文件,添加访问前缀/jsp/。更改后,dispatchservlet将

http://localhost:8080/springMVC_linkToHTML/index访问路径映射匹配到第二个新建的controller中,

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
              <property name="prefix" value="/jsp/" />
              <!-- 定义视图后缀 -->
              <property name="suffix" value=".jsp" />
       </bean>

3、在Web_Root下新建jsp文件夹添加index.jsp、final.jsp文件

	<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
	<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	<html>
	  <head>   
	    <title>link to a another</title>
	  </head>
	  
	  <body>
	   <h2>Loading a another page.</h2>
	   <form:form action="/springMVC_linkToHTML/jsp/redirect">
	   		<input type="submit" value="turn to other page"/>
	   </form:form>
	  </body>
	</html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title> 'final.jsp'</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h2>This is final page. <br></h2>
  </body>
</html>

结果

### 实现Spring MVC POST 请求重定向 在Spring MVC框架中,实现POST请求后的重定向操作是一个常见的需求。当接收到`POST`请求并完成相应的业务逻辑处理之后,通常会希望将用户重定向到另一个页面或资源上。 对于带有路径变量的情况,可以在控制器方法内定义@RequestMapping注解来指定HTTP请求的方法类型以及URL模式,并利用字符串形式的视图名称作为返回值来进行重定向。例如: ```java @RequestMapping(value = "/files/{path}", method = RequestMethod.POST) public String upload(@PathVariable("path") String path, ...) { // 执行上传文件或其他业务逻辑... return "redirect:/view/files/" + path; // 或者更简洁的方式:"redirect:files/{path}"[^2] } ``` 这里需要注意的是,在执行重定向的过程中,如果想要保留某些数据供目标地址使用,则可以借助于`RedirectAttributes`对象传递临时性的属性给下一个请求。这有助于保持用户体验的一致性和连贯性而不必担心GET请求可能带来的重复提交问题。 另外一种情况是关于如何确保模型(Model)中的数据能够在重定向后仍然可用。由于默认情况下,原始请求的数据不会被自动携带至新的请求中,因此需要显式地向`RedirectAttributes`添加这些信息以便它们能在后续请求中访问得到[^3]。 ```java import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.support.RedirectAttributes; // ... @RequestMapping(method = RequestMethod.POST) public String handleFormSubmission(RedirectAttributes redirectAttrs){ // 处理表单... // 将消息添加到flash属性中 redirectAttrs.addFlashAttribute("message", "成功保存!"); return "redirect:/success"; } ``` 上述代码片段展示了如何通过`addFlashAttribute()`方法把特定的信息存储起来用于下一次请求显示出来。这种方式非常适合用来反馈操作结果或是其他短暂存在的状态提示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值