【问题标题】:struts2-jquery-plugin+struts2-json-plugin : is it possible to have many JSON getters methods in the same action class?struts2-jquery-plugin+struts2-json-plugin :是否可以在同一个动作类中有许多 JSON getter 方法?
【发布时间】:2012-08-10 16:09:34
【问题描述】:

我的问题是:如果使用struts2-jquery-plugin+struts2-json-plugin,我可以定义很多函数吗:

@Actions( {
        @Action(value = "/func1", results = {
          @Result(name = "str1", type = "json")
        })
      })
public String func1(){
  //instructions
  return "str1";
}

@Actions( {
        @Action(value = "/func2", results = {
          @Result(name = "str2", type = "json")
        })
      })
public String func2(){
  //instructions
  return "str2";
}

还有许多 JSON 方法 getter,例如:

public String getJSON1()
{
  func1();
}
public String getJSON2()
{
  return func2();
}

在同一个动作类中?

非常感谢您。

【问题讨论】:

  • 我不明白目标。 getJSONn 函数如何与操作方法相关联?
  • 我认为他的意思是 get json 方法是动作。是的,您可以有多个返回 json 的操作方法。这往往会出现 FindEmployees、FindCustomers、FindSuppliers 的搜索参数相同的情况。然而,将导致多个动作的共性移动到它自己的带有字符串或更好的枚举的设置器中更有意义,然后你回到一个动作。

标签: jquery-plugins struts2 jsonplugin


【解决方案1】:

我过去曾尝试过,但没有成功。但我设计了一个解决方法,也许这对你有用。解决方法将参数传递给操作。例如

动作类

private String jsonMethod;
//getters and setters

public String getJSON()
{
     if(jsonMethod.equals("findEmp"))
     {

     }else if(jsonMethod.equals("findCust"))
     {

     }
     return SUCCESS;
}

来自你的jsp

<s:url id="findEmp" namespace="/" action="getJSON" >
    <s:param name="jsonMethod">findEmp</s:param>
</s:url> 
<s:url id="findCust" namespace="/" action="getJSON" >
    <s:param name="jsonMethod">findCust</s:param>
</s:url> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多