简体   繁体   English

某个页面的onclick事件,执行其他页面的POST动作

[英]Onclick event of a certain page, perform POST action of other page

Let me explain my problem with a scenario: 让我用一个场景解释我的问题:
I have webpages "A.jsp" and "B.jsp". 我有网页“ A.jsp”和“ B.jsp”。 "B.jsp" is a page with a form that requires input field, 'username' and on submission displays a certain page "C.jsp" with user-specific information. “ B.jsp”是具有表单的页面,该表单需要输入字段“用户名”,并且在提交时显示带有特定于用户的信息的特定页面“ C.jsp”。 "A.jsp" has table with an element that would correspond to 'username' on "B.jsp". “ A.jsp”的表中的元素与“ B.jsp”上的“用户名”相对应。 Onclick of a certain 'username' on "A.jsp", I want to be redirected to "C.jsp" with clicked username specific information. 在“ A.jsp”上某个“用户名”的onclick上,我想使用单击的用户名特定信息重定向到“ C.jsp”。

How do I get it done? 我该如何完成?

I am using MVC framework. 我正在使用MVC框架。 Only A.jsp and B.jsp are tied to controller, C.jsp is only a View. 只有A.jsp和B.jsp绑定到控制器,C.jsp只是一个视图。 But, I believe that is less of an issue, since this seem to be more of a javascript question. 但是,我认为这不是问题,因为这似乎是一个JavaScript问题。

Using prototype javascript framework, I have tried the following: 使用原型javascript框架,我尝试了以下操作:
redirectToC=function(pageurl){ redirectToC = function(pageurl){
var username=document.getElementById("user1").value; var username = document.getElementById(“ user1”)。value;
url=pageurl; url = pageurl; //I am using B.jsp as Pageurl //我使用B.jsp作为Pageurl
$j.ajax({ type: 'POST', $ j.ajax({type:'POST',
url: url, data: "username="+username, url:url,数据:“ username =” + username,
success: function(request){ 成功:功能(要求){
window.location=request; window.location = request;
} }
}); });
} }

Does C.jsp support only POST - or does it support GET requests as well? C.jsp是否仅支持POST-还是也支持GET请求?

  1. If it does, in the onClick of the 'username' link, set the location of the browser (i think document.location ) to the URL of C.jsp with the username as a GET parameter. 如果是这样,请在“用户名”链接的onClick中,将浏览器的位置(我认为document.location )设置为C.jsp的URL,并将用户名用作GET参数。 Since you are generating the A.jsp page dynamically, this URL can be embedded into the page at generation time 由于您是动态生成A.jsp页面,因此可以在生成时将此URL嵌入到页面中
  2. If it does not, a bad hack i can think of is to implement the the table element with the username, as a hidden form element which is submitted via javascript when clicked - 'form'.submit() 如果没有,我可以想到的一个坏办法是用用户名实现table元素,将其作为隐藏的form元素,单击后通过javascript提交'form'.submit()
<form id="showUserForm" action="/controller/showUser" method="POST">
    <input type="hidden" id="username" name="username">
</form>

.......... ..........

<a onclick="showUser('${users.userName}')" href="#"></a>

.......... ..........

<script type="text/javascript">
    function showUser(userName){
        document.getElementById("username").value = userName;
        document.getElementById("showUserForm").submit();
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM