//需求-输出Person类中的普通属性,数组属性,list集合属性和map集合属性
1.创建person类
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Person {
//需求-输出Person类中的普通属性,数组属性,list集合属性和map集合属性。
private String name;
private String[] phone;
private List<String> cities;
private Map<String,Object> map;
@Override
public String toString() {
return "Person{" +
"name=" + name +
", phone=" + Arrays.toString(phone) +
", cities=" + cities +
", map=" + map +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getPhone() {
return phone;
}
public void setPhone(String[] phone) {
this.phone = phone;
}
public List<String> getCities() {
return cities;
}
public void setCities(List<String> cities) {
this.cities = cities;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}
2.创建jsp文件
<%@ page import="com.atshangqiu.com.Person" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2020/11/21
Time: 20:57
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Person p=new Person();
p.setName("chenbin");
p.setPhone(new String[]{"1272772","12312312"});
List<String> cities=new ArrayList<String>();
cities.add("北京");
cities.add("阿萨德");
p.setCities(cities);
Map map=new HashMap();
map.put("key1","value1");
map.put("key2","value2");
p.setMap(map);
pageContext.setAttribute("person",p);
%>
输出person:${person}<br/>
输出person的name属性${person.name}
输出person的pones数组的属性值:${person.phone[0]}
输出person的cities集合的元素值:${person.cities}
输出person的list集合中个别元素值:${person.cities[0]}
输出person的map集合:${person.map}
输出person的map集合:${person.map.key1}
${pageScope.person }
</body>
</html>
3。<c:set/>,<c:if/>,<c:foreach/>相关案例
student类
public class Student {
//编号,用户名,密码,年龄,电话信息
private Integer id;
private String username;
private String password;
private Integer age;
private String phone;
public Student(Integer id, String username, String password, Integer age, String phone) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
this.phone = phone;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
jsp文件
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.atshangqiu.com.Student" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2020/11/22
Time: 16:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
<c:set/>作用:set标签可以往域中保存数据
域对象.setAttribute(key,value);
保存到哪个域
scope属性设置保存到哪个域
page表示pagecontext域
request表示request域
session表示session域
application表示servletcontext域
var属性是设置key是多少
value属性设置value是多少
--%>
保存之前:${requestScope.abc}
<c:set scope="request" var="abc" value="abcvalue"></c:set>
保存之后:${requestScope.abc}
<%--
if标签用来做if判断
<c:if/>
test属性表示判断的条件(使用el表达式输出)
--%>
<c:if test="${12==12}">
<h1>qwewq</h1>
</c:if>
<%--
<c:choose><c:when><c:otherwise>标签
作用:多路判断。跟switch...case...default非常接近
--%>
<%
request.setAttribute("height",178);
pageContext.setAttribute("zhong",156);
%>
<%--
choose标签开始选择判断
when标签表示每一种判断情况
test属性表示当前这种判断情况的值
otherwise表示剩下的情况
注意点:1.不能使用html注释,要使用jsp注释
2.when标签的父标签一定是choose标签
--%>
<c:choose>
<c:when test="${requestScope.height>190}">
<h1>巨人</h1>
</c:when>
<c:when test="${requestScope.height>180}">
<h1>很高</h1>
</c:when>
<c:when test="${requestScope.height>170}">
<h1>有点高</h1>
</c:when>
<c:otherwise>
<h1>剩下的小于170</h1>
</c:otherwise>
</c:choose>
<%--
<c:foreach/>
遍历 1到10,输出
begin属性设置开始的索引
end属性设置结束的索引
var属性表示循环的变量
--%>
<table border="1">
<c:forEach begin="1" end="10" var="i">
<tr>
<td>${i}</td>
</tr>
</c:forEach>
</table>
<hr>
<%--遍历Object数组
for(Object :arr)
items表示遍历的数据源(遍历的集合)
var表示当前遍历到的数据
--%>
<%
request.setAttribute("arr",new String[]{"1323232","21312312","213213"});
%>
<c:forEach items="${requestScope.arr}" var="i">
${i}
</c:forEach>
<%
Map map=new HashMap();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
request.setAttribute("map",map);
%>
<c:forEach items="${requestScope.map}" var="entry">
${entry.key}
${entry.value}
</c:forEach><br/>
<%--遍历List集合---list中存放Student类,有属性:编号,用户名,密码,年龄,电话信息 --%>
<%
List list=new ArrayList();
for (int i = 1; i <=10 ; i++) {
list.add(new Student(i,"username"+i,"pssword"+i,18+i,"phone"+i));
}
request.setAttribute("stus",list);
%>
<c:forEach items="${requestScope.stus}" var="stu">
${stu}
</c:forEach>
</body>
</html>