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>
  • 相关阅读:
    Lua弱引用table
    Javascript定义类(class)的三种方法
    双检锁技术
    【翻译】ASP.NET缓存管理
    socket python
    mvc项目
    MSBuild
    阅读glibc源码
    MVC3使用Unity实现依赖注入接口与于实现类自动注册
    C# 指针之美
  • 原文地址:https://www.cnblogs.com/liming1593/p/6516533.html
Copyright © 2011-2022 走看看