struts2中向结果传递参数不起作用

发布于 2024-10-04 05:49:32 字数 1747 浏览 0 评论 0原文

我已配置结果如下:它是我的自定义结果类型。

<result-types>
    <result-type name="myBytesResult" class="blahblah.MyBytesResult" />
</result-types>


<action name="myAction" class="blahblah.MyAction">
    <result name="success" type="myBytesResult">
       <param name="pptId">${pptId}</param>
    </result>
</action>

我的结果有 pptId 的 setter/getter,MyAction 也有 pptId 的 setter/getter。但是当我检查结果时,它没有设置 pptId (我在结果中得到 ${pptId} 作为字符串)。它似乎没有从 Action 中获取。

同样的原因可能是什么?

代码 MyAction

public String doDefault() {
       System.out.println("Default Called");
       setPptId("MyPpt");
      return "success";
  }

  public byte[] getMyImageInBytes() throws Exception { 
       try {
                //.....
       } catch (Exception e) {

       }
       return null;

  }


public String getContentType() {
    return contentType;
}

public void setContentType(String contentType) {
    this.contentType = contentType;
}

public String getPptId() {
    return this.pptId;
}

public void setPptId(String pptId) {
    this.pptId = pptId;
}

MyBytesResult

private String contentType;

private String pptId;


public void execute(ActionInvocation invocation) throws Exception {
    HttpServletResponse response = ServletActionContext.getResponse();
         //...Some more code for settign response
    System.out.println("pt Id[" + this.pptId + "]");

}


public String getContentType() {
    return contentType;
}


public void setContentType(String contentType) {
    this.contentType = contentType;
}


public String getPptId() {
    return pptId;
}


public void setPptId(String pptId) {
    this.pptId = pptId;
}

I have configured result as follows : Its my custom result type.

<result-types>
    <result-type name="myBytesResult" class="blahblah.MyBytesResult" />
</result-types>


<action name="myAction" class="blahblah.MyAction">
    <result name="success" type="myBytesResult">
       <param name="pptId">${pptId}</param>
    </result>
</action>

And my result has setter/getter for pptId and MyAction also has setter/getter for pptId. But when i check in my result, Its not setting pptId (I am getting ${pptId} as string in result). It seems its not getting getter from Action.

What could be reason for the same ?

The code MyAction

public String doDefault() {
       System.out.println("Default Called");
       setPptId("MyPpt");
      return "success";
  }

  public byte[] getMyImageInBytes() throws Exception { 
       try {
                //.....
       } catch (Exception e) {

       }
       return null;

  }


public String getContentType() {
    return contentType;
}

public void setContentType(String contentType) {
    this.contentType = contentType;
}

public String getPptId() {
    return this.pptId;
}

public void setPptId(String pptId) {
    this.pptId = pptId;
}

MyBytesResult

private String contentType;

private String pptId;


public void execute(ActionInvocation invocation) throws Exception {
    HttpServletResponse response = ServletActionContext.getResponse();
         //...Some more code for settign response
    System.out.println("pt Id[" + this.pptId + "]");

}


public String getContentType() {
    return contentType;
}


public void setContentType(String contentType) {
    this.contentType = contentType;
}


public String getPptId() {
    return pptId;
}


public void setPptId(String pptId) {
    this.pptId = pptId;
}

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

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

发布评论

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

评论(1

忘羡 2024-10-11 05:49:32

更新答案

因此,经过一番挖掘后,看来这实际上工作正常。如果需要,结果负责将传入数据评估为 OGNL 表达式。这就是重定向和 http 标头结果的工作原理。您可以根据自定义结果中的堆栈解析该值,如下所示:

String resolvedPptId = TextParseUtil.translateVariables(pptId, stack)

来自 translateVariables 的 Javadoc:

表达式中${...}和%{...}的所有实例转换为调用{@link ValueStack#findValue(java.lang.String)返回的值)}。如果某个项目不能
在栈上找到(返回null),那么整个变量${...}就不是
显示,就像该项位于堆栈上但返回空字符串一样。

Updated answer

So, after some digging, it appears that this is actually working properly. It is the responsibility of the Result to evaluate the incoming data as an OGNL expression if that is necessary. This is how the redirect and http header results work. You can parse the value against the stack in your custom result as follows:

String resolvedPptId = TextParseUtil.translateVariables(pptId, stack)

From the Javadoc for translateVariables:

Converts all instances of ${...}, and %{...} in expression to the value returned by a call to {@link ValueStack#findValue(java.lang.String)}. If an item cannot
be found on the stack (null is returned), then the entire variable ${...} is not
displayed, just as if the item was on the stack but returned an empty string.

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