Search code examples
javajavascriptjquerystruts2struts

Action class cannot fetch changed value from jsp


I have hidden field in my jsp

<s:hidden id = "selectedCombo" name="selectedCombo" value=""/>
<s:hidden id = "xyz" name="xyz" value="2"/>

I need to change it before submitting it to action, so I have written script code as below

var e = document.getElementById("somecombo");
var app = e.options[e.selectedIndex].value; 

var combo = document.getElementById("selectedCombo");
combo.value = app;
alert(combo.value);

var e = document.getElementById("rolesForm");
e.submit();

At this point, value of "selectedCombo" is changed and giving me updated value in alert.

Problem :

  • But I am not getting that updated value of "selectedCombo" hidden field in my action class. I am using struts 2 so I am trying to fetch value by OGNL.

  • I am successfully getting value of "xyz" in my action class as its value is static. But not working with "selectedCombo" !!

Is it OGNL cannot store changed value ?

Help appreciated.


Solution

  • It is not the problem of OGNL.

    You are not setting value to hidden field selectedCombo instead you are setting value to JavaScript local variable combo

    var combo = document.getElementById("selectedCombo");
    combo.value = app; 
     ↑  
    

    Now, combo variable has value assigned by app.

    You need to change the code to,

    document.getElementById("selectedCombo").value = app; //now this will set the value