I have a jsp view file named example.jsp and when click a button, below jquery method invoked :
$(document).ready(function() {
$("#a_button").click(function() {
var json_model== {
field1 : $("#a_textarea1").val(),
field2 : $("#a_textarea2").val(),
field3 : $("#a_textarea3").val(),
};
$.getJSON('AnAction', {
model : json_model
}, function(jsonResponse) {
alert('jsonResponse here');
});
});
});
And here my action class :
public class ActionClass implements Action {
private Model model;
@Override
public String execute() throws Exception {return null;}
public String actionMethod() {
System.out.println("Its Here");
System.out.println(model.getField1()+" "+model.getField2()+" "+model.getField3()+" ");
return SUCCESS;
}
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
}
And here Model class :
public class Model {
private String field1;
private String field2;
private String field3;
public Model(String field1,String field2, String field3) {
super();
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setResult(String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
}
My struts.xml :
<action name="AnAction" class="controller.ActionClass" method="actionMethod">
<result type="json">/example.jsp</result>
</action>
When I click the button, my action call works and I see Its Here text on server side console. But action call does not map the Model object.I mean 'model' is null when action invoked. However when I move field1,field2 and field3 to ActionClass like below this works. But of course I want only model object inside Action Class. How can I do that. Thanks for all advices.
//THIS IS WORKED VERSION OF ACTION CLASS
public class ActionClass implements Action {
private String field1;
private String field2;
private String field3;
@Override
public String execute() throws Exception {return null;}
public String actionMethod() {
System.out.println("Its Here");
System.out.println(field1+" "+field2+" "+field3+" ");
return SUCCESS;
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setResult(String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
}
//AND MY JQUERY METHOD WHICH IS COMPATIBLE WITH ABOVE
$(document).ready(function() {
$("#a_button").click(function() {
$.getJSON('AnAction', {
field1 : $("#a_textarea1").val(),
field2 : $("#a_textarea2").val(),
field3 : $("#a_textarea3").val()
}, function(jsonResponse) {
alert('jsonResponse here');
});
});
});
You can use parameter names prefixed with model.
var json_model = {
"model.field1" : $("#a_textarea1").val(),
"model.field2" : $("#a_textarea2").val(),
"model.field3" : $("#a_textarea3").val(),
};
$.getJSON('AnAction', json_model, function(jsonResponse) {
alert('jsonResponse here');
});
The model also needs a default constructor,
public Model() {}
so you can instantiate it to the action
private Model model = new Model();
After these changes the params
interceptor would be able to populate the model
object with the help of OGNL. And because your model properties are only strings they would be set without any possible type conversion of the parameters.