背景:框架中,两个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接收,这是最普通的用途。可参考SpringMVC 之@RequestBody 接收Json数组对象
e.跨域问题可以通过A请求A的后台,由后台发起urlconnection请求,或者在B的controller里SpringMVC解决跨域问题
来解决。可参考
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跳转,了解深入分析,可查看我的另一篇文章jsp取addFlashAttribute值深入理解即springMVC发redirect传隐藏参数