4

So I'm trying to create an action redirect that has dynamic parameter names as well as values. I understand how to do this in the struts.xml file for parameter values, but cannot seem to get things to evaluate correctly for the parameter name.

    <action name="SaveObject" method="save"
        class="com.mysite.actions.ObjectAction">
        <result name="success" type="redirectAction">
            <param name="actionName">${actionName}</param>
            <param name="${paramName}">${paramValue}</param>
        </result>
    </action>

Now the ${actionName} and ${paramValue} I have absolutely no issue with. actionName, paramValue, and paramName all have appropriately named getters/setters inside the ObjectAction.

Does anyone know how I can get the ${paramName} to evaluate correctly? It currently shows up as "${paramName}" in the URL and I need it to be the value of the paramName variable. I've tried using #paramName and %{paramName} due to my misunderstanding of OGNL and they all show up incorrectly in the URL as well. I've also tried adding a parse=true parameter, but I believe Struts 2 does that be default anyway.

  • shouldn't your result type be "redirect-action"? – geowa4 Apr 04 '11 at 20:28
  • It was renamed between 2.0 and 2.1 [link](https://cwiki.apache.org/S2WIKI/troubleshooting-guide-migrating-from-struts-20x-to-21x.html#TroubleshootingguidemigratingfromStruts2.0.xto2.1.x-Troubleshooting) – throwaway256623 Apr 04 '11 at 22:01
  • check this SO question,may be it will help you [SO Question](http://stackoverflow.com/questions/1313121/struts2-dynamic-parameter-name-in-redirect-action) – Umesh Awasthi Apr 04 '11 at 05:01
  • Yeah I actually found that question before posting my own. If you look at what allegedly works, it's essentially identical to what I've written, yet mine doesn't appear to be evaluating correctly. Thanks though! – throwaway256623 Apr 04 '11 at 05:06
  • thats seems a bit strange since this should work.please check if your paramName should not be empty nor it should be empty.. – Umesh Awasthi Apr 04 '11 at 05:24
  • It is not empty. In fact, putting a `${paramName}` in the result shows the paramName being evaluated correctly in the URL when it's used as a value, but still the wrong evaluation when using it as a name. It's very odd. – throwaway256623 Apr 04 '11 at 05:33
  • that's strange,even i am now clueless.. – Umesh Awasthi Apr 04 '11 at 05:38

1 Answers1

1

It is working.

   <action name="login" class="com.common.LoginAction" >
         <result name="success" type="redirectAction">
          <param name="actionName">${actionName}</param>
         <param name="${paramName}">${paramValue}</param>
         </result>
  </action>

In LoginAction.java

  package com.common;
  import com.opensymphony.xwork2.ActionSupport;
  public class LoginAction extends ActionSupport {
private static final long serialVersionUID = -1449554101273745861L;

private String paramName;
private String actionName;
private String paramValue;
public String execute(){

    paramName="id";
    setParamValue("1");
    setActionName("home");
    return SUCCESS; 
}
public void setParamName(String paramName) {
    this.paramName = paramName;
}
public String getParamName() {
    return paramName;
}
public void setParamValue(String paramValue) {
    this.paramValue = paramValue;
}
public String getParamValue() {
    return paramValue;
}
public void setActionName(String actionName) {
    this.actionName = actionName;
}
public String getActionName() {
    return actionName;
}
 }

Gives url

 http://localhost:8080/ProjectName/home.action?id=1

Now in HomeAction.java

 package com.common;

 import com.opensymphony.xwork2.ActionSupport;

public class HomeAction extends ActionSupport{
private static final long serialVersionUID = -127700165200747324L;
private int id;
public String execute(){

    return SUCCESS; 
}
public void setId(int id) {
    this.id = id;
}
public int getId() {
    return id;
}
}

And

 <action name="home" class="com.common.HomeAction" >
        <result name="success">Home.jsp</result> 
        <result name="error">index.jsp</result>
        <result name="input">index.jsp</result>
  </action>

In Home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

id=${id}<br/>

Gives output

 id=1
prem30488
  • 2,828
  • 2
  • 25
  • 57