「7.4 関数を使用するELコード」の版間の差分
ナビゲーションに移動
検索に移動
(同じ利用者による、間の1版が非表示) | |||
2行目: | 2行目: | ||
=====static メソッドを作成する===== | =====static メソッドを作成する===== | ||
− | + | [[JSP]]から直接呼ばれる メソッドクラス(*.java)を作成する | |
public class Section7_4_Util { | public class Section7_4_Util { | ||
8行目: | 8行目: | ||
String result = ""; | String result = ""; | ||
try { | try { | ||
− | result = | + | result = U[[R]]LEncoder.encode(url, "UTF-8"); |
} catch (UnsupportedEncodingException e) {} | } catch (UnsupportedEncodingException e) {} | ||
return result; | return result; | ||
16行目: | 16行目: | ||
String result = ""; | String result = ""; | ||
try { | try { | ||
− | result = | + | result = U[[R]]LDecoder.decode(url, "UTF-8"); |
} catch (UnsupportedEncodingException e) {} | } catch (UnsupportedEncodingException e) {} | ||
return result; | return result; | ||
23行目: | 23行目: | ||
=====TLD(タグライブラリディスクリプタ)を作成する===== | =====TLD(タグライブラリディスクリプタ)を作成する===== | ||
− | + | [http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd web-jsptaglibrary_2_0.xsd] | |
sec7_4.tld | sec7_4.tld | ||
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" | <taglib xmlns="http://java.sun.com/xml/ns/j2ee" | ||
− | xmlns:xsi="http://www.w3.org/2001/ | + | xmlns:xsi="http://www.w3.org/2001/[[XML]]Schema-instance" |
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee | xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee | ||
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" | http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" | ||
66行目: | 66行目: | ||
</web-app> | </web-app> | ||
− | ===== | + | =====[[JSP]]から利用する===== |
<%@ page language="java" %> | <%@ page language="java" %> | ||
<%@ page pageEncoding="Shift_JIS" %> | <%@ page pageEncoding="Shift_JIS" %> | ||
96行目: | 96行目: | ||
---- | ---- | ||
{{amazon|1932394389}} | {{amazon|1932394389}} | ||
− | + | [http://www.amazon.co.jp/dp/1932394389?tag=typea09-22&link_code=as3&creativeASIN=1932394389&creative=3999&camp=767 SCWCD Exam Study Kit: Java Web Component Developer Certification (ペーパーバック)] | |
---- | ---- | ||
− | {{include_html banner_html, "!J2EE"}} | + | {{include_html [[banner_html]], "!J2EE"}} |
2020年2月16日 (日) 04:21時点における最新版
目次
ある状況において、関数を使用する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 矢木浩人