トップ 一覧 ping 検索 ヘルプ RSS ログイン

7.4 関数を使用するELコードの変更点

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
!!!ある状況において、関数を使用するELコード、EL関数のためのコード、タグライブラリ記述子にEL関数を設定するコードを書く

::static メソッドを作成する
JSPから直接呼ばれる メソッドクラス(*.java)を作成する
 public class Section7_4_Util {
 
   public static String encodeUrl(String url) {
     String result = "";
     try {
       result = URLEncoder.encode(url, "UTF-8");
     } catch (UnsupportedEncodingException e) {}
     return result;
   }
   
   public static String decodeUrl(String url) {
     String result = "";
     try {
       result = URLDecoder.decode(url, "UTF-8");
     } catch (UnsupportedEncodingException e) {}
     return result;
   }
 }

::TLD(タグライブラリディスクリプタ)を作成する
[web-jsptaglibrary_2_0.xsd|http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd]

sec7_4.tld
 <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">
   <tlib-version>1.0</tlib-version>
   <short-name>sec74util</short-name> 
   <function>
     <name>encurl</name>
     <function-class>function.Section7_4_Util</function-class>
     <function-signature>
       java.lang.String encodeUrl(java.lang.String)
     </function-signature>
   </function>     
   <function>
     <name>decurl</name>
     <function-class>function.Section7_4_Util</function-class>
     <function-signature>
       java.lang.String decodeUrl(java.lang.String)
     </function-signature>
   </function>     
 </taglib>

::web.xml(配備記述子)にtaglib要素を追加

 <web-app>
      :
   <jsp-config>
     <taglib>
       <taglib-uri>
         http://function/sec74util
       </taglib-uri>
       <taglib-location>
         /WEB-INF/sec7_4.tld
       </taglib-location>
     </taglib>
   </jsp-config>
      :
  </web-app>

::JSPから利用する
 <%@ page language="java" %>
 <%@ page pageEncoding="Shift_JIS" %>
 <%@ page contentType="text/html;charset=Shift_JIS"%>
 <%@ taglib prefix="utl" uri="http://function/sec74util"%>
 
 <%
     request.setCharacterEncoding("Shift_JIS");
 %>
 <html>
 <head>
     <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
     <script>
     </script>
     <title>Section7.4</title>
 </head>
 <body>
     <form name="formSec7_4_1" action="/scwcd/jsp/Section7_4.jsp" method="POST">    
         <input type="text" name="url" size="40" value="${param.url}"/><input type="submit" value="encode"/>
         <span style="border: 1px groove gray;">${utl:encurl(param.url)}</span>
     </form>
     
     <form name="formSec7_4_2" action="/scwcd/jsp/Section7_4.jsp" method="POST">    
         <input type="text" name="url2" size="40" value="${param.url2}"/><input type="submit" value="decode"/>
         <span style="border: 1px groove gray;">${utl:decurl(param.url2)}</span>
     </form>
 </body>
 </html>
----
{{amazon 1932394389}}
[SCWCD Exam Study Kit: Java Web Component Developer Certification (ペーパーバック)|http://www.amazon.co.jp/dp/1932394389?tag=typea09-22&link_code=as3&creativeASIN=1932394389&creative=3999&camp=767]

----
{{include_html banner_html, "!J2EE"}}