简体   繁体   中英

Passing a parameter from JSP to action class in Struts 2

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 ie:

  • creating a javascript function for onClick event
  • inside the function ie setDealers , passing the value ie "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")
}
  • created a variable with name "reportGroup" in the action class ie PopUpAction.java with getters and setters for it.

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>

On clicking of the button, getDealersByGroup method of PopUpAction class is supposed to be invoked and use the passed value ie "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.

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

First, you have made a typo in code == vs =

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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