Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

java - Preserving all request parameters through a redirect to an Action

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, After completing Update action added the result type as Chain and it shows success message. But it is not disappearing when we click on Search immediately(first time) after update action completes. Help me to clear the message while clicking on search action.

Due to above issue i used redirect option in result type. But i could get the request parameters in redirected action. is there any way to get all request parametrs in redirected action other than hardcoding it?

enter image description here

<interceptor-stack name="storeStack">
    <interceptor-ref name="defaultStack" />
    <interceptor-ref name="store">
        <param name="operationMode">STORE</param>
    </interceptor-ref>
</interceptor-stack>

<interceptor-stack name="retrieveStack">
    <interceptor-ref name="defaultStack" />
    <interceptor-ref name="store">
        <param name="operationMode">RETRIEVE</param>
    </interceptor-ref>
</interceptor-stack>

<action name="hierarchySaveMap" method="updateHierarchyMap"
    class="com.cotyww.bru.web.action.master.HierarchyUpdateAction">
    <interceptor-ref name="storeStack" />
    <result name="success" type="redirectAction">
        <param name="actionName">hierUpdateMDA</param>
        <param name="parse">true</param>
    </result>
    <result name="input" type="tiles">hierarchyUpdate{1}</result>
    <result name="error" type="tiles">hierarchyUpdate{1}</result>
</action>

Is there a way to send the parameters to next action dynamically without hardcoding in struts.xml?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

<result name="success" type="redirectAction">
    <param name="actionName">hierUpdateMDA</param>
    <param name="${paramName1}">${paramValue1}</param>
    <param name="${paramName2}">${paramValue2}</param>
    <param name="${paramName3}">${paramValue3}</param>

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

<package name="requestGrabber" namespace="cool" extends="struts-default">
    <action name="source" class="org.foo.bar.cool.RequestGrabberAction" 
          method="source">
        <result type="redirect">                
            <param name="location">/cool/target.action${queryParameters}</param>
        </result>
    </action>
    <action name="target" class="org.foo.bar.cool.RequestGrabberAction" 
          method="target">
        <result name="success">/cool/requestGrabber.jsp</result>
    </action>
</package>

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<String> 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"%>

<!doctype html>
<html>
    <head>
        <title>Request Grabber</title>
    </head>
    <body>
        QueryString = <s:property value="queryParameters" />
        <s:form action="source" namespace="/cool">
            <s:textfield name="foo" value="%{#parameters.foo}" />
            <s:textfield name="bar" value="%{#parameters.bar}" />
            <s:submit />
        </s:form>       
    </body>
</html>

Enjoy


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...