自定义JSTL标签库(函数库)

来源:互联网 发布:梦里花落知多少名句 编辑:程序博客网 时间:2024/04/29 16:53
这个使用起来比较简单,不需要配置web.xml或者其它配置文件,但是可能需要在pom.xml中引入spring配置


另外,还有一套标准的函数库,但是要引入
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn"  uri="http://java.sun.com/jsp/jstl/functions"%>
如果不引入<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>,好像会出错的,要特别注意
比如<c:set var="myString" value="hello peter"/>
${fn:contains(myString, "hello")}  


前台会显示true,如果不引入core,前台会false


js调用方法


<scripttype="text/javascript">

top.location.href='${fns:getProjectUrl()}/index.jsp';

</script>


还有一个问题要说,可能在编辑器里面http://java.sun.com/jsp/jstl/core是红的,我们也无法进入URL,但是没有问题的

-------------------------------------------------------------------
写peter.tld,
这里特别说明,<name>getTestString</name>一定要写成函数名,乱写是不能调用的; <short-name>peter</short-name>是写在:前面的,也要小心,这个文件要放在/WEB-INF/tlds/peter.tld
<?xml version="1.0" encoding="UTF-8" ?>


<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 functions library</description>
  <display-name>JSTL functions sys</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>peter</short-name>
  <uri>http://java.sun.com/jsp/jstl/functionss</uri> 
  <function>
    <description>test</description>
    <name>getTestString</name>
    <function-class>peter.web.demo.common.utils.FrontPageTest</function-class>
    <function-signature>java.lang.String getTestString(java.lang.String)</function-signature>
    <example>${peter:getAdminPath(str)}</example>
  </function> 
</taglib>


-----------------------------------------------------------------
完成jsp,这里要特别说明一下,
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="peter" uri="/WEB-INF/tlds/peter.tld" %> 
这个要加好



<%@ page contentType="text/html;charset=UTF-8" %>
<%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="peter" uri="/WEB-INF/tlds/peter.tld" %> 
<html>
<head>
<title>BrchMng管理</title>
<meta name="decorator" content="default"/> 
</head>
<body> 
<form id="inputForm"  action="${pageContext.request.contextPath}/login" method="post" class="form-horizontal">  
<label> 111111111</label> 
<label>${peter:getTestString("into hou")}</label>
<div class="form-actions">
<input id="btnSubmit" class="btn btn-primary" type="submit" value="登陆"/>
<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
</div>
</form> 
</body>
</html>
--------------------------------------------------------
完成对应的类
package peter.web.demo.common.utils;


/**
 * Created by Administrator on 2017-03-15.
 */
public class FrontPageTest {
    public static String getTestString(String inStr )
    {
        System.out.println("inStr = " + inStr);
        return "fffffffffffffff";
    }
}
这个类没有什么需要配置的,但是,需要public静态函数,要不然,不好调用
0 0