简体   繁体   English

通过重定向到动作保留所有请求参数

[英]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. 我已将代码添加为,完成更新操作后,将结果类型添加为Chain,它显示成功消息。 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? 除了硬编码之外,还有什么方法可以使所有请求参数都处于重定向操作中?

在此处输入图片说明

<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? 有没有办法在不对struts.xml进行硬编码的情况下将参数动态发送到下一个动作?

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 您无法使用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>

But you can do it with a redirect result (that is generally used to redirect to non-action URLs). 但是您可以使用redirect结果 (通常用于重定向到非操作网址)来实现。

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. 基本上,您只需要在Struts配置中指定名称空间和操作名称(它们也可以是动态的,TBH),以及一个表示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. 然后,在第一个Action(或BaseAction)中,您需要一个方法来获取Parameter Map,循环遍历每个参数(及其每个值),对它们进行URLEncode并返回已安装的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. 这将与表单数据(POST),查询参数(通常为GET)或同时与两者(带有表单数据 QueryString的POST)一起使用,并且它是URL安全的。

Struts config 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. org.foo.bar.cool。 RequestGrabberAction.java (Action classes) 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 / 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 请享用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM