How do I name dynamic parameters in a Struts 2 action redirect?

So, I'm trying to create an action redirect with dynamic parameter names as well as values. I understand how to do this in struts.xml for parameter values, but cannot force things to evaluate the parameter name 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 problems. actionName, paramValue and paramName are all appropriately named getters / setters inside an ObjectAction.

Does anyone know how I can correctly evaluate $ {paramName}? Currently, it appears as "$ {paramName}" in the url, and I need this to be the value of the paramName variable. I tried using #paramName and% {paramName} due to my misunderstanding of OGNL, and they all also display incorrectly in the url. I also tried adding the parameter parse = true, but I believe that Struts 2 will still be the default.

+4
source share
1 answer

He works.

  <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/> 

Produces output

  id=1 
+1
source

Source: https://habr.com/ru/post/1346501/


All Articles