如何在 jsp:include 文件中通过引用(而不是通过值)访问对象?
一些背景故事:
我正在处理一个非常大的文件,最终导致以下错误:
The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit.
为了解决这个问题,我一直使用 jsp:include< 来“模块化”该文件/代码> 标签。通过序列化对象并使用
jsp:param
标记,然后在 jsp:include 文件中反序列化,我已成功地将对象从主文件传递到包含的文件。然而,就好像这些对象在主文件和多个包含的文件中被使用、修改、重用和重新修改一样。我想知道 是否有一种方法可以在渲染包含文件后将对象传回主文件,或者是否有一种方法可以通过引用访问这些对象,以便可以在一个包含文件中修改它们,并且修改后的对象的正确实例可以可以重用下面的其他包含文件吗?
到目前为止,我已经考虑了 pagecontext.setAttribute()
(这似乎不是通过引用,并且似乎不允许我在修改到主文件后将值传回)和 jsp :param
(与 pagecontext.setAttribute()
几乎相同)。
这是我到目前为止所拥有的:
下面的代码示例:(我希望这不会让任何人感到困惑。我不是在寻找语法更正,我只是在寻找一个允许通过引用访问同一对象的解决方案 可以在修改后访问它。
(例如全局变量以及 include 标记)或者让我将对象传递
//Serialized Objects, Google Gson object
Gson gson = new Gson();
String serializedObject = gson.toJson( objectName );
<jsp:include page="/includes/includedFileName1.jsp">
<jsp:param name="serializedObject" value="<%= serializedObject %>" />
</jsp:include>
<!-- Is there a way to ensure includedFileName2 .jsp gets the modified version of object from includedFileName1.jsp? -->
<jsp:include page="/includes/includedFileName2.jsp">
<jsp:param name="serializedObject" value="<%= serializedObject %>" />
</jsp:include>
<!-- Is there a way to ensure includedFileName3 .jsp gets the modified version of object from includedFileName2.jsp? -->
<jsp:include page="/includes/includedFileName3.jsp">
<jsp:param name="serializedObject" value="<%= serializedObject %>" />
</jsp:include>
回 main.jsp,以便下一个jsp
//Gson object used to convert serialized strings to java objects
Gson gson = new Gson();
ObjectType object = null;
if (null != request.getParameter("serializedObject")) {
object = gson.fromJson(request.getParameter("serializedObject"), ObjectType.class);
}
if (x = y) {object = somethingNew1;}
//is there a way to pass object back to the main.jsp?
: include
//Gson object used to convert serialized strings to java objects
Gson gson = new Gson();
ObjectType object = null;
if (null != request.getParameter("serializedObject")) {
object = gson.fromJson(request.getParameter("serializedObject"), ObjectType.class);
}
if (x = y) {object = somethingNew2;}
//is there a way to pass object back to the main.jsp?
includeFileName3.jsp
//Gson object used to convert serialized strings to java objects
Gson gson = new Gson();
ObjectType object = null;
if (null != request.getParameter("serializedObject")) {
object = gson.fromJson(request.getParameter("serializedObject"), ObjectType.class);
}
if (x = y) {object = somethingNew3;}
//is there a way to pass object back to the main.jsp?
该对象可能会也可能不会被修改,但必须可以从所有三个包含中访问,并且必须可以访问该对象的最新更改。
感谢您的宝贵时间。
a little back story:
I am working on a file that got to be very large, eventually resulting in the following error:
The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit.
In order to get around this, I have been "modularising" the file by utilizing the jsp:include
tags. I have successfully passed objects from the main file to the included file by serializing the objects and using the jsp:param
tag and then deserializing in the jsp:include file. However, being as though these objects are used, modified and reused and re-modified in the main file and in multiple included files. I am wondering
if there is a way to pass back the object to the main file after rendering the included file OR if there is a way to access these objects by reference so that they can be modified within one included file and the right instance of the modified object could be reused other included files below it?
So far I have considered pagecontext.setAttribute()
(which does not seem to be by ref and does not seem to let me pass the value back after being modified to the main file) and jsp:param
(pretty much the same thing as as the pagecontext.setAttribute()
).
This is what I have so far:
Code Sample below: (I hope this doesn't confuse anyone. I'm not looking for syntax corrections, I'm just looking for a solution that will allow the same object to be accessed by reference (such as a global variable along with the include tags) or for me to pass the object back to the main.jsp so that the next jsp:include can access it after it's been modified.
Main.jsp
//Serialized Objects, Google Gson object
Gson gson = new Gson();
String serializedObject = gson.toJson( objectName );
<jsp:include page="/includes/includedFileName1.jsp">
<jsp:param name="serializedObject" value="<%= serializedObject %>" />
</jsp:include>
<!-- Is there a way to ensure includedFileName2 .jsp gets the modified version of object from includedFileName1.jsp? -->
<jsp:include page="/includes/includedFileName2.jsp">
<jsp:param name="serializedObject" value="<%= serializedObject %>" />
</jsp:include>
<!-- Is there a way to ensure includedFileName3 .jsp gets the modified version of object from includedFileName2.jsp? -->
<jsp:include page="/includes/includedFileName3.jsp">
<jsp:param name="serializedObject" value="<%= serializedObject %>" />
</jsp:include>
includedFileName1.jsp
//Gson object used to convert serialized strings to java objects
Gson gson = new Gson();
ObjectType object = null;
if (null != request.getParameter("serializedObject")) {
object = gson.fromJson(request.getParameter("serializedObject"), ObjectType.class);
}
if (x = y) {object = somethingNew1;}
//is there a way to pass object back to the main.jsp?
includedFileName2.jsp
//Gson object used to convert serialized strings to java objects
Gson gson = new Gson();
ObjectType object = null;
if (null != request.getParameter("serializedObject")) {
object = gson.fromJson(request.getParameter("serializedObject"), ObjectType.class);
}
if (x = y) {object = somethingNew2;}
//is there a way to pass object back to the main.jsp?
includedFileName3.jsp
//Gson object used to convert serialized strings to java objects
Gson gson = new Gson();
ObjectType object = null;
if (null != request.getParameter("serializedObject")) {
object = gson.fromJson(request.getParameter("serializedObject"), ObjectType.class);
}
if (x = y) {object = somethingNew3;}
//is there a way to pass object back to the main.jsp?
The object may or may not be modified but has to be accessible from all three includes and the objects latest changes has to be accesible.
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢@home,@Ashley ...
我重新思考了它的实现方式,并使用 request.setAttribute() 和 request.getAttribute() 解决了我的问题。
我使用 request.setAttribute() 传递对象并在 jsp:include 中接收它。如果它在 jsp:include 中被修改(在大多数情况下不是这样的,但我确实有 1 到 2 种情况可能会修改该值),我会使用 request.setAttribute(0 and在我的父 jsp 页面中收到它,
感谢您的帮助,很抱歉我没有尽快回复。
Thanks @home, @Ashley ...
I re-thought the way this was implemented and resolved my issue using request.setAttribute() and request.getAttribute().
I pass through the object using request.setAttribute() and receive it in my jsp:include. If it was modified within the jsp:include (which it isn;t in most cases, but I do have 1 or two cases which may modify the value), I return the object from the jsp:iclude using request.setAttribute(0 and recive it back in my parent jsp page.
Thanks for all the help, I'm sorry I didn't reply back sooner.