0. 配置:自动扫描装载的controller包和视图解析器html
<?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. 后台java
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"; } }
1. 这里的添加的前缀redirect:就是表明重定向的意思,意思是切换到新的的视图中,重定向Model不共享,URL会被改变。注意重定向的话是GET的请求,也再也不适用所配置的spring视图解析,redirect:后面要写出完整的页面资源URL。jquery
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!web
@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数据ajax
3. 直接返回视图的话那确定是共享model数据啦~spring
ajax技术的是实现局部刷新并非特意用来实现跳转的,固然咱们能够实现跳转!根据ajax响应到的数据(头部或者内容)判断而后在前端进行跳转。(这里不作判断,当请求成功返回时候就跳转,使用同以上form相同的后台)app
<!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默认是异步请求,咱们能够根据本身需求设置为同步请求!异步