| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

7.4 関数を使用するELコード

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

ある状況において、関数を使用するELコード、EL関数のためのコード、タグライブラリ記述子にEL関数を設定するコードを書く

static メソッドを作成する

JSPから直接呼ばれる メソッドクラス(*.java)を作成する

  1. public class Section7_4_Util {
  2.  
  3. public static String encodeUrl(String url) {
  4. String result = "";
  5. try {
  6. result = URLEncoder.encode(url, "UTF-8");
  7. } catch (UnsupportedEncodingException e) {}
  8. return result;
  9. }
  10. public static String decodeUrl(String url) {
  11. String result = "";
  12. try {
  13. result = URLDecoder.decode(url, "UTF-8");
  14. } catch (UnsupportedEncodingException e) {}
  15. return result;
  16. }
  17. }
TLD(タグライブラリディスクリプタ)を作成する

web-jsptaglibrary_2_0.xsd

sec7_4.tld

  1. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  4. http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  5. version="2.0">
  6. <tlib-version>1.0</tlib-version>
  7. <short-name>sec74util</short-name>
  8. <function>
  9. <name>encurl</name>
  10. <function-class>function.Section7_4_Util</function-class>
  11. <function-signature>
  12. java.lang.String encodeUrl(java.lang.String)
  13. </function-signature>
  14. </function>
  15. <function>
  16. <name>decurl</name>
  17. <function-class>function.Section7_4_Util</function-class>
  18. <function-signature>
  19. java.lang.String decodeUrl(java.lang.String)
  20. </function-signature>
  21. </function>
  22. </taglib>
web.xml(配備記述子)にtaglib要素を追加
  1. <web-app>
  2. :
  3. <jsp-config>
  4. <taglib>
  5. <taglib-uri>
  6. http://function/sec74util
  7. </taglib-uri>
  8. <taglib-location>
  9. /WEB-INF/sec7_4.tld
  10. </taglib-location>
  11. </taglib>
  12. </jsp-config>
  13. :
  14. </web-app>
JSPから利用する
  1. <%@ page language="java" %>
  2. <%@ page pageEncoding="Shift_JIS" %>
  3. <%@ page contentType="text/html;charset=Shift_JIS"%>
  4. <%@ taglib prefix="utl" uri="http://function/sec74util"%>
  5.  
  6. <%
  7. request.setCharacterEncoding("Shift_JIS");
  8. %>
  9. <html>
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
  12. <script>
  13. </script>
  14. <title>Section7.4</title>
  15. </head>
  16. <body>
  17. <form name="formSec7_4_1" action="/scwcd/jsp/Section7_4.jsp" method="POST">
  18. <input type="text" name="url" size="40" value="${param.url}"/><input type="submit" value="encode"/>
  19. <span style="border: 1px groove gray;">${utl:encurl(param.url)}</span>
  20. </form>
  21. <form name="formSec7_4_2" action="/scwcd/jsp/Section7_4.jsp" method="POST">
  22. <input type="text" name="url2" size="40" value="${param.url2}"/><input type="submit" value="decode"/>
  23. <span style="border: 1px groove gray;">${utl:decurl(param.url2)}</span>
  24. </form>
  25. </body>
  26. </html>

SCWCD Exam Study Kit: Java Web Component Developer Certification (ペーパーバック)


{{include_html banner_html, "!J2EE"}}