<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>不厭其煩 &#187; jsp</title>
	<atom:link href="http://reder.wordpress.com/tag/jsp/feed/" rel="self" type="application/rss+xml" />
	<link>http://reder.wordpress.com</link>
	<description>Reder's Blog, experimental version</description>
	<lastBuildDate>Mon, 26 Sep 2011 16:52:36 +0000</lastBuildDate>
	<language>zh-tw</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='reder.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>不厭其煩 &#187; jsp</title>
		<link>http://reder.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://reder.wordpress.com/osd.xml" title="不厭其煩" />
	<atom:link rel='hub' href='http://reder.wordpress.com/?pushpress=hub'/>
		<item>
		<title>在 JSTL 中取用 Java constants</title>
		<link>http://reder.wordpress.com/2008/07/31/using-java-constants-in-jstl/</link>
		<comments>http://reder.wordpress.com/2008/07/31/using-java-constants-in-jstl/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 07:09:53 +0000</pubDate>
		<dc:creator>Reder</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[jstl]]></category>

		<guid isPermaLink="false">http://reder.wordpress.com/?p=135</guid>
		<description><![CDATA[以下程式碼來自討論串中。 在 Java 中，我們會使用 public static final String A_CONST  = "const"; 的方式設定常數，但在 JSTL 中無法直接取用。以下是解決方法： 由於 JSTL 可以取得 Map 或 JavaBean 的值，所以我們可以利用這個特性，建立一個 Class 繼承 HashMap 。然後用 relection 將其中的 fields 放入 Map 中。 然後我們的 Constants Class 再繼承上面的 Class： 那麼在 JSP 頁面上我們就可以這樣取用： 在討論串中另有針對無法繼承的 Interface 或 third party class 的改進法，但基本上我想我個人應該用不到，所以就不整理了。<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=reder.wordpress.com&amp;blog=2209636&amp;post=135&amp;subd=reder&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong><em>以下程式碼來自<a href="http://forums.sun.com/thread.jspa?threadID=508847&amp;forumID=45" target="_blank">討論串</a>中。</em></strong></p>
<p>在 Java 中，我們會使用 <code>public static final String A_CONST  = "const";</code> 的方式設定常數，但在 JSTL 中無法直接取用。以下是解決方法：</p>
<ol>
<li> 由於 JSTL 可以取得 Map 或 JavaBean 的值，所以我們可以利用這個特性，建立一個 Class 繼承 HashMap 。然後用 relection 將其中的 fields 放入 Map 中。 <pre class="brush: java;">
package com.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

/**
 * Class to reveal java constants to JSTL Expression Language
 * Uses reflection to scan the declared fields of a Constants class
 * Adds these fields to the Map.
 * Map is unmodifiable after initialization.
 *
 */
public class JSTLConstants extends HashMap {
	private boolean initialised = false;

	public JSTLConstants() {
		Class c = this.getClass();
		Field[] fields = c.getDeclaredFields();
		for (int i = 0; i &lt; fields.length; i++) {

			Field field = fields[i];
			int modifier = field.getModifiers();
			if (Modifier.isFinal(modifier) &amp;&amp; !Modifier.isPrivate(modifier))
				try {
					this.put(field.getName(), field.get(this));
				}
				catch (IllegalAccessException e) {}
		}
		initialised = true;
	}

	public void clear() {
		if (!initialised)
			super.clear();
		else
			throw new UnsupportedOperationException(&quot;Cannot modify this map&quot;);
	}

	public Object put(Object key, Object value) {
		if (!initialised)
			return super.put(key, value);
		else
			throw new UnsupportedOperationException(&quot;Cannot modify this map&quot;);
	}

	public void putAll(Map m) {
		if (!initialised)
			super.putAll(m);
		else
			throw new UnsupportedOperationException(&quot;Cannot modify this map&quot;);
	}

	public Object remove(Object key) {
		if (!initialised)
			return super.remove(key);
		else
			throw new UnsupportedOperationException(&quot;Cannot modify this map&quot;);
	}
}
</pre></li>
<li>然後我們的 Constants Class 再繼承上面的 Class： <pre class="brush: java;">
package com.utils;

import java.util.*;
public class JSTLConstantsTest extends JSTLConstants {

	public static final String name = &quot;Evnafets&quot;;
	public static final int age = 28;
	private static final String bad = &quot;bad&quot;;
	public static final String programmingSkill = &quot;Awesome&quot;;
	public static final String[] daysOfTheWeek = { &quot;Sun&quot;, &quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;, &quot;Fri&quot;, &quot;Sat&quot;, &quot;Sun&quot; };
	public int temp = 100;

	public static void main(String[] args) {
		Map m = new JSTLConstantsTest();

		System.out.println(&quot;Tee constants are &quot;);
		for (Iterator it = m.entrySet().iterator(); it.hasNext();) {
			Map.Entry me = (Map.Entry) it.next();
			System.out.println(me.getKey() + &quot; = &quot; + me.getValue());
		}
	}
}
</pre></li>
<li>那麼在 JSP 頁面上我們就可以這樣取用： <pre class="brush: java;">
&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %&gt;
&lt;jsp:useBean id=&quot;Constants&quot; class=&quot;com.utils.JSTLConstantsTest&quot;/&gt;
&lt;c:out value=&quot;${Constants.name}&quot;/&gt;
&lt;c:out value=&quot;${Constants.age}&quot;/&gt;
&lt;c:forEach var=&quot;day&quot; items=&quot;${Constants.daysOfTheWeek}&quot;&gt;
  &lt;c:out value=&quot;${day}&quot;/&gt;
&lt;/c:forEach&gt;
</pre></li>
</ol>
<p>在討論串中另有針對無法繼承的 Interface 或 third party class 的改進法，但基本上我想我個人應該用不到，所以就不整理了。</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/reder.wordpress.com/135/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/reder.wordpress.com/135/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/reder.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/reder.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/reder.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/reder.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/reder.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/reder.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/reder.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/reder.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/reder.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/reder.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/reder.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/reder.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/reder.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/reder.wordpress.com/135/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=reder.wordpress.com&amp;blog=2209636&amp;post=135&amp;subd=reder&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://reder.wordpress.com/2008/07/31/using-java-constants-in-jstl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9e3acfe0ee9096e8b00561891d69fef6?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">Reder</media:title>
		</media:content>
	</item>
	</channel>
</rss>
