背景:框架中,两个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里@CrossOrigin来解决。可参考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传隐藏参数

springMVC接受json并打开新页面的更多相关文章

  1. Ajax异步打开新页面弹框被拦截,无法将参数值传递到后台

    一.Form提交,打开新页面被拦截 手动触发Form提交打开新页面是不会被拦截的,但是如果通过Ajax异步处理回调后再程序自动触发Form提交的话,就会被浏览器当成广告弹框拦截 1.暂时的解决办法:如 ...

  2. CefSharp中ChromiumWebBrowser打开新页面处理(_blank)

    原文:CefSharp中ChromiumWebBrowser打开新页面处理(_blank) 版权声明:欢迎转载,但是请保留出处说明 https://blog.csdn.net/lanwilliam/a ...

  3. iOS如何用代码控制以不同屏幕方向打开新页面?

    转载:http://blogread.cn/it/article/7765?f=wb#original 代码示例:https://github.com/johnlui/Swift-On-iOS/tre ...

  4. JavaScript关闭窗口的同时打开新页面的方法

    做网页的时候需要弹出一个小窗口,然后要实现一个功能就是鼠标点击超链接关闭小窗口并打开一个新页面,就如同下图: 这是一个小窗口,点击超链接这个窗口会关闭并且会正常在浏览器打开新页面,首先写js关闭窗口的 ...

  5. js打开新页面 关闭当前页 关闭父页面

    js打开新页面.关闭当前页.关闭父页面 2010-04-29 14:04:13|  分类: 页面与JavaScript |  标签: |字号大中小 订阅     //关闭当前页面,并且打开新页面,(不 ...

  6. DTcms手机版使用余额支付 提示信息跳转到PC版的错误。以及提交订单不打开新页面

    手机版使用余额支付 提示信息跳转到PC版的错误 引起错误的原因是中间需要提交到DTcms.Web\api\payment\balance\index.aspx去处理 导致BasePage.cs中的li ...

  7. js 打开新页面 window.open()

    利用js打开一个新页面,而不是一个新窗口. 在网上各种东西啊,蛋疼了半天,还白疼了.. 后来看到a标签有target属性,然后又发现window.open()的第二个参数是target,然后我笑了(e ...

  8. Web设计中打开新页面或页面跳转的方法 js跳转页面

    Web设计中打开新页面或页面跳转的方法 一.asp.net c# 打开新页面或页面跳转 1. 最常用的页面跳转(原窗口被替代):Response.Redirect("newpage.aspx ...

  9. ASP.NET 后台打开新页面

    [TOC] Response.Write 这是最常见的后台打开新页面的方法. Response.Write("<script>window.open('~/FileView.as ...

随机推荐

  1. Selenium操作示例——鼠标悬停显示二级菜单,再点击二级菜单或下拉列表

    这两天在玩python中selenium,遇到一个问题,就是鼠标移动到页面中某按钮或菜单,自动弹出二级菜单或下拉菜单,再自动点击其中的二级菜单或下拉列表. 首先,手工操作:打开母校的主页 http:/ ...

  2. [蓝桥杯]ALGO-122.算法训练_未名湖边的烦恼

    问题描述 每年冬天,北大未名湖上都是滑冰的好地方.北大体育组准备了许多冰鞋,可是人太多了,每天下午收工后,常常一双冰鞋都不剩. 每天早上,租鞋窗口都会排起长龙,假设有还鞋的m个,有需要租鞋的n个.现在 ...

  3. Lucene suggest [转]

      The Big Data Zone is presented by Splunk, the maker of data analysis solutions such as Hunk, an an ...

  4. DB2 Rename SQL0108N错误

    今天DB2 Rename的时候遇到了“SQL0108N ‘表名’名称的限定符数是错误的. 语句如下: db2 rename tableA to tableB 异常: SQL0108N The name ...

  5. SQL Server的分页优化及Row_Number()分页存在的问题

    最近有项目反应,在服务器CPU使用较高的时候,我们的事件查询页面非常的慢,查询几条记录竟然要4分钟甚至更长,而且在翻第二页的时候也是要这么多的时间,这肯定是不能接受的,也是让现场用SQLServerP ...

  6. java设计模式-Iterator

    Iterator模式 主要是用在容器的遍历上,其他的地方都不怎么用:理解一下,会用了就可以了:   1.背景 请动手自己写一个可以动态添加对象的容器: 代码: ArrayList.java(是自己实现 ...

  7. C++进阶小结

    1.C++中类的不同存储区的对象的初始值 class test; class test { private: int i; int j; public: int geti() { return i; ...

  8. react事件中的this指向

    在react中绑定事件处理函数的this指向一共有三种方法,本次主要总结这三种方式. 项目创建 关于项目的创建方法,在之前的文章中有记录,这里不再赘述,项目创建成功后,按照之前的目录结构对生成的项目进 ...

  9. WebStrom配置SVN服务

    本文讲解了如何在公司内网通过代理使用WebStrom等软件连接外部svn服务器,重点在于如何配置网络. 网络设置 1.配置WebStrom软件公司内网网络环境.首先配置代理服务器,点击“file-&g ...

  10. 头皮溢脂性皮炎推荐联合治疗:采乐50ml+希尔生100g(请看详情页)维生素B2维生素B6

    治疗头皮脂溢性皮炎采乐50ml+希尔生100g联合用药用法:用药先用一般香皂清洗头发及头皮,头皮处于湿润状态,取适量希尔生洗剂涂于头皮表面,充分揉搓出泡沫样5 min后用清水冲净抹干,再将适量采乐洗剂 ...