EL表达式和标签

来源:互联网 发布:vgn p17h装linux 编辑:程序博客网 时间:2024/05/17 17:59

n       EL:表达式语言,在支持servlet2.4/JSP2.0或以上版本的容器中有效。

 

n       语法: ${ expression }

 

n       例如:

 

n       ${ sessionScope.user.name}

 

 

n       User user=(User)session.getAttribute(user);

n       String name=user.getName();

n       EL:表达式语言,在支持servlet2.4/JSP2.0或以上版本的容器中有效。

 

n       语法: ${ expression }

 

n       例如:

 

n       ${ sessionScope.user.name}

 

 

n       User user=(User)session.getAttribute(user);

n       String name=user.getName();

n       EL存取变量数据的方法

n       ${ 变量 }

n       存取某一范围的变量值,没有制定范围默认为page假如找不到依次为request,session,application;

n       假如全部范围都找不到,就返回null

n       ${username}     ${requestScope.username}

 

n       EL中的四大隐含对象

n       PageScope

n       RequestScope

n       SessionScope

n       ApplicationScope

n       pageContext

n       表示此JSPPageContext

n       pageScope

n       取得page范围的属性名称所对应的值

n       requestScope

n       取得request范围的属性名称所对应的值

n       sessionScope

n       取得session范围的属性名称所对应的值

n       applicationScope

n       取得application范围的属性名称所对应的值

n       param

n       如同request.getParameter(String name),返回String

n       paramValues

n       如同reqeust.getParameterValues(String name);返回String[]

JSTL --Java标准标签函数库:

n       JSTL是一个标准的已经制定好的标签库,可以用到各种领域,:基本输入输出、流程控制、循环、XML文件解析、数据库查询及国际化应用等。

 

n       JSTL中提供的标签主要分为5

n       核心标签库

n       I18N标签库

n       SQL标签库

n       XML标签库

n       函数标签库

JSTL

前置名称

URI

范例

核心标签库

c

http://java.sun.com/jsp/jstl/core

<c:out>

I18N格式标签库

fmt

http://java.sun.com/jsp/jstl/fmt

<fmt:formatDate>

SQL标签库

sql

http://java.sun.com/jsp/jstl/sql

<sql:query>

XML标签库

xml

http://java.sun.com/jsp/jstl/xml

<xml:forBach>

函数标签库

fn

http://java.sun.com/jsp/jstl/functions

<fn:split>

n       使用比较

n       Jsp :   <%= userList.getUser().getPhoneNumber()%>

n       Jstl :  

    <c:out   value=<%= userList.getUser().getPhoneNumber()%>/>

n       Jstl + el  :

      <c:out value=${userList.user.phoneName}/>

JSTL 的使用

n       JSTL必须在支持Servlet2.4Jsp2.0以上版本的容器上才行。

 

n       JSTL是由Apache组织的JakartaProject所实现,所以可以到Apache网站去下载,一般是.zip格式的。

n       jakarta-taglibs-standard-1.1.2.zip

JSTL –安装

n       www.apache.org下载jakarta-taglibs-standard-current.zip 

 

n       akarta-taglibs-standard-1.1.2/lib中的jstl.jarstandard.jar文件拷贝到WEB-INF/lib目录。

 

n       解压缩下载的文件,jakarta-taglibs-standard-1.1.2/tld目录全部拷贝到WEB-INF/目录下。

 

n       核心标签库:基本输入输出、流程控制、迭代操作和URL操作。

 

n       声明:

     <%@taglib prefix=c  uri=http://java.sun.com/jsp/jstl/core”%>

 

n       分为四大类:

n       表达式操作:out, set, remove,catch;

n       流程控制:if,choose,when, otherwise;

n       迭代操作:forEach,forTokens;

n       URL操作:import,url,redirect,param;

<c:set><c:out>

n       <c:set>主要用来将变量存储至jsp范围中或是javabean的属性中。

n       <c:set   var=“”  value=“”  scope=“” />

n       value中的值存储至scope指定的var变量名中

 

n       <c:set   var=“”  scope=“” />

n       内容体

n       </c:set>

n       将内容体中的信息存储至scope指定的var变量名中

 

n       <c:set   value=“” target=“bean”

                            property=propertyName/>

n       value中的值存储到target指定的bean中的property属性中

n       <c:set var=number value=${1+8} scope=session/>

 

n       <c:set var=“number” scope=“session”>

n       ${2+3}

n       </c:set>

 

n       <c:set target=User property=name  value=Tom/>

n       <c:set target=User property=name  value=${param.name}/>

 

n       切记:

n       var  scope这两个属性的值不能使用表达式来表示

 

n       var=${name} scope=${ourScpe}

n       <c:out>主要是用来显示数据的内容,和<%= %>是一样的效果。

 

n       <c:out  value=“” [defalut=“”]   [escapeXml=true]/>

n       value:表示需要显示出来的值

n       default:假如value中的值为null,则显示defalut的值

n       escapeXml:是否转换非凡字符,如:<转换成&lt;

 

n       value属性是必须的

n       defaultescapeXml是可选属性

n       <c:out value=hello,JSTL!/>

 

n       <c:out  value=${3+5}/>

 

n       <c:out  value=${param.number}  [default=2006 ]/>

 

n       <c:out value=“<p>有非凡字符</p> />

n       <p>有非凡字符</p>

 

n       <c:out value=“<p>有非凡字符</p> escapeXml=false/>

n       有非凡字符

<c:remove>:

n       <c:remove>主要用来移出变量

 

n       <c:remove var=varName [scope={p/r/s/a}]/>

n       Scope 的默认值为page.

n       假如没有设定scope值,则从page/request/

  session/application范围依次查找,找到就删除,假如找不到就不做任何事情。

 

n       <c:remove var=“number”  scope=“session”/>

流程控制标签

n       <c:if>

 

 

n       <c:chose>

 

n       <c:when>

 

n       <c:otherwise>

 

n       <c:if>的用途和我们一般在程序中用的if一样

 

n       <c:if  test=testCondition    [var=“” scope=“”]/>

 

n       <c:if test=“${sessionScope.username==“admin”}”>

n                Admin你好!

n       </c:if>

n       <c:choose>表示选择,但要作为<c:when><c:otherwise>的父标签来使用。

 

n       <c:choose>

n       内容

n       </c:choose>

 

n       可选内容:

n       空白

n       1或多个<c:when>

n       0或多个<c:otherwise>

n       <c:when> 的用途和我们一般在程序中的when一样

n       <c:when test=“testCondition”>

n       内容

n       </c:when>

 

n       <c:otherwise>表示所有条件都不成立时执行

n       < c:otherwise>

n       内容

n       </ c:otherwise >

 

n       假如和<c:choose>一起用必须放在<c:choose></c:choose>中间,且必须放在<c:otherwise>前使用。

 

n       选择条件:      

n       <c:choose>

n       <c:when test=“${condition }”>

n       ….

n       </c:when>

n       <c:when test=“${condition }”>

n       ….

n       </c:when>

n       <c:otherwise>

n       ….

n       </c:otherwise>

n       </c:choose>

<c:forEach>

n       <c:forEach>

n       为循环控制,它可以将集合(Collection)中的成员循序浏览一遍

 

n       当条件符合时,就会持续反复执行< c:forEach>的本体内容

 

n       迭代循环:

n       <c:forEach  items=“”  var=      begin=“”

n                            end=“”     step=“”>

n          ……

n       </c:forEach>

 

n       参数说明:

n       items:被迭代的集合对象

n       var:用来存放现在指到的成员,String

n       begin:开始位置,int

n       end:结束位置,int, end 值必须大于begin

n       step:每次迭代的间隔数,int,必须大于0

总结:要使用JSTL标签的话首先要导入JSTL.JAR的价包,在我们自己的项目中利用工具可以添加上去了;使用<c></c>标签的话,假如没有c.tld文件,我们就需要找到相应的.c.tld文件COPYWEB-INF/lib/web.xml文件等级的目录下了;如c.tld 文件的内容如下:

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"

    version="2.0">

   

  <description>JSTL 1.1 core library</description>

  <display-name>JSTL core</display-name>

  <tlib-version>1.1</tlib-version>

  <short-name>c</short-name>

  <uri>http://java.sun.com/jsp/jstl/core</uri>

 

  <validator>

    <description>

        Provides core validation features for JSTL tags.

    </description>

    <validator-class>

        org.apache.taglibs.standard.tlv.JstlCoreTLV

    </validator-class>

  </validator>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

上面是省略掉的板块[不省略的话大概有几千行的代码吧。。。。。。]

</taglib>

其实别害怕,我们用的时候指要知道一个uri属性就可以了…………………………………………………………..如上面的:

  <uri>http://java.sun.com/jsp/jstl/core</uri>

因为我们在<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"  %>页面中的这个属性上面就要用到:uri这个属性 prefix=”c”是指标签的前缀是C

OK,我们就可以在页面中使用JSTL标签了,现在我们只说<C></C>标签

下面了带点实质的:

参考一下了:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<jsp:directive.page import="Bean.Message"/>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"  %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'showmessage.jsp' starting page</title>

   

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

 

  </head>

  <script type="text/JavaScript">

  function change()

  {

 

  document.myform.submit();

  //document.myform.nowpage.value=a;

 

  }

 

  </script>

  <%

  //ArrayList<Message> list=(ArrayList)request.getAttribute("list");

 

 

 

   %>

 

  <body bgcolor="#8080c0">

  <form name="myform" action="ShowMessageListServlet" method="post" >

   <table align="center" bgcolor="#8080c0" border="1">

   <tr>

   <td><font color="blue" size=5>学员编号</font></td>

      <td><font color="blue" size=5>学员姓名</font></td>

         <td><font color="blue" size=5>留言标题</font></td>

            <td><font color="blue" size=5>留言内容</font></td>

               <td><font color="blue" size=5>留言时间</font></td>

   </tr>

 

  <c:forEach items="${requestScope.list}" var="list">

   <tr>

   <td><c:out value="${list.id}"/></td>

   <td><c:out value="${list.username}"/></td>

   <td><c:out value="${list.title}"/></td>

   <td><c:out value="${list.message}"/></td>

   <td><c:out value="${list.sendDate}"/></td>

   </tr>

   </c:forEach>

  

  <tr>

  <td>总共${requestScope.messagecount}条留言</td>

   <td>当前第${requestScope.nowpage}<href="ShowMessageListServlet?nowpage=1 }">首页</a>/<href="ShowMessageListServlet?nowpage=${requestScope.pagecount}">尾页</a></td>

  <c:if test="${requestScope.nowpage>1}">

  <td><href="ShowMessageListServlet?nowpage=<%=Integer.parseInt(request.getAttribute("nowpage").toString())-1%>">上一页</a></td>

  </c:if>

  <c:if test="${requestScope.nowpage==1}">

   <td><a><font color="blue">已是首页</font></a></td>

  </c:if>

  <c:if test="${requestScope.nowpage<requestScope.pagecount}">

  <td><href="ShowMessageListServlet?nowpage=<%=Integer.parseInt(request.getAttribute("nowpage").toString())+1 %>">下一页</a></td>

  </c:if>

  <c:if test="${requestScope.nowpage==requestScope.pagecount}">

  <td><a><font color="blue">已是尾页</font></a></td>

  </c:if>

  

      <td>跳转到第

      <select name="nowpage" onChange="change()">

    

      <c:forEach begin="1" end="${requestScope.pagecount}" var="i">

 

        <c:choose>

           <c:when test="${requestScope.nowpage==i}">

              <option value="${i}" selected>${i}</option>

           </c:when>

           <c:when test="${requestScope.nowpage!=i}">

             <option value="${i}">${i}</option>

           </c:when>

        </c:choose>    

   

     </c:forEach>     

      </select></td>

  </tr>

   </table>

   </form>

  </body>

</html>

上面的是一个分页显示留言的主页面,大家有爱好的可以研读一下,基本上用到了<C></C>的很多的标签了……可以说是一个很经典的例子了,但还是EL表达式和JSTL的综合版了,上面也用到了EL表达式的一些知识;

总结:EL表达式是取值或得到值的表达式.......对于数据的操作可以采用EL表达式,而JSTL中的一些标签更多的是对页面流程的控制......所以二者结合起来就实现了取值和显示值的效果,以实现了ServletJSP页面显示的综合运用………..最终实现网站的控制和现实流程;

 

 好了,到今天为止,JSP课程的一小部分已讲完了....更多的问题和技术上的交流,请在本人的QQ458563488或者269957018留言就可以了….谢谢各位耐心的访问和阅读我博客上面的文章..希望和以技术交流为目的而来的访客成为笔友共同努力,一起进步;

0 0