最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

assign javascript variable to jsp code? - Stack Overflow

programmeradmin0浏览0评论

I have 4 arraylist in jsp and I want to get elements of lists from javascript function.

it looks like there is no problem getting one element from jsp arraylist.

But I don't know how to move multiple or all elements from jsp arraylist to javascript code.

below is my code

<script>
    function makeTable(){
    for(c=0;c<row_num;c++){
        row[c]=document.createElement('tr'); 
        for(k=0;k<cell_num;k++) {
            cell[k]=document.createElement('td');
            cont = document.createElement('a');
            cont.href="./bbs_view.jsp?count=" + c;
            cont.innerHTML ="<%=title.get(0)%>";
            cell[k].appendChild(cont);
            row[c].appendChild(cell[0]);
        }
    }
}
</script>

as you can see, above function can get only one element from jsp arraylist.
is there any way to replace <%=title.get(0)%> to
something like <%=title.get( javascript var c)%>?

I have 4 arraylist in jsp and I want to get elements of lists from javascript function.

it looks like there is no problem getting one element from jsp arraylist.

But I don't know how to move multiple or all elements from jsp arraylist to javascript code.

below is my code

<script>
    function makeTable(){
    for(c=0;c<row_num;c++){
        row[c]=document.createElement('tr'); 
        for(k=0;k<cell_num;k++) {
            cell[k]=document.createElement('td');
            cont = document.createElement('a');
            cont.href="./bbs_view.jsp?count=" + c;
            cont.innerHTML ="<%=title.get(0)%>";
            cell[k].appendChild(cont);
            row[c].appendChild(cell[0]);
        }
    }
}
</script>

as you can see, above function can get only one element from jsp arraylist.
is there any way to replace <%=title.get(0)%> to
something like <%=title.get( javascript var c)%>?

Share Improve this question asked Mar 29, 2012 at 10:38 Seho LeeSeho Lee 4,1089 gold badges34 silver badges42 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

No.

JSP runs on the server. It outputs some text. The browser interprets that text as JavaScript. There is no path back.

Instead, use a JSON encoder to provide the array in a JavaScript friendly format. Use that in your script so you get a JavaScript array, then loop over that.

Emit a javascript array within your scriplet (assumin titles is a java collection of Strings) and use the js counterpart of your array.

<script>

var jsArray = [];

<% int i = 0; foreach (String iterat : titles) { %>

jsArray[<%= i %>] = '<%= iterat %>';

<% i++; } %>
</script>

Is roughly unelegant (better have this in a custom tag, but should work).

BEWARE: typed on the fly, can have syntax typos in ^^

BigMike

Ok.. I finally made it.

for those who will have same question like me, I would like to leave my code.

<%@import="java.sql.*, java.util.ArrayList"%>

<%  
class bbsData{
    String title;
    String date;
    String body;
    int count;
}

ArrayList<bbsData> bbslist = new ArrayList<bbsData>();
    try{
     rs=stmt.executeQuery("select * from bbsdata");
     while(rs.next()){
        bbsData bbsOb = new bbsData();
        bbsOb.date = rs.getString(1);
        bbsOb.title= rs.getString(2);
        bbsOb.body = rs.getString(3);
        bbsOb.count = Integer.parseInt(rs.getString(4));
        bbslist.add(bbsOb);
    }    
    stmt.close();
    Conn.close();        
}catch(Exception e){
    out.println(e);
} 
<script>

var jsArray = [];

function dbData(date, title, body, count){  
   this.date = date;
   this.title = title;
   this.body = body;
   this.count = count;
}
function setJsArry(){
    <% int i = 0;
       for (bbsData iterat : bbslist) {
           iterat = new bbsData();
           iterat = bbslist.get(i);
     %>
    date = '<%= iterat.date %>'; 
    title = '<%= iterat.title %>';
    body = '<%= iterat.body %>';
    count = '<%= iterat.count %>';
    jsArray[<%=i%>] = new dbData(date, title, body, count);
       <%i++; } %>

}
function makeTable(){
    setJsArry();
    for(c=0;c<row_num;c++){
    row[c]=document.createElement('tr'); 
       for(k=0;k<cell_num;k++) {
          cell[k]=document.createElement('td');
          cont = document.createElement('a');
          cont.href="./bbs_view.jsp?count=" + c;
          cont.innerHTML =jsArray[c].title;
          cell[k].appendChild(cont);
          row[c].appendChild(cell[k]);
      }
  }
}
</script>
发布评论

评论列表(0)

  1. 暂无评论