[英]Pass data from one jsp to another and display that jsp view
Hey i am a php developer and this is my first go with jsp. 嘿,我是一名php开发人员,这是我第一次使用jsp。 Now i retrieved a Json string from my class and converted it into GSON.
现在我从我的类中检索了一个Json字符串并将其转换为GSON。 I display a field in my
result.jsp
for eg:- ID and on clicking the id it should go to details.jsp
and show more info about that ID 我在
result.jsp
显示一个字段,例如: - ID,点击id,它应该转到details.jsp
并显示有关该ID的更多信息
Currently my result.jsp is as follows:- 目前我的result.jsp如下: -
<html>
<body>
<div class="list-group">
<%
String json = (String)request.getAttribute("jsonstring");
Gson gson = new Gson();
ConCom diff = new ConCom();
diff = gson.fromJson(json, ConCom.class);
List<ComparisonResultDTOarr> ls = diff.getComparisonResultDTOarr();
for(int i = 0;i<ls.size();i++)
{
List<AuditItemLogsDTOArr> lsinner = ls.get(i).getAuditItemLogsDTOArr();
%><a href="#" class="list-group-item">
<%out.println(lsinner.get(0).getKeyAsString());%></a><%
}
%>
</div>
</body>
</html>
I read around SO and googled it and understood that that i could make a hidden form. 我在周围阅读并用谷歌搜索并理解我可以制作一个隐藏的形式。 Now I create a form with the following two fields and using the anchor tag i submit the form.
现在我创建一个包含以下两个字段的表单,并使用锚标记提交表单。 But the values in the form need to be posted according to the ID clicked, how can i make that dynamic?
但是表单中的值需要根据点击的ID发布,我该如何制作动态?
So if my form is as follows:- 如果我的表格如下: -
<form action="details.jsp" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="phone">
</form>
And my details.jsp will be like:- 而我的details.jsp将如下: -
<%= request.getParameter("firstname") %>
<%= request.getParameter("phone") %>
I want the firstname and phone to be set according to the ID clicked and the form to be submitted. 我希望根据点击的ID和要提交的表单设置名字和电话。 I can obtain the String/Integer value from my diff object in this page.
我可以从此页面中的diff对象获取String / Integer值。 Would i need to use JQuery?
我需要使用JQuery吗? Any help?
有帮助吗?
Also i know i should be using JSTL. 我也知道我应该使用JSTL。 And i will get to that soon.
我很快就会谈到这一点。 Thank you.
谢谢。
You could call a javascript function during the onclick event of your anchor tag: 您可以在锚标记的onclick事件期间调用javascript函数:
<a href="#" onclick="submitHiddenForm("<%=lsinner.getFirstName()%>", "<%=lsinner.getPhone()%>");">...
Your JS function would like like: 你的JS函数喜欢:
function submitHiddenForm(firstName, phone) {
document.getElementById("firstname").value = firstName;
document.getElementById("phone").value = phone;
// attach a name attribute to your form tag
// submit the form
document.myForm.submit();
}
I hope this helps. 我希望这有帮助。
EDIT: changed diff to lsinner, since that's the var used in the loop. 编辑:将差异更改为lsinner,因为这是循环中使用的var。
On click of the ID call a javascript function passing the values inside the function you can dynamically set the values of the form by getting each element like document.getElementById("firstname").value=value passed
similarly set the other fields and in the end document.myform.submit();
点击ID调用javascript函数传递函数内部的值,你可以通过获取像
document.getElementById("firstname").value=value passed
这样的每个元素来动态设置表单的值document.getElementById("firstname").value=value passed
类似地设置其他字段并在end document.myform.submit();
Note since we are fetching HTML elements by Id you can use 请注意,因为我们可以使用Id来获取HTML元素
<form action="details.jsp" method="post" name="myform">
First name: <input type="hidden" name="firstname" id="firstname"><br>
Last name: <input type="hidden" name="phone" id="phone">
</form>
type="hidden"
will hide the elements. type="hidden"
将隐藏元素。 check these for better understanding. 检查这些以便更好地理解。
If you have the diff object in details.jsp as well, it should be enough to pass only the ID as a parameter, in a normal link. 如果你在details.jsp中也有diff对象,那么在普通链接中只应该将ID作为参数传递就足够了。
<a href="details.jsp?id=<%= id %>">details</a>
You'd probably do the same in PHP. 您可能在PHP中也这样做。
JQuery is client side JavaScript and not required to solve your problem. JQuery是客户端JavaScript,不需要解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.