使用bean将数组从java传递给javascript

时间:2013-09-17 07:22:43

标签: java javascript jsp

我试图将一个String []从一个java类传递给一个jsp文件,我设法传递一个简单的字符串,但我现在已经停留了几天。有人可以帮助我吗,因为我是新人,所以我想要更详细的答案。

Java类:

public class UserArray extends Applet {

public String  javaArray [] =
        { "array 1", "array 2" , "array 3" };

public String [] getJavaArray() {
    return javaArray;
}
}

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sb" uri="/struts-bootstrap-tags" %>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <jsp:useBean id ="array" class="eu.xyx.ztr.applet.UserArray"/>
</head>
<body>
   <h4>

  <jsp:getProperty name ="array" property="javaArray"/> 

 </h4>

</body>

到目前为止,我有一个maage来获取String []的地址。我只是想获取javaArray的值,如果它可以用这样的形式获取它们:

<script>

    function addrow(tableID){


        var numeuser=document.numeuser;

        var nume=["adrian","cristi","levi",numeuser];
        var prenume=["ric","dre","asd"];
        var email=["rew@td","qq@eqwq","ee@ew"];

2 个答案:

答案 0 :(得分:0)

你应该使用像谷歌JSON或杰克逊JSON这样的javascript库 看看这些链接

http://viralpatel.net/blogs/creating-parsing-json-data-with-java-servlet-struts-jsp-json/

答案 1 :(得分:0)

的Servlet

   protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    ArrayList myArray = new ArrayList();  
    myArray.add("array 1");  
    myArray.add("array 2");  
    myArray.add("array 3");  
    request.setAttribute("myArray", myArray);  

    //then call jsp :   
    RequestDispatcher rd = request.getRequestDispatcher("mypage.jsp");  
    rd.forward(request, response);  

}

JSP

  <body>
      <c:forEach var="myarr" items="${myArray}">     
        <c:out value="${myarr}" />      
      </c:forEach>   
 </body>