zoukankan      html  css  js  c++  java
  • 在jsp中接收并处理servlet传过来的含有bean的List

    在jsp中接收并处理servlet传过来的含有bean的List

    例如有以下bean

    package com.test.domain;
    class Student{
    	private Stirng name;
    	private int age;
    	
    	public Student(String name, int age){
    		this.name = name;
    		this.age = age;
    	}
    
    	public String getName(){
    		return this.name;
    	}
    
    	public void setName(Stirng name){
    		this.name = name;
    	}
    
    	public int getAge(){
    		return this.age;
    	}
    
    	public void setAge(int age){
    		this.age = age;
    	}
    }  
    

    在Servlet中有如下代码:

    List<Student> stuList = new ArrayList<Student>();
    stuList.add(new Student("Tom", 19));
    stuList.add(new Student("Jerry", 20));
    request.setAttribute("stuList", stuList);
    

    在jsp中接收并处理传过来的stuList:

    首先在jsp中导入Student的包如下:

    <%@ page import= "com.test.domain.Student"%>    
    

    在JavaScript中通过如下方式处理:

    <script type="text/javascript">
    	var stuName = new Array();
    	<% int i=0;List<Student> stuList = (List<Student>) request.getAttribute("stuList");
    	   for(i = 0; i < stuList.size(); i ++) { %>
    			stuName[<%=i%>] = <%=((Student) stuList.get(i)).getName()%>;				
    	<% } %>
    </script>
  • 相关阅读:
    java代码split分割数字类
    P1330 封锁阳光大学
    1022 舞会2
    1626 爱在心中
    P2024 食物链(two)
    P1196 银河英雄传说
    P1892 团伙
    P1546 最短网络(最小生成树)
    烦人的幻灯片(拓扑)
    例4.15 奖金(拓扑排序)
  • 原文地址:https://www.cnblogs.com/liming1593/p/6516533.html
Copyright © 2011-2022 走看看