2014-04-28 19 views
1

我有一個單獨的jsp頁面,用於上傳文件。這個jsp頁面必須在我的應用程序的許多不同位置使用,但每次我使用這個jsp頁面時,我的表單行爲類都是不同的。我正在使用struts2和休眠。任何人都可以請建議如何實現這一點。 下面給出的是我的jsp代碼。單個Jsp與不同的動作類

<table width="100%" cellspacing="0" border="0" cellpadding="0"> 

<tr> 
    <td colspan="3" align="left"><s:url 
     action="" id="idfileValidate" escapeAmp="false"></s:url> <input 
     type="button" class="btn"/></td> 
</tr> 

............... 

<tr> 
    <td colspan="3" align="left"><input type="button" class="btn"/><s:url 
     action="" id="idfileUpload" escapeAmp="false"></s:url> <input 
     type="button" class="btn" id="buttonUpload"/> 
    </td> 
</tr> 
</table> 

在兩個<s:url>標籤的作用會有所不同不同的調用位置。

可能有人請幫助我在此

回答

0

你需要在URL給操作名稱如下

<table width="100%" cellspacing="0" border="0" cellpadding="0"> 

<tr> 
    <td colspan="3" align="left"><s:url 
     action="fileValidate" id="idfileValidate" escapeAmp="false"></s:url> <input 
     type="button" class="btn"/></td> 
</tr> 

............... 

<tr> 
    <td colspan="3" align="left"><input type="button" class="btn"/><s:url 
     action="fileUpload" id="idfileUpload" escapeAmp="false"></s:url> <input 
     type="button" class="btn" id="buttonUpload"/> 
    </td> 
</tr> 
</table> 

您需要映射struts.xml的那個動作如下

<action name="fileValidate" 
      class="com.action.struts2.validatefileaction" > 

<action name="fileupload" 
      class="com.action.struts2.fileupload" > 

試試這個

+0

感謝您的解決方案,但是如果我在jsp中提及動作類名稱,我如何在不同動作類的不同位置使用相同的jsp? – Anju

+0

我認爲現在這個jsp被用在兩個類中,如果你點擊第一個按鈕,它會調用文件驗證操作,如果你點擊第二個按鈕,那麼它會調用文件上傳操作,然後這個jsp用於兩個不同的操作。現在你想做什麼? –

0

JSP中唯一的動作名稱是可變的。在每個動作類或基類

String actionName; 
//getter and setter 

創建一個域,然後使用這個在JSP

<tr> 
    <td colspan="3" align="left"><s:url 
     action="%{actionName}" id="id%{actionName}" escapeAmp="false"></s:url> <input 
     type="button" class="btn"/></td> 
</tr> 

,它將動態地爲每個操作返回這個JSP結果替換操作名稱。