I have a problem with setting the nested field using jQuery using Struts2 framework.
I can't set the field of ModelDriven
object myModel
if this object has field on second level.
That is:
I cannot set the field like myModel.myObject.myField
, but I can set myModel.myField
.
Can somebody help and tell how to set the nested field in the model?
To be clear:
Code below works:
<script>
function myFunction(myVal) {
$.getJSON('login/open', {
myField : myVal
}, function(data) {
if(data.ajaxResponseCode == 0){
$("#myButton").remove();
$("#myId").html("id set to=" + data.idSet + '');
};
return false;
});
};
</script>
Code below does NOT work:
<script>
function myFunction(myVal) {
$.getJSON('login/open', {
myObject.myField : myVal
}, function(data) {
if(data.ajaxResponseCode == 0){
$("#myButton").remove();
$("#myId").html("id set to=" + data.idSet + '');
};
return false;
});
};
</script>
What is interesting setting the field with JSP Struts tags works:
<s:textfield name="myObject.myField" />
If you are using model driven action and implemented ModelDriven
interface you should make sure the interceptor stack is configured properly to the action. See this answer for better understanding interceptor stack configuration.
You can also use a devMode
and examine exceptions produced by OGNL to check error messages. Look at this answer for better explanation and example of using a model driven.
If you have all required configuration and Java objects accessible to evaluate an OGNL expressions, then you can check OGNL Basics to better understand OGNL working.
When calling a jQuery Ajax function and using options for parameter passing to the Struts2 action, make sure that you get parameter names in the request before params
interceptor populate them to the action bean or model.
Try this code and change it if it works for you
$.getJSON('login/open', {
'myObject.myField' : myVal
}, function(data) {