struts2

Single JSP with different Action Class


I have a single JSP page, which is used to upload files. This page has to be used in many different location in my application, but each time I use this page, my action class for the form is different.

I am using Struts 2 and Hibernate. Could anyone please suggest on, how to implement this.

Given below is my JSP code:

  <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>

The action in both the <s:url> tag will be different calling locations.


Solution

  • You need to give action name in url as following

     <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>
    

    You need to mapped that action in struts.xml as following

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

    Try this