0

I am working on Struts2 application. In my jsp, I have a javascript variable whose value is being assigned through struts tag s:iterator.

Something like this:

//Javascript

// an object agent with getter setter which is being used through request.getParameter().

<script>
var user = new Array();

// i want to assign value to javascript array variable through this Agent object

<s:if test='agent!=null'>
    <s:iterator var='i' value='agent'>
        <s:if test='i!=null'>
            user[user.length]= {
            agentName:'<s:property value='agentName'/>',
            agentId: '<s:property value='agentName'/>'
            };
        </s:if>
    </s:iterator>
</s:if>

</script>

This javascript array will be used for the dropdown menu optiion in html, where when a person select an agent name, its relevent data will get populated in the next dropdown options.

But the user variable is not taking values through struts tag.

I am stuck on this. Any help?

Rohit
  • 1
  • 2
  • Why you trying to get data in javascript. This is not a recombinant way. You should call ajax from your javascript code to get the data. please look for the right approach. – Pratik Bhajankar Apr 18 '19 at 09:57
  • Please refer below link https://stackoverflow.com/a/6577691/2943913 – Pratik Bhajankar Apr 18 '19 at 10:05
  • When a person selects a value struts can't see it until the value has been passed to the controller and appropriate result is returned. Another option is available if you use Map instead of Array and dynamically add options like in this answer https://stackoverflow.com/a/37483141/573032. – Roman C Apr 19 '19 at 22:21
  • This is a clunky way to do this--do this kind of work on the Java side and expose what you need instead of delegating responsibility to the view layer. – Dave Newton Apr 20 '19 at 10:46

0 Answers0