2011-08-23 39 views
0

一點背後的故事:如何在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? 

對象可以或可以不被修改,但必須可以從所有三個包含中訪問,並且對象的最新更改必須可訪問。

謝謝你的時間。

+1

我不明白這個問題,如果你通過'request.getAttribute()'和'request.setAttribute()'來訪問它,對象不會改變。所有修改將在請求期間可見。我錯過了什麼嗎? – home

+1

你是如何使用jsp:params標籤傳遞對象的!恐怕似乎沒有直接解決您的問題..你將不得不使用request.getAttribute和request.setAttribute,並確保它們一貫處理。如果有人有一個更好的解決方案,請讓我們知道..我也想知道太 – Ash

+0

我會盡力澄清我要麼尋找解決方案a)主文件 - >對象實例化 - >對象傳遞給jsp:include - >對象修改 - >對象傳遞迴主文件 - >對象傳遞給另一個jsp:包含 - >對象修改 - >對象傳回主文件....或b)主文件 - >對象實例化 - >在jsp中引用對象:包含 - >對象修改 - >對象引用另一個jsp:include - >新的修改值是可訪問的,因爲它是通過引用訪問的。 – AlexScript

回答

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和收回我的父母jsp頁面。

感謝您的幫助,對不起,我沒有及時回覆。

相關問題