EI表达式

1.JavaBean 必须要有一个公共的无参构造器

2. 在表单回显

  "<%=request.getParameter("username")==null?"":request.getParameter("username") %>" =value=“$param.username{}” 明显简化JSP开发

3. EL提供. 和[] 两种运算符来提取数据,

[] 是为了区别 对象中属性带有特需字符 .

session.setAtrribute("com.http.mvp","value") 点的话无法进行区分

  2).EL 能够进行自动类型转换,前面score=100,后者Score=8911

 3) EL的隐含对象

   1.与输入有关隐含对象,两个 param 和paramValues 对象

      paramValues接收多个参数  不需要强制转换一直.下去就行

  2.其他隐含对象 ---cookie head 和pageContext最有用pageContext

   initParam获取当前WEb初始化参数,但是web.xml进行<context-param>的设定   

  3.EL中pageContext获取其他页面和本页面上的属性


一. 简单的标签:共同特性的tag库应用于不同的项目中,体现了软件复用的思想

  1.建立一个标签处理器类servlet实现SimpleTag接口

  2.建立一个.tld 文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglib       rary_2_0.xsd"
 6     version="2.0">
 7     
 8      <description>JSTL 1.1 core library</description>
 9   <display-name>MyTag core</display-name>
10   <tlib-version>1.1</tlib-version>
11   
12   <!-- 建议JSP页面上使用的名称 -->
13   <short-name>atGuiGu</short-name>
14   
15   <!-- TLD 文件的ID唯一标识当前TLD文件,多个TLD URI不能重复 -->
16   <uri>http:// www.atguigu.com/myTag/core</uri>
17 </taglib>

3.进行描述标签操作

4. 在JSP页面上进行导入 完成自定义标签的使用

 

2. 标签的属性: 属性必须要在.tld 文件中设定:只有先设定才能够获取

  如果是简单的标签的直接可以实现 SimpleTagSupport 就行

1 <attribute>
<!--z注意必须要与 标签处理器类中 setter方法定义属性相同--> 2 <name>value</name> 3 <required>true</required> 4 <!-- 当前属性是否接受动态值 --> 5 <rtexprvalue>true</rtexprvalue> 6 </attribute>

3.JSP中设定带属性:

<atGuiGu:hello value="${param.name }" count="10"/>

之下代码 先在TLD文件设定属性,同时标签处理器类 设定setter和getter方法

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
 6     version="2.0">
 7     
 8      <description>JSTL 1.1 core library</description>
 9   <display-name>MyTag core</display-name>
10   <tlib-version>1.1</tlib-version>
11   
12   <!-- 建议JSP页面上使用的名称 -->
13   <short-name>atGuiGu</short-name>
14   
15   <!-- TLD 文件的ID唯一标识当前TLD文件,多个TLD URI不能重复 -->
16   <uri>http:// www.atguigu.com/myTag/core</uri>
17   
18  <!-- 描述TLD文件 -->
19 
20 <tag>
21      <!-- 标签名 -->
22      <name>hello</name>
23      <!-- 标签 所在的全类名 -->
24     <tag-class> com.atug.taglib.HelloSimpTag</tag-class>
25     <!-- 标签体全类型 -->
26     <body-content>empty</body-content>
27     
28     <!-- 描述当前的属性 -->
29     <attribute>
30 <name>value</name> 31 <required>true</required> 32 <!-- 当前属性是否接受动态值 --> 33 <rtexprvalue>true</rtexprvalue> 34 </attribute> 35 <attribute> 36 <name>count</name> 37 <required>false</required> 38 <!-- 当前属性是否接受动态值 --> 39 <rtexprvalue>false</rtexprvalue> 40 </attribute> 41 </tag> 42 43 </taglib>
 1 public class HelloSimpTag implements SimpleTag {
 2  private String count;
 3  private String value;
 4     public String getCount() {
 5     return count;
 6 }
 7 
 8 public void setCount(String name) {
 9     this.count = name;
10 }
11 
12 public String getValue() {
13     return value;
14 }
15 
16 public void setValue(String value) {
17     this.value = value;
18 }
19 
20 public PageContext getPageContext() {
21     return pageContext;
22 }
23 
24 public void setPageContext(PageContext pageContext) {
25     this.pageContext = pageContext;
26 }
27 
28     @Override
29     public void doTag() throws JspException, IOException {
30         // TODO Auto-generated method stub
31             System.out.println(value +" "+count);
32             
33      HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
34     pageContext.getOut().print("hello"+request.getAttribute("name"));
35     
36     }
37 
38     @Override
39     public JspTag getParent() {
40         // TODO Auto-generated method stub
41         return null;
42     }
43 
44     @Override
45     public void setJspBody(JspFragment arg0) {
46         // TODO Auto-generated method stub
47 
48     }
49     private PageContext pageContext=null;
50     // JSp引擎调用 得到其他的8个JSP的内置对象
51     @Override
52     public void setJspContext(JspContext arg0) {
53         // TODO Auto-generated method stub
54      this.pageContext=(PageContext) arg0;
55     }
56 
57     @Override
58     public void setParent(JspTag arg0) {
59         // TODO Auto-generated method stub
60 
61     }
62 
63 }
View Code

明显在doTage方法获取到来有JSP页面设定的属性

 

 开发父标签  子标签将父标签的属性在JSP页面显示   注意父标签必须是 JSP Tag接口  getJspContext()方法相当于PageContext ,让JSP页面使用某个对象必须用Tomcat内置的JspContext.setAttribute()进行对象设定

  JspContext是继承SimpleTagSupport类

   1)父标签无法获取子标签的引用

   2)子标签通过getParent() 方法获取父标签(继承SimpleTag)

    1.ParentTag parentTag=(ParentTag)getParent();

           2.获取父标签属性 String name=parentTag.getName();


7.开发一个标签能够实现  

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

1 <c:choose>
2           <c:when test="${param.age > 24 }">大学毕业</c:when>
3            <c:when test="${param.age > 20 }">高中毕业</c:when>
4            <c:otherwise>高中之下...</c:otherwise>
5     </c:choose>
6     

当然下C标签下有这样的一个标签,但是编写属于自己的choose,when,otherwise功能:

   步骤:

       --> when 标签有一个boolean 类型属性test

      --> choose 是when 和otherwise的父标签,when在otherwise之前使用

---> 在父标签choose中定义全局变量 Boolean 的flag ,用于判断子标签满足条件下是否执行,相当于swithch break一样

当执行完成之后不往下执行

        当 when d的test是true并且when的父标签flag也是true 执行when标签

       when test=true,choose flag=false  不执行标签体

      when test=false,choose flag=true,执行otherwise标签

1.when 标签: getJspBody()。invoke(null): 将标签中内容输出到JSP页面上

 1 package com.atug.taglib;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.jsp.JspException;
 6 import javax.servlet.jsp.tagext.SimpleTagSupport;
 7 
 8 public class ChooseTag extends SimpleTagSupport {
 9   private boolean flag=true;
10 
11 public boolean isFlag() {
12     return flag;
13 }
14 
15 public void setFlag(boolean flag) {
16     this.flag = flag;
17 }
18  
19 // 父标签之言子标签自己之下就ok
20 @Override
21 public void doTag() throws JspException, IOException
22 {
23     getJspBody().invoke(null);
24 }
25   
26 }
View Code
 1 package com.atug.taglib;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.jsp.JspException;
 6 import javax.servlet.jsp.tagext.SimpleTagSupport;
 7 //开发简单标签 用simpleTagSupport
 8 public class WhenTag  extends SimpleTagSupport{
 9     
10     private boolean test;
11 
12     public boolean isTest() {
13         return test;
14     }
15 
16     public void setTest(boolean test) {
17         this.test = test;
18     }
19     
20     @Override
21     public void doTag() throws JspException, IOException
22     {
23         if(test)
24         {// test 值右 param.age>24 算术判断
25             // 获取父标签的 flag 属性
26            ChooseTag chooseTag= (ChooseTag) getParent();
27            boolean flag=chooseTag.isFlag();
28            
29            if(flag)
30            {
31                // 那么将标签体的内容默认输出到浏览器显示
32                getJspBody().invoke(null);
33                //执行比较完成之后将本个when关闭
34                chooseTag.setFlag(false);
35            }
36         }
37     }
38 }
View Code
 1 package com.atug.taglib;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.jsp.JspException;
 6 import javax.servlet.jsp.tagext.SimpleTagSupport;
 7 
 8 public class OtherwiseTag extends SimpleTagSupport {
 9     @Override
10     public void doTag() throws JspException, IOException {
11     
12          ChooseTag chooseTag=(ChooseTag) getParent();
13          
14          if(chooseTag.isFlag())
15          {
16              getJspBody().invoke(null);
17          }
18     }
19 }
View Code

TLD.文件上配置:

 1 <tag>
 2      <name>choose</name>
 3      <tag-class>com.atug.taglib.ChooseTag</tag-class>
 4       <body-content>scriptless</body-content>
 5  </tag>
 6  
 7   <tag>
 8      <name>when</name>
 9      <tag-class>com.atug.taglib.WhenTag</tag-class>
10       <body-content>scriptless</body-content>
11       
12       <attribute>
13               <name>test</name>
14               <required>true</required>
15               <rtexprvalue>true</rtexprvalue>
16       </attribute>
17  </tag>
18   <tag>
19      <name>otherwise</name>
20      <tag-class>com.atug.taglib.OtherwiseTag</tag-class>
21       <body-content>scriptless</body-content>
22  </tag>
View Code
1 <atGuiGu:choose>
2      <atGuiGu:when test="${param.age > 24 }">大学毕业</atGuiGu:when>
3      <atGuiGu:when test="${param.age > 20 }">高中毕业</atGuiGu:when>
4      <atGuiGu:otherwise>高中之下</atGuiGu:otherwise>
5     </atGuiGu:choose>
6     
7     
View Code

8.EL的自定义函数:

 步骤:

  1.编写Java类的静态方法 必须public修饰静态方法

  2.tld文件中进行说明

1 <function>
2        <name>concat</name>
3        <function-class>com.atug.taglib.MyELFunction</function-class>
4        <function-signature>java.lang.String concat(java.lang.String,java.lang.String)</function-signature>
5  </function>

原文地址:https://www.cnblogs.com/woainifanfan/p/6689931.html