//普通字符串
request.setAttribute("hello", "hello World");
//结构
Group group=new Group();
group.setId(1);
group.setName("尚学堂");
User user=new User();
user.setName("谷超");
user.setAge(27);
user.setGroup(group);
request.setAttribute("user", user);
//map
Map map=new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
request.setAttribute("map", map);
//数组
String[] strArr=new String[]{"hello","world","ni","hao"};
request.setAttribute("strArr", strArr);
// 对象数组
User[] users=new User[10];
for(int i=0;i<10;i++){
User u=new User();
u.setName("User"+i);
users[i]=u;
}
request.setAttribute("users", users);
// list
ArrayList list=new ArrayList();
for(int i=0;i<10;i++){
User u=new User();
u.setName("User"+i);
list.add(u);
}
request.setAttribute("userList", list);
//empty
request.setAttribute("value1", null);
request.setAttribute("value2", "");
request.setAttribute("value3", new ArrayList());
request.setAttribute("value4", "11111");
JSP中代码
<li>普通字符串</li><br>
hello(jsp 脚本):<%=request.getAttribute("hello") %><br>
hello(el表达式):${hello }<br>
el表达式,el的隐含对象pageScope,requestScope,sessionScope,applicationScope<br>
hello(el表达式):${requestScope.hello }<br>
hello(el表达式,从sessionScope中取得):${sessionScope.hello }<br>
<li>结构</li><br>
姓名:${user.name }<br>
年龄:${user.age }<br>
所属组:${user.group.name}<br>
<li>map</li><br>
mapvalue.key1:${map.key1 }<br>
mapvalue.key2:${map.key2 }<br>
<li>数组,采用[]和下标</li><br>
strArr[2]:${strArr[2] }<br>
<li>对象数组,采用[]和下标</li><br>
userArr[2].name:${users[2].name }<br>
<li>list</li><br>
userList[5].name:${userList[5].name }<br>
<li>el表达式对运算符的支持</li><br>
1+2=${1+2 }<br>
10/5=${10/5 }<br>
10 div 5=${10 div 5 }<br>
10 % 3=${10 % 3 }<br>
10 mod 3=${10 mod 3 }<br>
4 > 3=${4> 3 }<br>
<!--
==/eg
!=/ne
</lt
>/gt
<=/le
>=/ge
&&/and
||/or
!/not
/ / div
%/mod
+
-
*
-->
<li>测试empty</li><br>
value1:${empty value1 }<br>
value2:${empty value2 }<br>
value3:${empty value3 }<br>
value4:${empty value4 }<br>
通常和jstl结合使用,表达式语言有三个重要部分
EL隐式对象 pageScope,requestScope,sessionScope,applicationScope
存取器
运算符
using functions <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> ${fn:contains(var1,'hello')}
本文详细介绍了如何在JSP中使用EL表达式来访问各种类型的数据,包括字符串、对象结构、Map、数组、列表等,并展示了如何进行运算及使用empty测试。
1594

被折叠的 条评论
为什么被折叠?



