实例解析JSP中EL表达式的各种运用(3)

package com.javaweb.ch08; import java.util.*; //一个简单的JavaBean示例 public class Person{ //name属性 private String name; //age属性 private int age; //sex属性 private String sex; //friends属性 private ArrayList friends; //无参构造方法 public Person(){ } //自定义构造的方法 public Person(String name,int age,String sex){ this.name = name; this.age = age; this.sex = sex; } //获取name的属性值 public String getName(){ return name; } //获取age属性值 public int getAge(){ return age; } //获取sex的属性值 public String getSex(){ return sex; } //设置name的属性 public void setName(String name){ this.name = name; } //设置age的属性值 public void setAge(int age){ this.age = age; } //设置sex的属性值 public void setSex(String sex){ this.sex = sex; } //设置friends的值 public void setFriends(ArrayList friends){ this.friends = friends; } //获取friends的属性值 public ArrayList getFriends(){ return friends; } }

<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.util.*,com.javaweb.ch08.*" %> <!DOCTYPE html> <html> <head> <title>设置JavaBean中的属性</title> </head> <body> <% //实例化一个Person Person person = new Person("zhangdapeng",24,"男"); //创建一个friends实例 ArrayList friends = new ArrayList(); //添加值 friends.add("wang wu"); friends.add("li si"); person.setFriends(friends); //存储在session范围内 session.setAttribute("person",person); %> <a href="https://www.jb51.net/GetJavaBean.jsp">跳转到GetJavaBean.jsp</a> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>取得JavaBean中的属性值</title> </head> <body> 姓名:${sessionScope.person.name}<br /> 年龄:${sessionScope.person.age}<br /> 性别:${sessionScope.person.sex}<br /> 朋友:${sessionScope.person.friends[0]},${sessionScope.person.friends[1]}<br /> </body> </html>

使用存储器读取Map中的数据

<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.util.*,com.javaweb.ch08.*" %> <!DOCTYPE html> <html> <head> <title>设置Map页面</title> </head> <body> <% //新建一个HashMap HashMap userInfo = new HashMap(); //在HashMap中设置值 userInfo.put("username","zhangdapeng"); userInfo.put("password","zhnagda123"); //将值存储到session范围中 session.setAttribute("userInfo",userInfo); %> <a href="https://www.jb51.net/GetMapDemo.jsp">跳转到GetMapDemo.jsp</a> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>使用存储器读取map中的数据</title> </head> <body> 用户名:${sessionScope.userInfo.username}<br /> 密码:${sessionScope.userInfo.password}<br /> </body> </html>

使用存储器读取数组中的数据

<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.util.*,com.javaweb.ch08.*" %> <!DOCTYPE html> <html> <head> <title>设置Array页面</title> </head> <body> <% String[] names = {"zhangda1","zhangda2","zhangda3"}; //将值存储到session范围中 session.setAttribute("names",names); %> <a href="https://www.jb51.net/GetMapDemo.jsp">跳转到GetArrayDemo.jsp</a> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>使用存储器读取map中的数据</title> </head> <body> 用户名1:${sessionScope.names[0]}<br /> 用户名2:${sessionScope.names[1]}<br /> </body> </html>

存储器的复杂应用

<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.util.*,com.javaweb.ch08.*" %> <!DOCTYPE html> <html> <head> <title>设置Array页面</title> </head> <body> <% ArrayList<Person> persons = new ArrayList<Person>(); Person person1 = new Person("wang wu",24,"男"); Person person2 = new Person("wang liu",24,"女"); persons.add(person1); persons.add(person2); session.setAttribute("persons",persons); %> <a href="https://www.jb51.net/GetMapDemo.jsp">跳转到GetArrayDemo.jsp</a> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>使用存储器读取map中的数据</title> </head> <body> 用户名1:${sessionScope.persons[0].name},${sessionScope.persons[0].age},${sessionScope.persons[0].sex}<br /> 用户名2:${sessionScope.persons[1].name},${sessionScope.persons[1].age},${sessionScope.persons[1].sex}<br /> </body> </html>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjpyj.html