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默認是異步請求,咱們能夠根據本身需求設置爲同步請求!異步