Java – how to have dynamic parameter names in struts 2 action redirection?
So I'm trying to create an action redirect with dynamic parameter names and values I understand how to use struts This operation is performed for the parameter value in the XML file, but the content of the parameter name does not seem to be evaluated correctly
<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 ${actionname} and ${paramvalue} I have absolutely no problem Actionname, paramvalue and paramname all have appropriate getter / setter names in objectaction
Who knows how to correctly evaluate ${paramname}? It is currently displayed as "${paramname}" in the URL. I need it as the value of the paramname variable Due to my misunderstanding of ognl, I tried to use #paramname and% {paramname}, and they are also displayed incorrectly in the URL I also tried to add a parse = true parameter, but I believe struts 2 is the default anyway
Solution
This is work
<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 In 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; } }
Give website
http://localhost:8080/ProjectName/home.action?id=1
Now in homeaction In 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>
At 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/>
Give output
id=1