如何在 Struts 2 操作重定向中拥有动态参数名称?

发布于 2024-10-30 01:51:51 字数 771 浏览 6 评论 0原文

因此,我尝试创建一个具有动态参数名称和值的操作重定向。我了解如何在 struts.xml 文件中对参数值执行此操作,但似乎无法正确评估参数名称。

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

现在我对 ${actionName} 和 ${paramValue} 绝对没有问题。 actionName、paramValue 和 paramName 在 ObjectAction 中都有适当命名的 getter/setter。

有谁知道如何让 ${paramName} 正确评估?它目前在 URL 中显示为“${paramName}”,我需要它作为 paramName 变量的值。由于我对 OGNL 的误解,我尝试使用 #paramName 和 %{paramName},它们在 URL 中也显示不正确。我也尝试过添加一个 parse=true 参数,但我相信 Struts 2 无论如何都会默认它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2024-11-06 01:51:51

它正在发挥作用。

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

在 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;
}
 }

给出 url

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

现在在 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;
}
}

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

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

给出输出

 id=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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文