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(タグライブラリディスクリプタ)を作成する
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>
SCWCD Exam Study Kit: Java Web Component Developer Certification (ペーパーバック)
{{include_html banner_html, "!J2EE"}}
© 2006 矢木浩人