EL函数;自定义函数、标签

一、el函数

*Sun公司提供el函数库
		<hr color="blue" size="2"/>
		<!-- 	el函数的应用 -->
		${fn:toLowerCase("wQAASDWDdawdwa") }
		${fn:toUpperCase("wQAASDWDdawdwa") }
		${fn:length("wdawdaw") }
		${fn:split("www.it315.org",".") }
		${fn:join(arr,".") }

二、自定义函数

*需要在webroot->WEB-INF->新建tld文件,注意两个版本要一致2.0
*配置里tld文件,首先在声明xsi:schemaLocation位置,把前面的一段复制到后面web前面。
然后配置函数:注意函数名称name和signature和静态的java函数三个一致,引入标签库,要重启服务器
	 <short-name>mydefine</short-name>
	 <uri>http://www.ayit.cn/12</uri>
	 <function>
		<name>MyPrint</name>
		<function-class>com.ayit.fn.FnTest</function-class>
		<function--signature>java.lang.String MyPrint(java.lang.String)</function--signature>
	 </function>

三、自定义标签

*一般使用simpleTag接口完成自定义的标签
	-缺点:需要实现所有方法

* 开发步骤
	-第一步操作:定义一个java方法实现功能(但是这个方法必须是静态static方法)

	-第二步操作:创建一个函数库(也就是一个tld文件,但是这个tld文件需要创建在WEB-INF目录下)

*使用SimpleTagSupport类实现自定义标签的开发
		- 开发自定义标签时候,实现接口,或者继承类,下面这些方法会按照一定的顺序自动执行
			-- setJspContext(JspContext pc)
			- setParent(JspTag parent) 
			- setJspBody(JspFragment jspBody) 
			-- doTag() :执行标签

* 练习一:页面输出 hello
	-在jsp里面使用<tagdef:print/>,执行会在页面上显示内容 hello

	-开发步骤
		--第一步操作:创建一个类,继承SimpleTagSupport类
			public class tagdef extends SimpleTagSupport{

				PageContext pageContext =null;
				
				public void doTag() throws JspException, IOException {
					pageContext.getOut().print("hello");
				}
				public void setJspContext(JspContext pc) {
					this.pageContext = (PageContext) pc;
				}
			}
		--第二步操作:创建一个自定义标签库(也就是tld文件,但是这个文件创建在WEB-INF目录下)
			 <tag>
				<name>print</name>
				<tag-class>com.ayit.tagdef.tagdef</tag-class>
				<body-content>empty</body-content>
			 </tag>
		--第三步操作:在jsp里面使用标签,要引入自定义的标签库
		--第四步操作:需要重新启动服务器

	-jsp
		<tagdef:print/>

* 练习二:输出标签体
	- 配置
			 <tag>
				<name>print1</name>
				<tag-class>com.ayit.tagdef.tagdef1</tag-class>
				<body-content>scriptless</body-content>
			 </tag>
	
	-tagdef1类
				public class tagdef1 extends SimpleTagSupport {
		
				PageContext pageContext = null;
				public void doTag() throws JspException, IOException {
					JspFragment jspBody = getJspBody();
					jspBody.invoke(pageContext.getOut());
				}	
				public void setJspContext(JspContext pc) {
					this.pageContext = (PageContext) pc;
				}
				}
	-jsp
		<tagdef:print1>
			ABCD
		</tagdef:print1>


* 练习三:标签后面的所有的内容都不执行
	- 配置
		 <tag>
			<name>print2</name>
			<tag-class>com.ayit.tagdef.tagdef2</tag-class>
			<body-content>empty</body-content>
		 </tag>

	-tagdef2类
				public class tagdef2 extends SimpleTagSupport {

					public void doTag() throws JspException, IOException {
							throw new SkipPageException();
					}
				}
	-jsp
		 <tagdef:print2/>

* 练习四:实现if标签
	- 配置
	<tag>
		<name>if1</name>
		<tag-class>cn.itcast.myc.TestC4</tag-class>
		<body-content>scriptless</body-content>
		<attribute>
			<name>test</name>
			<required>true</required>
			<type>boolean</type>
		</attribute>
	 </tag>
	-tagdef3类

	-jsp
		<tagdef:print3 test="true">
			true
		</tagdef:print3>

四、jsp的开发模式

* 有两种
	- 模型一:使用的技术jsp+javabean,这种开发模式缺点是在jsp里面写大量java代码,java代码也要和html混合使用,
	不利用程序的维护。

	- 模型二(也叫做MVC): 使用的技术是jsp+servlet+javabean,每种技术只负责自己应该做的事情,

		--jsp负责显示数据
		--javabean负责封装和处理数据
		--servlet负责管理操作,什么数据显示到什么页面

* 什么是MVC:
	- m: 模型,使用的技术是javabean,负责封装数据
	- v:视图,使用的技术是jsp,负责显示数据
	- c:控制器,使用的技术是servlet,负责管理操作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
11.函数函数说明 使用举例 12.fn:contains 判定字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchString)}"> 13.fn:containsIgnoreCase 判定字符串是否包含另外一个字符串(大小写无关) <c:if test="${fn:containsIgnoreCase(name, searchString)}"> 14.fn:endsWith 判定字符串是否以另外字符串结束 <c:if test="${fn:endsWith(filename, ".txt")}"> 15.fn:escapeXml 把一些字符转成XML表示,例如 <字符应该转为< ${fn:escapeXml(param:info)} 16.fn:indexOf 子字符串在母字符串中出现的位置 ${fn:indexOf(name, "-")} 17.fn:join 将数组中的数据联合成一个新字符串,并使用指定字符格开 ${fn:join(array, ";")} 18.fn:length 获取字符串的长度,或者数组的大小 ${fn:length(shoppingCart.products)} 19.fn:replace 替换字符串中指定的字符 ${fn:replace(text, "-", "?")} 20.fn:split 把字符串按照指定字符切分 ${fn:split(customerNames, ";")} 21.fn:startsWith 判定字符串是否以某个子串开始 <c:if test="${fn:startsWith(product.id, "100-")}"> 22.fn:substring 获取子串 ${fn:substring(zip, 6, -1)} 23.fn:substringAfter 获取从某个字符所在位置开始的子串 24.${fn:substringAfter(zip, "-")} 25.fn:substringBefore 获取从开始到某个字符所在位置的子串 ${fn:substringBefore(zip, "-")} 26.fn:toLowerCase 转为小写 ${fn.toLowerCase(product.name)} 27.fn:toUpperCase 转为大写字符 ${fn.UpperCase(product.name)} 28.fn:trim 去除字符串前后的空格 ${fn.trim(name)}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值