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

查看:185
本文介绍了通过重定向到Action保留所有请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在更新页面中的记录后使用更新的消息(成功/失败)填充记录。这两个动作来自同一页面。我已将代码添加为,在完成Update操作后,将结果类型添加为Chain,并显示成功消息。但是在更新操作完成后立即点击搜索(第一次)时它不会消失。帮助我在点击搜索操作时清除消息。

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>

但您可以使用重定向结果(通常用于重定向到非操作网址)。

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中),你需要一个方法来获取参数映射,循环遍历每个参数(及其每个值),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 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 (行动类)

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

/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天全站免登陆