将参数从JSP传递到Struts 2中的操作类 [英] Passing a parameter from JSP to action class in Struts 2

查看:67
本文介绍了将参数从JSP传递到Struts 2中的操作类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击按钮(非提交按钮)时将文本字段的值作为URL参数从JSP传递到操作类,并在此链接中找到了解决方案:Struts2中的Onchange事件.

I am trying to pass value of a text-field as a URL parameter from a JSP to an action class on clicking of a button(non-submit button) and have found a solution in this link: Onchange event in Struts2.

我已经执行了该链接中提到的所有步骤,即:

I have followed all the steps mentioned in that link i.e.:

  • onClick事件创建javascript函数
  • 在函数setDealers内,将值"reportGroup"传递给操作类,如下所示
  • creating a javascript function for onClick event
  • inside the function i.e. setDealers, passing the value i.e. "reportGroup" to the action class as shown below
function setDealers(){
  var rep_value=document.getElementById("reportGroup").value;
  alert("Value is"+rep_value);
  window.location=="getDealersByGrouppopUpAction?reportGroup="+rep_value;
  alert("Just a check")
}

  • 在操作类中创建了一个名称为"reportGroup"的变量,即PopUpAction.java带有它的getter和setter方法.
    • created a variable with name "reportGroup" in the action class i.e. PopUpAction.java with getters and setters for it.
    • 为了支持所有这些功能,我在struts.xml中进行了以下配置:

      Also to support all this, I have following configuration in struts.xml:

      <action name="*popUpAction" class="popUpAction" method="{1}" > 
          <!--this will call a desired method present inside action class --> 
          ...
          ...
      </action>
      

      单击按钮时,应该调用PopUpAction类的getDealersByGroup方法,并在SQL查询中使用传递的值,即"reportGroup". 但是,按照上述javascript函数setDealers,只有警报命令会执行,并且所需的值不会传递给操作类.

      On clicking of the button, getDealersByGroup method of PopUpAction class is supposed to be invoked and use the passed value i.e. "reportGroup" in a SQL query. But as per the above javascript function setDealers only alert commands are getting executed and the desired value is not getting passed to the action class.

      struts.xml是否有任何丢失或错误的内容.

      Is there any thing missing/ or wrong with struts.xml.

      推荐答案

      首先,您在代码===

      window.location="getDealersByGrouppopUpAction?reportGroup="+rep_value;
      

      第二,这听起来像重定向到操作,用于调用操作,请使用 s:action $.ajax() 参见

      Second, this sounds like redirect to the action, for calling action use s:action or $.ajax() see example.

      技巧,对于URL,最好使用 s:url 标签以构建网址.

      Trird, for URLs better use s:url tag to build the url.

      var url = "<s:url action='getDealersByGrouppopUpAction'/>"+"?reportGroup="+rep_value;
      window.location=url;
      

      这篇关于将参数从JSP传递到Struts 2中的操作类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆