Reputation: 163
I am struggling with a supposedly simple task. I have an ajax call to a method on action class. How should the action method look like? Thanks.
$.ajax( {
type : "POST",
url : "loadBenchMark",
data : {rulesetInt: 1},
success : function(data) {
console.log('success');
},
error : function(request, textStatus, errorThrown) {
console.log("Error Thrown:" +request.statusText);
}
});
My struts.xml
<action name="loadBenchMark"
class="com.test.cashdesk.mktpx.ui.action.LoadDefaultsAction" method="retrieveBenchmark">
<result type="json"></result>
</action>
I tried my action method to be like
public String retrieveBenchmark(int rulesetInt) {
// do something here
return SUCCESS;
}
but the call to the action throws a 500 internal server error
.
having a getter setter
for rulesetInt
and call to action method like this works.
public String retrieveBenchmark() { // access rulesetInt here}
Answer from here
Please help.
Upvotes: 1
Views: 4175
Reputation: 163
Thanks for the answers. I used 'Steven Benitez' way of doing to achieve what I wanted.
I needed to post a lot of complex javascript object arrays as part of ajax call. So now Declared a String variable ajaxJSONData
in the action with getter and setter. I put some code below. I deserialize the string on java side.
Ajax call:
var postData = ajaxPostData("loadGridData");
// <![CDATA[
$.ajax( {
type : "POST",
url : "finalRuleTbaAction",
data : {"ajaxJSONData" : postData},
dataType : "json",
success : function(result) { ............
});
function :
function ajaxPostData(callingMethod) {
if("loadTemplateAndCoupons" == callingMethod) {
var result = [];
result.push(new jsObject("ruleset", $('#ruleset').val()));
result.push(new jsObject("benchmark", $('#productSelect').val()));
return JSON.stringify(result);
} else if("loadGridData" == callingMethod) {
var result = [];
result.push(new jsObject("ruleset", $('#ruleset').val()));
result.push(new jsObject("benchmark", $('#productSelect').val()));
result.push(new jsObject("couponmin", $('#couponmin').val()));
result.push(new jsObject("couponmax", $('#couponmax').val()));
return JSON.stringify(result);
}
}
Upvotes: 0
Reputation: 8016
If you want to send different javascript objects and dont want to declare getter/setter for them in the action class use the following method
In your action class decleration
public class YourClass extends ActionSupport implements ServletRequestAware
then declare this varible and its getter/setter
HttpServletRequest request
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletRequest getServletRequest() {
return this.request;
}
Now in your function
public String retrieveBenchmark() {
String stringValue=getServletRequest().getParameter("rulesetInt");
int rulesetInt=Integer.parseInt(stringValue);//do this if your value is integer
// do something here
return SUCCESS;
}
now you can send any variable and without declaring getter function you can access it using getServletRequest().getParameter("yourVariableName")
Upvotes: 1
Reputation: 11055
No, Struts2 action methods do not take arguments. If you want to set a parameter called rulesetInt
, you will need a setRulesetInt
method.
Example
private int rulesetInt;
public String retrieveBenchmark() {
// do something here
return SUCCESS;
}
public void setRulesetInt(final int rulesetInt) {
this.rulesetInt = rulesetInt;
}
Upvotes: 1