${applicationScope.user.name}
如:(e1.jsp)
<html> <head></head> <body style="font-size:30px;font-style:italic;"> <% User user = new User(); user.setName("tom"); user.setAge(22); request.setAttribute("user",user); User user2 = new User(); user2.setName("jetty"); user2.setAge(22); session.setAttribute("user",user2); %> name:<% //User user1 = (User)request.getAttribute("user1"); //out.println(user1.getName()); %> <br/> name:${sessionScope.user.name} </body> </html><span style="font-size:14px;"> </span>b,方式二
b2,[]里面允许出现从0开始的整数,用来访问数组中的某个元素。
如:(e2.jsp)
<html> <head></head> <body style="font-size:30px;font-style:italic;"> <% User user = new User(); user.setName("tom"); user.setAge(22); user.setInterest(new String[]{"snooker","football"}); request.setAttribute("user",user); %> name:${user['name']} <% request.setAttribute("propname","name"); %> name:${user[propname]} interest:${user.interest[0]} </body> </html>2)进行一些简单的计算
找不到值
如:(e3.jsp)
<html> <head></head> <body style="font-size:30px;font-style:italic;"> 算术运算<br/> ${1+1}<br/> ${"100" + "200"} 关系运算<br/> ${1>0}<br/> <% request.setAttribute("str1","abc"); %> ${"abc" == str1} <br/> 逻辑运算<br/> ${1 >0 && 2 > 1 } empty运算<br/> <% request.setAttribute("str2",""); %> 空字符串: ${empty str2 }<br/> <% List list = new ArrayList(); //list.add("abc"); request.setAttribute("list",list); %> 空的集合:${empty list}<br/> <% request.setAttribute("obj",null); %> 值为null:${empty obj}<br/> 找不到值:${empty abc} </body> </html>3)获取请求参数值
等价于request.getParameterValues("city");
如:(e4.jsp)
<html> <head></head> <body style="font-size:30px;font-style:italic;"> username:${param.username}<br/> city:${paramValues.city[0]} </body> </html>案例: e5.jsp
使用el表达式输出员工的所有信息。Employee(id,name,salary,age)
准备工作:实体类设计(省略),导入jstl.jar,standart.jar(这里下载)
<html> <head></head> <body style="font-size:30px;font-style:italic;"> id:${e.id}<br/> name:${e.name}<br/> salary:${e.salary + 1000}<br/> age:${e.age}<br/> </body> </html>
public class ActionServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String uri = request.getRequestURI(); String action = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf(".")); if(action.equals("/list")){ Employee e = new Employee(); e.setId(1); e.setName("tom"); e.setSalary(20000); e.setAge(22); request.setAttribute("e",e); request.getRequestDispatcher("e5.jsp") .forward(request, response); }else if(action.equals("/list2")){ List<Employee> employees = new ArrayList<Employee>(); for(int i=0;i<8;i++){ Employee e = new Employee(); e.setName("emp00" + (i + 1)); e.setAge(22); employees.add(e); } request.setAttribute("employees",employees); request.getRequestDispatcher("jstl03.jsp") .forward(request, response); } } }
<servlet> <servlet-name>ActionServlet</servlet-name> <servlet-class>web.ActionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ActionServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
(5)jstl标签的使用
1)jstl(java standard tag lib)是什么?MyEclipse 5.5.1 GA\eclipse\plugins\com.genuitec.eclipse.j2eedt.core_5.5.1\data\libraryset\JSTL1.1\libstandard.jar jstl.jar
step2,使用taglib指令引入标签。scope属性指定绑订的范围("page","requst","session","application")。
如:(jstl01.jsp)
<%@page pageEncoding="utf-8" contentType="text/html;charset=utf-8" %> <%@page import="bean.*" %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head></head> <body style="font-size:30px;font-style:italic;"> <% Employee e = new Employee(); e.setGender("m"); request.setAttribute("e",e); %> 性别:<c:if test="${e.gender == 'm'}" var="rs" scope="request">男</c:if> <c:if test="${!rs}">女</c:if> </body> </html>b,choose标签
test属性用法同if标签。
如:(jstl02.jsp)
<%@page pageEncoding="utf-8" contentType="text/html;charset=utf-8" %> <%@page import="bean.*" %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head></head> <body style="font-size:30px;font-style:italic;"> <% Employee e = new Employee(); e.setGender("x"); request.setAttribute("e",e); %> 性别:<c:choose> <c:when test="${e.gender == 'm'}">男</c:when> <c:when test="${e.gender =='f'}">女</c:when> <c:otherwise>保密</c:otherwise> </c:choose> </body> </html>c,forEach标签
getCount():返回当前是第几次迭代(从1开始)
如:(jstl03.jsp)
<%@page pageEncoding="utf-8" contentType="text/html;charset=utf-8" %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <style> .row1{ background-color:#fff8dc; } .row2{ background-color:yellow; } </style> </head> <body style="font-size:30px;font-style:italic;"> <table width="60%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>序号</td> <td>姓名</td> <td>年龄</td> <td>index</td> </tr> <c:forEach items="${employees}" var="e" varStatus="s"> <tr class="row${s.index % 2 + 1}"> <td>${s.count}</td> <td>${e.name}</td> <td>${e.age}</td> <td>${s.index}</td> </tr> </c:forEach> </table> </body> </html>(6)自定义标签
JSP:有标签体,并且,标签体的内容可以是java代码(只有复杂标签技术才支持)。
如果要使用jstl,不用再将jstl相关的jar文件拷贝到WEB-INF\lib下,因为javaee5.0已经包含了相应的jar文件。