EL表达式、标签

来源:互联网 发布:飞思卡尔k60单片机 编辑:程序博客网 时间:2024/05/18 06:22

一、EL表达式
    1 EL表达式获取前台传递过来的属性语法${}
    2 只能获取通过setAttribute设置的值,不能修改值
    3 如果想获取对象的属性则,直接通过点语法或者中括号的方
式来获取
如:
    ${user.name} 或者 ${user['name']}
   注意:
点语法或者中括号里面的字段必须与该对象的属性名一致


二、标签

aglib的引入:
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
prefix:前缀

1、set
默认作用域是pageContext,仅当前页面,主要用来设置值,用getAttribute取值,也可以用${}
如:
<c:set var="name" value="zhangsan1" ></c:set>
<c:set var="name" value="zhangsan2" scope="request"></c:set>
<c:set var="name" value="zhangsan3" scope="session"></c:set>
<c:set var="name" value="zhangsan4" scope="application"></c:set>

2、条件语句
如:
<c:if test="${age%2!=0}">

3、forEach
如:
<c:forEach items="${list}" var="item" varStatus="statu">
<span>${statu.index}</span>
<span>${item}</span>
<br/>
</c:forEach>
想要设置list,需要用java代码,然后用setAttribute设置

三、自定义标签
1.建一个类,实现接口BodyTag
如:
package myweb_05_;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.Tag;

public class MyTage implements BodyTag {

private PageContext pageContext;
private String content;
private int times = 1;

public int getTimes() {
return times;
}

public void setTimes(int times) {
this.times = times;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public PageContext getPageContext() {
return pageContext;
}

@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
return 0;
}

@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return 0;
}

@Override
public int doStartTag() throws JspException {

JspWriter out = pageContext.getOut();

try {
for (int i = 0; i < times; i++) {
out.write("<h1>" + content + "</h1>");
}
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return 0;
}

@Override
public Tag getParent() {
// TODO Auto-generated method stub
return null;
}

@Override
public void release() {
// TODO Auto-generated method stub

}

@Override
public void setPageContext(PageContext arg0) {
// TODO Auto-generated method stub
pageContext = arg0;

}

@Override
public void setParent(Tag arg0) {
// TODO Auto-generated method stub

}

@Override
public void doInitBody() throws JspException {
// TODO Auto-generated method stub

}

@Override
public void setBodyContent(BodyContent b) {
// TODO Auto-generated method stub

}

}

2.建立.tld文件和web.xml放在同一个目录下
如:
<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
   
  <description>我的新标签库</description>
  <display-name>MY core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>my</short-name>
  <uri>http://java.tag.com/my</uri>

  <tag>
    <description></description>
    <name>jereh</name>
    <tag-class>myweb_05_.MyTage</tag-class>
    <body-content>JSP</body-content>
   
    <attribute>
<name>content</name>
<required>true</required>
    </attribute>
   
    <attribute>
<name>times</name>
<required>false</required>
    </attribute>
  </tag>

</taglib>

3、使用
使用时引入:
如:
<%@ taglib uri="http://java.tag.com/my" prefix="m" %>
原创粉丝点击