一点背后的故事:如何在jsp:include文件中通过引用(而不是按值)访问对象?
我的工作,进行了非常大的文件,最终导致出现以下错误:
The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit.
为了解决这个问题,我已经“modularising “该文件通过利用jsp:include
标签。我通过序列化对象并使用jsp:param
标记,然后在jsp:include文件中反序列化,成功地将主文件中的对象传递到了包含的文件。但是,仿佛使用这些对象,在主文件和多个包含文件中修改并重新使用和重新修改这些对象。我想知道 如果有一种方法可以在渲染包含文件之后将对象传递回主文件,或者如果有一种方法可以通过引用访问这些对象,以便可以在一个包含文件中修改它们,修改后的对象可以在其下面的其他包含文件中重用?
到目前为止,我已经考虑pagecontext.setAttribute()
(这似乎并没有被裁判似乎并没有让我传递值回进行修改,以在主文件之后)和jsp:param
(几乎同样的事情,作为pagecontext.setAttribute()
)。
这是我到目前为止有:
下面代码示例:(我希望这不会混淆任何人,我不是找语法修正,我只是寻找一个解决方案,将允许。通过引用访问的相同对象(例如全局变量和include标签)或者将对象传递回main.jsp,以便下一个jsp:include在修改后可以访问它。
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?
对象可以或可以不被修改,但必须可以从所有三个包含中访问,并且对象的最新更改必须可访问。
谢谢你的时间。
我不明白这个问题,如果你通过'request.getAttribute()'和'request.setAttribute()'来访问它,对象不会改变。所有修改将在请求期间可见。我错过了什么吗? – home
你是如何使用jsp:params标签传递对象的!恐怕似乎没有直接解决您的问题..你将不得不使用request.getAttribute和request.setAttribute,并确保它们一贯处理。如果有人有一个更好的解决方案,请让我们知道..我也想知道太 – Ash
我会尽力澄清我要么寻找解决方案a)主文件 - >对象实例化 - >对象传递给jsp:include - >对象修改 - >对象传递回主文件 - >对象传递给另一个jsp:包含 - >对象修改 - >对象传回主文件....或b)主文件 - >对象实例化 - >在jsp中引用对象:包含 - >对象修改 - >对象引用另一个jsp:include - >新的修改值是可访问的,因为它是通过引用访问的。 – AlexScript