el和jstl笔记

来源:互联网 发布:云计算培训有前途吗 编辑:程序博客网 时间:2024/05/17 05:17


el:
jsp的内置表达式语言,从jsp2.0开始.
用来替代<%=..%>
作用:
1.获取域中数据 ★
2.执行运算 ★
3.获取常见的web对象
4.调用java的方法
格式:
${el表达式}

获取域中数据:★

1.获取简单数据
${pageScope|requestScope|sessionScope|applicationScope.属性名}
以后经常使用:
${属性名}:依次从pageContext,request,session,application查找指定属性,若查找到返回值,结束该次查找
若查找不到,返回""
------------------------------------------------------------------------------------
例子: <%
request.setAttribute("rkey", "rvalue");
session.setAttribute("skey", "svalue");
session.setAttribute("rkey", "svalue");
application.setAttribute("akey", "avalue");
%>
获取request中的数据:<br>
老方式:<%=request.getAttribute("rkey") %><br/>
el方式:${requestScope.rkey }<br/>
<hr>

获取session中的数据:<br>
老方式:<%=session.getAttribute("skey") %><br/>
el方式:${sessionScope.skey }<br/>
<hr>

获取application中的数据:<br>
老方式:<%=application.getAttribute("akey") %><br/>
el方式:${applicationScope.akey }<br/>

<hr>
获取失败老方式:<%=application.getAttribute("aakey") %><br/>
获取失败el方式:${applicationScope.aakey }

<hr>
便捷获取:
${skey },${rkey },${aakey },${akey }
<hr>
${rkey }
----------------------------------------------------------------------------------------------
2.获取复杂数据
获取数组中的数据
${域中的名称[index]}
获取list中的数据
${域中的名称[index]}
获取map中的数据
${域中的名称.键名}
-----------------------------------------------------------------------------
<%
//往request域中放入数组
request.setAttribute("arr", new String[]{"aa","bb","cc"});

//往request域中放入list
List list=new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
request.setAttribute("list", list);

//往request域中放入map
Map m=new HashMap();
m.put("username","tom");
m.put("age",18);
request.setAttribute("map", m);
%>

获取域中的数组:<br>
老方式:<%=((String[])request.getAttribute("arr"))[1] %><br>
el方式:${arr[1] }<br>
<hr>

获取域中的list:<br>
老方式:<%=((List)request.getAttribute("list")).get(1) %><br>
el方式:${list[1] }<br>
list的长度:${list.size() }
<hr>

获取域中的map:<br>
老方式:<%=((Map)request.getAttribute("map")).get("age") %><br>
el方式:${map.age }<br>
<hr>
-----------------------------------------------------------------------------------------------------------








javabean导航
javabean:
java语言编写的一个可重用的组件,
狭义上来说就是我们编写的一个普通的java类 例如:User Person 
javabean规范:
1.必须是一个公共的具体的类  public class
2.提供私有的成员变量  private String id;//
3.提供公共访问字段的方法 get|set|is方法
public String getId(){..}
一旦有公共的方法之后,get|set|is之后的内容,将首字母小写,将这个东西称之为bean属性
id就是一个bean属性
4.提供一个无参的构造器
5.一般实现序列化接口  serializable

3.${域中javabean名称.bean属性}
---------------------------------------------------------------------
例子 el  获取对象中字段的值
<%
User u=new User();
u.setId("001");
u.setName("tom");
u.setPassword("123");

//将u放入域中
request.setAttribute("user", u);
%>
获取域中javabean的id值:<br>
老方式:<%=((User)request.getAttribute("user")).getId() %><br/>
el方式:${user.id }<!-- 相当于调用 getXxx() -->


<hr>
el获取那么:${user.name }<br>
错误演示:${user.username }
-------------------------------------------------------------------------------------


//////////////////
执行运算:
四则运算 关系(>..) 逻辑(&& ||)
注意:
+:只能进行加法运算,字符串形式数字可以进行加法运算.
empty:判断一个容器的长度是否为0(array set list map),还可以判断一个对象是否为空
${empty 域中的对象名称}
取反 not   例如    not empty 
${not empty 域中对象名称}
三元运算符   ${ 3>4?"yes":"no" }
--------------------------------------------------------------------------------------------------------
例子:
<%
request.setAttribute("i", 3);
request.setAttribute("j", 4);
request.setAttribute("q", "12");
request.setAttribute("k", "k");

List l=null;
request.setAttribute("list", l);

List ll=new ArrayList();
ll.add("22");
request.setAttribute("list_", ll);

User user=null;
request.setAttribute("bean", user);

User user_=new User();
request.setAttribute("bean_", user_);
%>

${i+j }<br/>
${i+q }<br/>
${q+q }<br/>
<%-- ${i+k }<br/> --%>
<hr>
域中list的长度是否为0:${empty list}<br/>
域中list_的长度是否为0:${empty list_ }<br/>

<hr>
域中的bean是否为空:${empty bean }<br/>
域中的bean_是否为空:${empty bean_ }<br/>

<hr>
${ 3>4?"yes":"no" }<br/>

${i==3 }
--------------------------------------------------------------------------------------






//////////////////
el的内置对象
11个

pageScope
requestScope
sessionScope
applicationScope

param
paramValues

header
haederValues

initParam

cookie★



cookie内置对象:
${cookie} 获取map{key=Cookie}
例如:创建cookie
Cookie c=new Cookie("username","tom");
通过${cookie}获取相当于
{username=new Cookie("username","tom")}
相当于map的key是cookie的键
map的value是当前cookie

若项获取名称username的cookie的value值(获取tom)
${cookie.username.value}--javabean导航
注意:
java中Cookie的api
getName():获取cookie的名称
getValue():获取cookie的value值
我们称name和value是cookie的bean属性

使用cookie内置对象:
${cookie.给cookie起名字.value}

例如:
获取jsession的值
${cookie.JSESSIONID.value}

pageContext:获取不是map集合,相当于jsp的pageContext内置对象
在jsp页面中获取项目名
${pageContext.request.contextPath}
例如:<a href="${pageContext.request.contextPath }/demo8.jsp">pagecontext内置对象获取项目名(掌握)</a>
-----------------------------------------------------------------------------------------------------
jstl:
jsp标准的标签库语言
apache的
用来替代java脚本
使用步骤:
1.导入jar包 (jstl.jar和standard.jar)
2.在页面上导入标签库
<%@taglib prefix="" uri=""%>

例如:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
jstl的分类:
core:核心类库 ★
fmt:格式化|国际化
xml:过时了
sql:过时了
函数库:很少使用
core:掌握
★c:if
★c:forEach



★c:if 判断
<c:if test="${el表达式}">满足的时候输出的内容</c:if>
例如:
<c:if test="${3>4 }">
3大于4
</c:if>
<c:if test="${3<=4 }">
3不大于4
</c:if>
//////////////


例子 比如可以给我们登陆成功页面 加上判断
<c:if test="${not empty name }">


${name}欢迎你!


</c:if>


<c:if test="${empty name }">


 
你尚未登陆请重新登陆<a href="/ServletDemo/login.jsp">点击登陆</a>


</c:if>
---------------------------------------------------------------------------------------------------------
















★c:forEach 循环
格式1:
<c:forEach begin="从那里开始" end="到那里结束" step="步长" var="给变量起个名字" varStatus="循环状态变量">
${i }--${vs.count }--${vs.current }<br>
</c:forEach>
 
varStatus:用来记录循环的状态
常用的属性:
count:记录次数 从1开始
current:当前遍历的内容
index: 索引 从0 开始
例如:
<c:forEach begin="1" end="20" step="2" var="i" varStatus="vs">
${i }--${vs.count }--${vs.current }<br>
</c:forEach>

格式2:
<c:forEach items="${el获取域中的容器}" var="n">
${n }
</c:forEach>

例如:
//遍历list
<c:forEach items="${list }" var="n">
${n }
</c:forEach>

//遍历map
<c:forEach items="${map }" var="en">
${en.key }-- ${en.value }<br/>
  </c:forEach>
-----------------------------------------------------------------------------------
例子:
<%
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("张三", "23"));
list.add(new Student("李四", "24"));
list.add(new Student("王五", "25"));
list.add(new Student("赵六", "26"));


request.setAttribute("list", list);
%>


<table height=300 width=400 align=center>
<tr bgcolor=green>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<c:forEach items="${list }" var="stu" varStatus="s">
<c:if test="${s.count%2==0 }">
<tr bgcolor=red>
</c:if>
<c:if test="${s.count%2!=0 }">
<tr bgcolor=yellow>
</c:if>


<td>${s.count }</td>
<td>${stu.name }</td>
<td>${stu.age }</td>
</tr>
</c:forEach>


</table>
-----------------------------------------------------------------------------------------------------------------
* c:url:
功能:
* 自动补项目路径
* 自动url重写
<a href='<c:url value="/foreach.jsp" />'>foreach.jsp</a>


---------------------------------------------------------------------------------------------------------------------------
  <%
  HttpServletRequest req = (HttpServletRequest)(pageContext.getRequest());
  String contextPath = req.getContextPath();
  session.setAttribute("aaa", "aaa");
  %>
  <%=contextPath %><br>
  ${pageContext.request.contextPath}
    <a href="${pageContext.request.contextPath}/foreach.jsp">foreach.jsp</a><br>
   
   
    <a href='<c:url value="/foreach.jsp" />'>foreach.jsp</a>




--------------------------------------------------------------------------------------------------------------------
* MVC模式 一种前辈们总结出来的开发模式
* M :Model 模
* 封装数据,处理业务逻辑
* V : View 视图
* 界面显示
* C :Controller 控制器
* 调度




* JavaWeb的三层架构
* org.westos.web.servlet:web层
*  org.westos.service:service层
*org.westos.dao:dao层
* org.westos.domain : JavaBean
*  org.westos.util:工具包
org.westos.test:测试包



原创粉丝点击