JSP EL
核心作用
减少2JSP页面中的java代码
方便jsp中代码的修改,也方便美工修改页面
例如:${10+10}
<h2>Hello,${user.name}</h2>
运算符
类型 定义
算术型 +、-、*、/=div、%=mod
逻辑型 And=&&,or=|,!=not
关系运算型 ==、eq,!、ne,>、gt,、<=、le、>=、ge、<、lt、
条件型 A?b:c
空 empty
以上写代,后面的英文可以代替前面的符号
${12 <= 14 }<Br/>
${(3>r) ||(b>3)}
使用jspEl读取jababean属性
类型 示例 对应的调用方法
JavaBean ${user.username}
${user["username"]}
${user['username']} user.getUsername()
数组 ${sport[1]} sport[1]
List ${address[2]} address.get(2)
Map ${phone.home} phone.get('hone')
以上的方法都有三种写,我只写了前一个,
JSP EL的内置对象
pageContext\pageScope\requestScope\sessionScope\applicastionScope
param\paramValues\header\headerValues\cookice\initParam
这里的内置对象不是jsp中的内置对象,而是El的内置对象
但是他们是只可以互换的
如
<%=session.getAttribute("phone")%>
等价于
${sessionScope.phone};
设定jsp不使用JSP EL
1,在jsp页面中加page(只在当前页)
<%@ page isElgnored="true"%>
2,在整个web应用中
<jsp-conifg>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
在jsp中转义$符合
1,在前面加\--------\$
2,加单引号''--------'$'
出现的问题:
在jseEl中不法得到List,Map的值
定制标记库
概述
增加表现层的处理能力
减少jsp文件中的java代码
实例:运行时间
新建一个类(TimerTag)继承TagSupport
定义start,end属性
重写doStatrTag(),doEndTag()方法
private long start;
private long end;
@Override
public int doStartTag() throws JspException {、
//得到系统的时间
start=System.currentTimeMillis();
return EVAL_BODY_INCLUDE;
}
@Override
public int doEndTag() throws JspException {
end=System.currentTimeMillis()-start;
try {
JspWriter out=pageContext.getOut();
out.println("running time:"+end+"ms");
} catch (Exception e) {
throw new JspTagException(e);
}
return EVAL_PAGE;
}
在web-inf中创建tld文件
文件头在tomcat中的\webapps\examples\WEB-INF\jsp2得到
最终内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.
xmlns:xsi="http://www.
xsi:schemaLocation="http://java. http://java.
version="2.0">
<description>A tag library timeel</description>
<!--这里的版本自己可以随便取-->
<tlib-version>1.0</tlib-version>
<short-name>SimpleTagLibrary</short-name>
<!--这是将是要在jsp页面中要写的内容-->
<uri>/
<tag>
<description>outputs hell.oie</description>
<!--这是标签并没有-->
<name>timer</name>
<!--这里是自定标签的类-->
<tag-class>com.yds.entity.Timtag</tag-class>
<body-content>JSP</body-content>
</tag>
</taglib>
在jsp页面中要加一标签头
<%@ taglib prefix="随便写" uri="/
页面中要用的话就得写
<随便写:timer>要执行的内容</
注意:
上面的 <body-content>JSP</body-content>中的JSP的值
JSP:表示标记中间可以有java代码,或html代码
empty:表示中不能包含代码
scriptless:不能包java代码,可能和el代码
tagdependent:标记自己来设定
注意上面的大小写,不只有第一大写,
实例二:时间
private String parttern = "yyyy-MM-dd hh:mm:ss";
private Date date;
public void setParttern(String parttern) {
this.parttern = parttern;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public int doEndTag() throws JspException {
SimpleDateFormat sdf=new SimpleDateFormat(parttern);
if(date==null){
date=new Date();
}
try {
JspWriter out=pageContext.getOut();
out.print("当前时间是"+sdf.format(date));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EVAL_PAGE;
}
Tld中写
<description>outputs hell.oie</description>
<name>datetime</name>
<tag-class>com.yds.entity.DateTimeTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>parttern</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>this is a datetime</description>
</attribute>
Jsp页面中可以写
<util:datetime parttern="yyyy年-MM月-dd日" />
tagSuppert的返回值
doStartTag() 方法如果返回的是SKIP_BODY,说明这个文件不会执行了
直接去执行doEndTag()方法
返回EVAL_BODY_INCLUDE表示标包含内容记是要执行的
执行完了之后,如果重写了doAfterBodyTag()方法,就去执行该方法doAfterBodyTag()方法如果返回EVAL_BODY_AGAG重复回来在执行标记中所包含的 内容
返回SKIP_BODY则招待doEndTag方法
doEndTag()返回SKIP_PAGE则不执行JSP
返回EVAL_PAGE则执行JSP
BodyTagSupport的用法
上面的方法之后,些方法,可以在执行标签的时候,可以对标签里面的内容进行操作
实例,过滤标签
public int doAfterBody() throws JspException {
//得到Body里面的内容
BodyContent body=this.getBodyContent();
String str=body.getString();
// 对定制标记所包含的内容进行HTML代码过滤
String newContent = StringUtil.filterHtml(str);
try {
JspWriter out=body.getEnclosingWriter();
out.println(newContent);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SKIP_BODY;
}
标签转换类
public static boolean validateNull(String args) {
if (args == null || args.length() == 0) {
return true;
} else {
return false;
}
}
/**
* 判断输入的字符串参数是否为空或者是"null"字符,如果是,就返回target参数,如果不是,就返回source参数。
*/
public static String chanageNull(String source, String target) {
if (source == null || source.length() == 0 || source.equalsIgnoreCase("null")) {
return target;
} else {
return source;
}
}
/**
* 过滤<, >,\n 字符的方法。
* @param input 需要过滤的字符
* @return 完成过滤以后的字符串
*/
public static String filterHtml(String input) {
if (input == null) {
return null;
}
if (input.length() == 0) {
return input;
}
input = input.replaceAll("&", "&");
input = input.replaceAll("<", "<");
input = input.replaceAll(">", ">");
input = input.replaceAll(" ", " ");
input = input.replaceAll("'", "'");
input = input.replaceAll("\"", """);
return input.replaceAll("\n", "<br>");
}
标记库打包
1,把class文件复制到新建一个目录temp
2,把META-INF文件也放在这里
3,在把tld文件放到META-INF文件夹里同
4,cmd到temp文件下
Jar -cvf 名称.jar *