簡體   English   中英

通過重定向到動作保留所有請求參數

[英]Preserving all request parameters through a redirect to an Action

更新頁面中的記錄后,我需要用更新的消息(成功/失敗)填充記錄。 這兩個操作都來自同一頁面。 我已將代碼添加為,完成更新操作后,將結果類型添加為Chain,它顯示成功消息。 但是,更新操作完成后,當我們立即(第一次)單擊“搜索”時,它並沒有消失。 單擊搜索操作時,幫助我清除消息。

由於上述問題,我在結果類型中使用了重定向選項。 但是我可以在重定向操作中獲取請求參數。 除了硬編碼之外,還有什么方法可以使所有請求參數都處於重定向操作中?

在此處輸入圖片說明

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

有沒有辦法在不對struts.xml進行硬編碼的情況下將參數動態發送到下一個動作?

您無法使用redirectAction做到這一點,其中的參數名稱和值可以是動態的,但是參數的數量必須是硬編碼的,例如

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

但是您可以使用redirect結果 (通常用於重定向到非操作網址)來實現。

基本上,您只需要在Struts配置中指定名稱空間和操作名稱(它們也可以是動態的,TBH),以及一個表示QueryString的動態參數。

然后,在第一個Action(或BaseAction)中,您需要一個方法來獲取Parameter Map,循環遍歷每個參數(及其每個值),對它們進行URLEncode並返回已安裝的QueryString。 而已。

這將與表單數據(POST),查詢參數(通常為GET)或同時與兩者(帶有表單數據 QueryString的POST)一起使用,並且它是URL安全的。

Struts配置

<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 (動作類)

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>

請享用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM