-1

I am trying to access <s:property value="Names"/> values in my JavaScript but getting error. Here Names is a list. I tried to put <s:property value="Names"/> in variable also but still its not converting to array. Not sure where I am going wrong . I have put in my JavaScript in JSP page. The evaluated value of <s:property value="Names"/> is coming as [abc,xyz]. Help is appreciated! Code:

<script type="text/JavaScript">
    $(document).ready(function() {
      var temp= new Array();
      temp=<s:property value="Names"/>;

    });
  </script> 

While debugging it shows:

<script type="text/JavaScript">
        $(document).ready(function() {
          var temp= new Array();
          temp=[xyz,abc];

        });
      </script>

Its gives error that xyz is undefined.

abhinav singh
  • 856
  • 6
  • 23
  • 45
  • If you don't show your code, I don't know how you expect anyone to tell you what you're doing wrong. – Barmar May 29 '14 at 17:30
  • You can use a list instead of array it would convert the value if the value has a type that supported by existing converter. – Roman C May 29 '14 at 17:37
  • 1
    You have to render legal JavaScript. If you want to render an array of strings in JavaScript then you need to actually render JavaScript. There are a number of options, including converting it into JSON, which in this case, would create the desired array of strings. – Dave Newton May 30 '14 at 00:31

1 Answers1

0

You can try this

< script type="text/JavaScript" >
$(document).ready(function() {
      var temp= new Array();

      <s:iterator id="names" value="names" status="stat">
         temp.push("<s:property/>");
      </s:iterator>
});
< / script>

In action class

 private List<String> names;

 @Override
 public String execute() throws Exception {
     names=new ArrayList<String>();
     names.add("xyz");
     names.add("abc");
 }

 public void setNames(List<String> names) {
      this.names = names;
 }

 public List<String> getNames() {
      return names;
 }
prem30488
  • 2,828
  • 2
  • 25
  • 57