简体   繁体   English

如何将参数化方法传递给Struts 2动作的method属性?

[英]How to pass a parameterised method to a Struts 2 action's method attribute?

I'm going to do search data from database using like expression. 我将使用like表达式从数据库中搜索数据。 My method looks like this: 我的方法如下所示:

public List<Item> getData(String qString){
   return itemJpa.getSuggestedData(qString);
}

My purpose is when I input a query string to the qString variable, I need to get the data in the list. 我的目的是当我向qString变量输入查询字符串时,我需要获取列表中的数据。 I'm doing this using Struts2. 我正在使用Struts2。 I need to map this method to the struts action's method attribute. 我需要将此方法映射到struts操作的method属性。 Such as

<action name="DummyDB" class="com.shopping.op.welcome.DummyDB" method="**In Here**"></action> 

Is it possible? 可能吗? If it is how can I do this? 如果是我该怎么办?

You can't map an action to a method that has parameters in signature, or return result other than String . 您不能将动作映射到在签名中具有参数的方法,也不能返回String以外的结果。 What can you do is create another method for example doData() 您可以做的是创建另一个方法,例如doData()

private List<Item> data;
//getter and setter

public String doData() {
  data = getData(qString);
  ...
  return Action.SUCCESS;
}

<action name="DummyDB" class="com.shopping.op.welcome.DummyDB" method="doData">
 <result>/jsp/whateveryoudo.jsp</result>
</action>

In addition to it you need to know that a class you mapped to the action should be a qualified java bean which have a default no-arg constructor or doesn't have constructors at all (in this case the implicit default constructor is used). 除此之外,您需要知道映射到该动作的类应该是合格的Java bean,它具有默认的no-arg构造函数或根本没有构造函数(在这种情况下,使用隐式默认构造函数)。 Struts will instantiate the bean when you make a request to the action mapped by this action config. 当您请求由该动作配置映射的动作时,Struts将实例化bean。 So, to get it able to do you should follow these simple rules. 因此,要使其能够工作,您应该遵循以下简单规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM