一个小故事:
我正在处理一个非常大的文件,最终导致以下错误:
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标签)或者我将对象传递回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?
可以修改或不修改对象,但必须可以从所有三个包含中访问该对象,并且必须可以访问对象的最新更改。
感谢您的时间。
答案 0 :(得分:0)
谢谢@home,@ Ashley ......
我重新考虑了这个实现的方式并使用request.setAttribute()和request.getAttribute()解决了我的问题。
<%
//set new parameters to be passed through to the jsp:include
request.setAttribute("objectName", objectInstance);
%>
<jsp:include page="/includes/requisitionClinicalConditionContent.jsp"/>
<%
//get parameters passed from the jsp:include
objectInstance = (object) request.getAttribute("objectName");
%>
我使用request.setAttribute()传递对象,并在我的jsp:include中接收它。如果它在jsp:include中被修改(在大多数情况下它不是,但我确实有一两个可以修改值的情况),我使用request.setAttribute(0和0)从jsp:iclude返回对象把它放回我父母的jsp页面。
感谢所有的帮助,对不起,我没有尽快回复。