skip to Main Content

Hello community I have the following problem, I am working on a project with JSP and Struts technology, I am really new because it is an old technology but it is what I had to work with.

I have the problem that calls are being made to the action when I refresh my page, I understand this is because in my URL is the last action that I send to call, so it is making a call back to that action, I have more than one form within my JSP which have different actions, I do not know if this is correct or have to change them to ajax or is there any way to solve it?

I have already added a POST method for the actions but it still has not worked.

My actions in configuration file:

<action name="plantillasAction_*" method="{1}" class="com.xxx.action.ActionPlantillas">           
        <result name="success">/jsp/plantillas/ResultTemplates.jsp</result>
        <result name="templateUpload">/jsp/plantillas/CreateCoordinates.jsp</result>
        <result name="saveCoordinates">/jsp/plantillas/SaveCoordinates.jsp</result>
        <result name="updateCoordinates">/jsp/plantillas/UpdateCoordinatesTemplate.jsp</result> 
        <interceptor-ref name="defaultSecurityStack">
            <param name="tokenSession.excludeMethods">init,templateFileUpload,processNavigator, 
                addCoordinateTemplate,deleteCoordinateToSave,saveTemplate,
                deleteTemplate,viewUpdateTemplate,addCoordinatesTemplateUpdate,
                searchTemplates,processUpdateNavigator</param>
        </interceptor-ref>
        <allowed-methods>
            templateFileUpload,
            processNavigator,
            addCoordinateTemplate,
            deleteCoordinateToSave,
            saveTemplate,
            deleteTemplate,
            viewUpdateTemplate,
            addCoordinatesTemplateUpdate,
            processUpdateNavigator,
            init, 
            searchTemplates
            
            
        </allowed-methods>
    </action>

My CreateCoordinates.jsp:

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-tags.tld" prefix="s" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html>
<html>
   <head>
      <meta name="decorator" content="contentLayout"/>
   </head>
   <body>
      <script src="${pageContext.request.contextPath}/html/js/jquery/alerts/jquery.alerts.js"  type="text/javascript"></script>
      <link rel="stylesheet" href="${pageContext.request.contextPath}/html/js/jquery/alerts/jquery.alerts.css">
      <script src="${pageContext.request.contextPath}/html/js/coordinatesTemplate.js" type="text/javascript"></script>
      <script src="${pageContext.request.contextPath}/html/js/templates.js"  type="text/javascript"></script>
      <script language="javascript">
         function update(value){
         var url="myAction?someVariable=value";
         window.open(url,"_blank","directories=no, status=no,width=1400, height=870,top=0,left=0");
         }
                    
         
         
         
      </script>
      <div id="content">
         <div class="wrap">
            <h1 style="margin-left:15px">
               <s:text name="label.module.template.header.update"/>
            </h1>
            <s:form action="plantillasAction_processUpdateNavigator" method="post" >
               <s:token />
               <hr style="border:15px;">
               <hr style="border:2px;">
               <div id="navigator" align="center">
                  <table align="center">
                     <tr>
                        <s:if test="currentPosition==imagePDFs.size() ">
                           <script type="text/javascript"> disableFirstButtons()</script>
                        </s:if>
                        <s:if test="currentPosition== 1 && imagePDFs.size()  != 1">
                           <script type="text/javascript"> enableFirstButtons()</script>
                        </s:if>
                        <s:if test="currentPosition==1 && imagePDFs.size()==1 ">
                           <script type="text/javascript"> enableAllButtons()</script>
                        </s:if>
                        <td>
                           <button id="firstPage" name="buttonAction" type="submit" value="3" ><<</button>
                        </td>
                        <td>
                           <button id="backPage" name="buttonAction" class="previous" type="submit" value="2" ><</button>
                        </td>
                        <td align="center">
                           <li>${currentPosition}/${imagePDFs.size()}</li>
                        </td>
                        <td align="right">
                           <button id="nextPage" name="buttonAction" class="next" type="submit" value="1">></button>
                        </td>
                        <td align="right">
                           <button id="endPage" name="buttonAction" type="submit" value="4">>></button>
                        </td>
                     </tr>
                  </table>
               </div>
               <div class="wrapper">
                  <div id="content">
                     <div id="pdf-container" >
                        <div id="signers-list">
                        </div>
                        <img id="pdf-page" src="data:image/png;base64,${image}" />
                     </div>
                     <!-- .pdf-container -->
                  </div>
                  <!-- .content -->
               </div>
               <!-- .wrapper -->
               <div id="navigator" align="center">
                  <table align="center">
                     <tr>
                        <td align="center">
                           <button id="firstPage2" name="buttonAction" type="submit" value="3"><<</button>
                        </td>
                        <td align="center">
                           <button id="backPage2" name="buttonAction" class="previous" type="submit" value="2"><</button>
                        </td>
                        <td align="center">
                           <li>${currentPosition}/${imagePDFs.size()}</li>
                        </td>
                        <td align="rigth">
                           <button id="nextPage2" name="buttonAction" class="next" type="submit" value="1">></button>
                        </td>
                        <td align="rigth">
                           <button id="endPage2" name="buttonAction" type="submit" value="4">>></button>
                        </td>
                     </tr>
                  </table>
               </div>
            </s:form>
            <s:form action="routeAction_updateRoute" method="post" validate="false" namespace="/plantilla" theme="simple" onsubmit="return false;">
               <s:token />
               <table width="100%" cellspacing="0" cellpadding="0">
                  <tr height="10px">
                  </tr>
                  <tr height="40px" valign="middle" class="tablecells">
                     <td width="25%" class="negritas">
                        <s:text name ="label.module.template.info.plantilla" />
                     </td>
                  </tr>
                  <tr height="40px" valign="middle" class="tablecells">
                     <td width="14%">
                        <s:text name="label.module.template.add.nombre.plantilla"/>
                     </td>
                     <td>
                        <s:textfield id="idTemplateName" maxlength="80" size="40" 
                           value="%{currentTemplateView}" cssClass="inputbox"/>
                     </td>
                  </tr>
                  <tr height="5px">
                  </tr>
               </table>
               </table>
               <!-- TABLA CON BOTONES PARA BUSCAR INTEGRANTES A LA NUEVA RUTA (GRID)-->
               <table width="100%" cellspacing="0" cellpadding="0">
                  <tr height="40px" valign="middle" align="center" class="tablecells">
                     <td colspan="2" class="leftElement">
                        <button id="agregarFirma" class="action green" style="float:none" name="buttonActionCoordiante" type="submit" value="2">
                        <span id="fisnish" class="label" onClick='createDragDropElement()'>Agregar</span>
                        </button>
                        <button class="action green" style="float:none" id="btnCancelar" onClick="regresar('${pageContext.request.contextPath}')">
                           <span id="iniciar" class="label">
                              <s:text name="boton.cancelar"/>
                           </span>
                        </button>
                     </td>
                  </tr>
                  <tr height="15px">
                  </tr>
               </table>
            </s:form>
            
      </div>
      <script src="${pageContext.request.contextPath}/html/plugins/fontawesome/js/fontawesome-all.min.js" type="text/javascript"></script>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js'></script>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.943/pdf.min.js'></script>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/interact.js/1.10.11/interact.min.js'></script>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.943/pdf.worker.min.js'></script>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/TweenMax.min.js'></script>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/utils/Draggable.min.js'></script>
      <script src="${pageContext.request.contextPath}/html/js/mainPdfUpdate.js" type="text/javascript"></script>
   </body>
</html>

When I enter my JSP this is the URL:

enter image description here

When I press any button of my first form it changes the URL to

enter image description here

If I refresh my page there, the action name is plantillasAction_processUpdateNavigator ,what I want in this case is to execute the action plantillasAction_viewUpdateTemplate.

My ActionPlantillas.java:

public class ActionPlantillas extends ActionSupport implements ServletRequestAware {

    private static Logger logger = LogManager.getLogger(ActionPlantillas.class);

    private HttpServletRequest request;
    
    public String viewUpdateTemplate() {

           //OPERATIONS
            return "updateCoordinates";
       
    }

    public String processUpdateNavigator() {
        //OPERATIONS
       return "updateCoordinates";
    }  




}

2

Answers


  1. Create a SuccessAction with redirectAction class.

    <action name="plantillasAction_*" method="{1}" class="com.xxx.action.ActionPlantillas"> 
    <result name="success">/jsp/plantillas/ResultTemplates.jsp</result>
    </action>
    <action name="SuccessAction" class="SuccessPage">
    <result name="success">/jsp/plantillas/success.jsp</result>
    <action name="manual_order" class="materialTypeMasterAction" method="enquiry_process">
    <result type="redirectAction">SuccessAction</result>
    </action>
    

    Like this:
    Prevent action resubmission

    Please notice that if you do not change the URL, it will always resubmit the form. So if you implement this solution above, the url will change on success (redirectSuccess).

    You don’t have to really create a new success page, you can redirect to the original url/action in which the form is loaded (clean).

    I hope this helps.

    Login or Signup to reply.
  2. If you use Struts application the concept of an action is more wide than you use it in the JSP forms. Everything that is coming to Struts with a request is an action. So, refreshing a page means calling the action which is mapped by the URL.

    If you follow a pattern of action first, then JSP is forwarded by the servlet dispatcher when Struts executes a dispatcher result. It is a default result type used by a result configuration. The action after it executes usually returns some result with the name. Actually it is a result code, that allows the action invocation to stop processing an action and execute a result immediately.

    As I said, after result is executed the JSP is forwarded to the browser and URL is not the location of that JSP, but the action which returned that JSP as a result. If request is forwarded then URL doesn’t change. If you want to change URL you should use a new request with updated URL.

    In your scenario you render a JSP with the viewUpdateTemplate() action. And you want to show the same JSP after you execute processUpdateNavigator() action. Because the same result updateCoordinates is returned.

    You should be aware that different actions are executed by deferent requests, different request methods, by different threads, and use new action instances per request. It means that everything that was rendered via OGNL or JSTL in by the first action might not be available to the second action if they don’t share the same prepare() method by implementing the same Prepareable interface. So, JSP might not be rendered completely.

    On the other hand you don’t want to stay on the URL of the action that was executing a POST request. In this case you should return a redirect response. It’s done by Struts redirect or reditectAction result type. Both of them changes the location in browser URL, and following a new GET request.

    In you case you should configure this result type to the action and specify which action it is redirected. It should be the first action, because it returns the above JSP. The second action won’t return JSP, instead it return reditectAction result to the first action.

    Note, any attribute that you used in the JSP via the second action should be scoped to session or shared via prepare() because redireced response loses its attributes.

    The code:

    In struts.xml:

    <action name="plantillasAction_*" method="{1}" class="com.xxx.action.ActionPlantillas">           
            <result name="success">/jsp/plantillas/ResultTemplates.jsp</result>
            <result name="templateUpload">/jsp/plantillas/CreateCoordinates.jsp</result>
            <result name="saveCoordinates">/jsp/plantillas/SaveCoordinates.jsp</result>
            <result name="updateCoordinates">/jsp/plantillas/UpdateCoordinatesTemplate.jsp</result> 
           <result name="viewUpdateTemplate" type="redirectAction">plantillasAction_viewUpdateTemplate</result>
            <interceptor-ref name="defaultSecurityStack">
                <param name="tokenSession.excludeMethods">init,templateFileUpload,processNavigator, 
                    addCoordinateTemplate,deleteCoordinateToSave,saveTemplate,
                    deleteTemplate,viewUpdateTemplate,addCoordinatesTemplateUpdate,
                    searchTemplates,processUpdateNavigator</param>
            </interceptor-ref>
            <allowed-methods>
                templateFileUpload,
                processNavigator,
                addCoordinateTemplate,
                deleteCoordinateToSave,
                saveTemplate,
                deleteTemplate,
                viewUpdateTemplate,
                addCoordinatesTemplateUpdate,
                processUpdateNavigator,
                init, 
                searchTemplates
                
                
            </allowed-methods>
    </action>
    

    In Action class:

        public String viewUpdateTemplate() {
    
               //OPERATIONS
                return "updateCoordinates";
           
        }
    
        public String processUpdateNavigator() {
            //OPERATIONS
           return "viewUpdateTemplate";
        }  
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search