【问题标题】:Struts Method with Parameter from Ajax GET带有来自 Ajax GET 的参数的 Struts 方法
【发布时间】:2018-07-30 21:15:08
【问题描述】:

我从 JavaScript 发出 GET Ajax 请求,

    $.ajax({
        type: "GET", 
        url: "myMethod?id=" + id, 
        success: function(data) {
           // ...
        } 

    });

服务器应用程序是带有 struts.xml 的 Struts。该操作在 struts.xml 中映射为

<action name="myMethod" method="myMethod" class="myapp.SomeClass">
        <result type="json">
            <param name="contentType">text/plain</param>
        </result>       
    </action>   

并且该类具有带参数的方法,

public String myMethod(int id) {
  //..      
}

但我得到的错误是

java.lang.NoSuchMethodException: myapp.myMethod()

从这个线程我知道 Struts 不支持参数化映射方法, Calling method with arguments in struts 2?

但他们的解决方案(引入 Action 属性)在这里似乎不合适。 id 不是服务器维护的表单字段或操作属性,它是从 JS 层传入的东西。

那么如何在 Struts 中使用 GET 参数来处理这个自定义的 JS Ajax 场景呢?

我没有使用注释,即使这是一个 Struts2 应用程序。整个应用程序是用配置而不是注释编写的。

【问题讨论】:

    标签: struts2 struts


    【解决方案1】:

    对于 struts 中的数据绑定,您必须将 int id 设置为具有 setter 和 getter 的操作类的字段,并且该方法应与此类字段一起使用,而不是将其作为参数接收。

    public class ExampleAction extends ActionSupport {
      private Integer id;
    
      public String method() {
        doSomething(this.id);
        return SUCCESS;
      }
    
      public void setId(Integer id) ...
      public String getId() ...
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 1970-01-01
      • 2014-02-18
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2018-07-18
      相关资源
      最近更新 更多