JAVA Web学习(17)___11.6 定义和使用EL函数

2019-09-14  本文已影响0人  岁月静好浅笑安然

11.6 定义和使用EL函数

11.6.1 定义和使用函数

public class StringDeal {
    public static String shiftEnter(String  str){
        String newStr=str.replace("\r\n", "<br>");//替换回车换行符
        newStr=newStr.replaceAll("","&nbsp;");    //替换空格符号
        return newStr;
    }

}
<?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 web-jsptaglibrary_2_0.xsd"  
    version="2.0"> 
 <tlib-version>1.0</tlib-version>
 <jsp-version>1.2</jsp-version>
 <uri>/stringDeal</uri>
 <function>
 <name>shiftEnter</name>
 <function-class>com.hwp.StringDeal</function-class>
 <function-signature>java.lang.String  shiftEnter(java.lang.String)</function-signature>
 </function>
</taglib>

参数说明
<uri>/stringDeal</uri>用于指定tld文件的映射路径
<name>shiftEnter</name>用于指定EL函数所对应的方法名,通常与java文件名字一样
<function-class>com.hwp.StringDeal</function-class> 包名+类名 -
<function-signature>java.lang.String shiftEnter(java.lang.String)</function-signature> 用于指定EL函数所对应的静态方法,包含返回值类型和入口参数类型

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    
    <title>EL表达式</title>
    
  </head>
  
  <body>
   
     <form action="deal.jsp" name="form1" method="post">
     <textarea rows="5" cols="30" name="content"></textarea>
     <br>
     <input type="submit" name="Button" value="提交">
     </form>
  </body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/stringDeal" prefix="wghfn" %>
<% request.setCharacterEncoding("utf-8"); %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  
    
    <title>My JSP 'deal.jsp' starting page</title>
    
    

  </head>
  
  <body>
   内容为:<br>
   ${wghfn:shiftEnter(param.content)}
  </body>
</html>

上一篇下一篇

猜你喜欢

热点阅读