基于from的跳转
0. 配置:自动扫描装载的controller包和视图解析器
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动扫描方式装载bean -->
<context:component-scan base-package="com.any.demoSpring.controller"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/html/"/>
<property name="suffix" value=".html"/>
</bean>
</beans>
1. 简单的基于form的请求跳转
<form action="postTest.do" method="post">
<input type="submit" value="Form提交" />
</form>
2. 后台
package com.any.demoSpring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping(value="postTest.do",method= RequestMethod.POST)
public String postTest(){
return "TEST2";
}
}
基于form的重定向
1. 这里的添加的前缀redirect:就是代表重定向的意思,意思是切换到新的的视图中,重定向Model不共享,URL会被改变。注意重定向的话是GET的请求,也不再适用所配置的spring视图解析,redirect:后面要写出完整的页面资源URL。
package com.any.demoSpring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping(value="postTest.do",method= RequestMethod.POST)
public String postTest(){
return "redirect:/html/TEST2.html";
}
}
又或者想隐藏完整资源的重定向URL可以这样做!redirect: 重定向一个GET请求(redirect.do)之后,再用这个请求返回经过视图解析器解析的视图TEST2!
@RequestMapping(value="postTest.do",method= RequestMethod.POST)
public String postTest(){
return "redirect:redirect.do";
}
@RequestMapping(value="redirect.do",method= RequestMethod.GET)
public String redirect(){
return "TEST2";
}
2. 如果是forward:表示转发。URL不变,且共享Model数据
3. 直接返回视图的话那肯定是共享model数据啦~
AJAX不支持后台跳转页面
ajax技术的是实现局部刷新并不是特地用来实现跳转的,当然我们可以实现跳转!根据ajax响应到的数据(头部或者内容)判断然后在前端进行跳转。(这里不做判断,当请求成功返回时候就跳转,使用同以上form相同的后台)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TEST</title>
</head>
<body>
<div>
<button id="btn">AJAX请求跳转</button>
</div>
<script src="../js/lib/jquery-3.2.1.js"></script>
<script>
$('#btn').click(function () {
var inVal = $('#inVal').val();
$.ajax({
url: "postTest.do",
type: "POST",
contentType:"application/text;charset=UTF-8",
success: function(data) {
window.location.href="redirect.do";
},
error: function() {
alert("请求出错!");
}
});
});
</script>
</body>
</html>
ps:ajax默认是异步请求,我们可以根据自己需求设置为同步请求!