1. 程式人生 > >springMVC接受json並開啟新頁面

springMVC接受json並開啟新頁面

背景:框架中,兩個web工程A,B,我的B工程開發了一個對外action介面,A來連,要實現的功能是,A的頁面發起一個action請求,到達B的springmvc,通過驗證後,開啟一個B工程新的tab的新視窗

方案:B用form提交json字串。A後臺用String param(不用@RequestBody取)後用vo轉化為物件,返回頁面引數採用redirectAttributes.addFlashAttribute隱藏傳輸,並用二次action轉發,A的頁面做window.location頁面跳轉

 

前提:

a.後端是不能新開啟瀏覽器頁面的。

b.如果後端要用@RequestBody收引數,請求type='post',dataType="json",contentType="application/json".

c.開啟新頁面只能由前端form提交,或者window.open來做,這兩個方法是沒有辦法做到@RequestBody的要求。

d.發ajax主要是用於發請求獲取資料,然後處理。可以用@ReqsuestBody接收,這是最普通的用途。可參考https://www.cnblogs.com/cainiao-Shun666/p/6557604.html

e.跨域問題可以通過A請求A的後臺,由後臺發起urlconnection請求,或者在B的controller裡@CrossOrigin來解決。可參考https://www.cnblogs.com/morethink/p/6525216.html

 

A工程jsp

<html>
  <head>
      <title>My JSP 'MyJsp.jsp' starting page</title>
      </head>
    <body>
    This is my JSP page. <br>
  <button id="bt_send" style="width: 300px;" type="submit" onClick="openCROSpage()">send Reuest</button></td></tr>
</body>
<script type="text/javascript">
function openCROSpage(){
     var req={
        name:"mike",
        password:"123456"
      };
     var tempForm
= document.createElement("form"); tempForm.id="tempForm1"; //set the way of sending request tempForm.method="post"; //tempForm.accept-charset="UTF-8"; //the url is used for "window.open"excute by action of form tempForm.action="page/login/winOpen.do"; //bind the parameter for "window.open" by attributes "target",such as window attributes tempForm.target="_blank"; //tempForm.enctype="multipart/form-data"; //set the url para by creation of hidden elements var hideInput = document.createElement("input"); // hideInput.type="hidden"; hideInput.name= "strParam"; hideInput.value= JSON.stringify(req); tempForm.appendChild(hideInput); //add the form into the page body document.body.appendChild(tempForm); //submit manually tempForm.submit(); //remove the temp form from the page body document.body.removeChild(tempForm);
    } </script>
</html>


B的controller

@Controller
@RequestMapping("/page/login")
public class LoginController {

  @RequestMapping(value = "/redirect.do")
    public String doRedirect(String strParam,RedirectAttributes redirectAttributes) {
         User user= new User((JSONObject) JSON.parse(strParam));
    
     redirectAttributes.addFlashAttribute("loginInfo", user};
redirectAttributes.addFlashAttribute("userId", "ID001"); redirectAttributes.addFlashAttribute("userName", "mike");      return "redirect:../public/winOpenSucc.do"; }   @RequestMapping(value = "/winOpenSucc.do") public String redirectPage() { return "../public/indexTest.jsp";//B的此頁面可用el取,即${loginInfo.name} } }

B的VO

package com.vdo;
import com.alibaba.fastjson.JSONObject;
public class User { String name; String password;
  public User(JSONObject obj){
    this.name=obj.getString("name");
    this.password=obj.getString("password");
  }
/** * 獲取{@link #name}屬性的值 * * @return {@link #name}屬性的值 */ public String getName() { return name; } /** * 設定{@link #name}屬性的值 * * @param name * 屬性值 */ public void setName(String name) { this.name = name; } /** * 獲取{@link #password}屬性的值 * * @return {@link #password}屬性的值 */ public String getPassword() { return password; } /** * 設定{@link #password}屬性的值 * * @param password * 屬性值 */ public void setPassword(String password) { this.password = password; } }


B的indexTest

<html>
<head>
</head>
<body>
</body>
<script type="text/javascript">
  //這裡可以對共公變數進行初始化,然後再進行頁面轉發
  project.userInfo.loginUser=${loginInfo.name};
  project.current.operatorName=${userName};
   var operatorId=${userId};
       window.location="page/public/reportList.jsp?id="+operatorId;//無此步則無法實現url跳轉,即url顯示為action
   </script >
<html>

為何要進行二次action轉發和jsp window.location跳轉,瞭解深入分析,可檢視我的另一篇文章https://www.cnblogs.com/pu20065226/p/10032048.html