Preserving all request parameters through a redirect to an Action

前端 未结 1 794
粉色の甜心
粉色の甜心 2020-12-04 03:57

I need to populate the records with updated message (success / failure)after updating the records in the page. both the actions are from same page. I have added the code as,

1条回答
  •  没有蜡笔的小新
    2020-12-04 04:13

    You can't do it with a redirectAction, where parameters names and values can be dynamic but the number of parameters must be hard-coded, like

    
        hierUpdateMDA
        ${paramValue1}
        ${paramValue2}
        ${paramValue3}
    

    But you can do it with a redirect result (that is generally used to redirect to non-action URLs).

    Basically, you need to specify only the namespace and the action name (and they could be dynamic too, TBH) in Struts configuration, and a dynamic parameter representing the QueryString.

    Then in the first Action (or in a BaseAction), you need a method to get the Parameter Map, loop through each parameter (and each one of its values), URLEncode them and return the mounted QueryString. That's it.

    This will work with form data (POST), query parameters (generally GET) or both (POST with form data and QueryString), and it's URL safe.

    Struts config

    
        
                            
                /cool/target.action${queryParameters}
            
        
        
            /cool/requestGrabber.jsp
        
    
    

    org.foo.bar.cool.RequestGrabberAction.java (Action classes)

    package org.foo.bar.cool;
    
    import javax.servlet.http.HttpServletRequest;
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import java.util.Enumeration;
    import org.apache.struts2.interceptor.ServletRequestAware;
    import com.opensymphony.xwork2.ActionSupport;
    
    @SuppressWarnings("serial")
    public class RequestGrabberAction extends ActionSupport 
                                   implements ServletRequestAware {
    
        private HttpServletRequest request; 
        public void setServletRequest(HttpServletRequest request){ 
            this.request = request;
        }
    
        public String source(){
            System.out.println("Source Action executed");
            return SUCCESS;
        }
    
        public String target(){     
            System.out.println("Target Action executed");
            return SUCCESS;
        }
    
    
        public String getQueryParameters() throws UnsupportedEncodingException {
            String queryString = "";
    
            // Get parameters, both POST and GET data
            Enumeration parameterNames = request.getParameterNames();
    
            // Loop through names
            while (parameterNames.hasMoreElements()) {            
                String paramName = parameterNames.nextElement();
                // Loop through each value for a single parameter name
                for (String paramValue : request.getParameterValues(paramName)) {
                    // Add the URLEncoded pair
                    queryString += URLEncoder.encode(paramName, "UTF-8") + "="
                                 + URLEncoder.encode(paramValue,"UTF-8") + "&";
                } 
            }
    
            // If not empty, prepend "?" and remove last "&"
            if (queryString.length()>0){  
                queryString = "?" 
                            + (queryString.substring(0,queryString.length()-1));
            }
    
            return queryString;
        }
    
    }
    

    /cool/requestGrabber.jsp

    <%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    
    
    
        
            Request Grabber
        
        
            QueryString = 
            
                
                
                
                   
        
    
    

    Enjoy

    0 讨论(0)
提交回复
热议问题