通过重定向到 Action 保留所有请求参数 [英] Preserving all request parameters through a redirect to an Action

查看:26
本文介绍了通过重定向到 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?

<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 中进行硬编码?

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

推荐答案

您不能使用 redirectAction 来实现,其中参数名称和值可以是动态的,但参数的数量必须是硬编码的,例如

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>

但是您可以使用 redirect 结果(通常用于重定向到非操作 URL)来实现.

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

基本上,您只需要在 Struts 配置中指定命名空间和操作名称(它们也可以是动态的,TBH),以及一个表示 QueryString 的动态参数.

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.

然后在第一个 Action(或在 BaseAction 中),您需要一个方法来获取 Parameter Map,循环遍历每个参数(及其每个值),对它们进行 URLEncode 并返回安装的 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.

这将适用于表单数据 (POST)、查询参数(通常为 GET)或两者(POST 与表单数据 QueryString),并且它是 URL 安全的.

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

<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(动作类)

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>

享受

这篇关于通过重定向到 Action 保留所有请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆