URLつきメールをメーラーで送信するためのカスタムタグ

変なタイトルになってしまった・・・。

「このページの情報を、他の人に見てもらいたい」という時に、開いているページのリンクを含んだメールを
簡単に送信するためのタグです。
開いているページのリンクを取得する部分でSeasarを使っていますが、もちろんSeasarを使わなくてもできます。


タグクラス

public class OpenMailerLinkTag extends BodyTagSupport
{
	/**
	 * タグの開始
	 */
	public int doStartTag() throws JspException
	{
		HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
		
		Map requestScope = (Map)SingletonS2Container.getComponent("requestScope");
		String requestUrl = (String)requestScope.get("javax.servlet.forward.request_uri");
		
		StringBuffer lsbTag = new StringBuffer();
		lsbTag.append("<a href=\"mailto:?body=");
		lsbTag.append("http://");
		lsbTag.append(request.getServerName()).append(":").append(request.getServerPort());
		lsbTag.append(requestUrl);
		lsbTag.append("\">");
		
		try
		{
			JspWriter out = pageContext.getOut();
			out.print(lsbTag.toString());
			
			return EVAL_BODY_INCLUDE;
		}
		catch(Exception e)
		{
			throw new JspException(e);
		}
	}
	
	/**
	 * タグの終了
	 */
	public int doEndTag() throws JspException
	{
		try
		{
			JspWriter out = pageContext.getOut();
			out.print("</a>");
		}
		catch(Exception e)
		{
			throw new JspException(e);
		}
		
		return EVAL_PAGE;
	}
}

tldファイルを作成します。
私はWEB-INF/tldフォルダーの中に置くようにしています。

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:xml="http://www.w3.org/XML/1998/namespace"
		xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
		version="2.1">
	
	<tlib-version>1.0</tlib-version>
	<uri>/okahirotag</uri>
	
	<tag>
		<name>openmailer</name>
		<tag-class>okahirop.OpenMailerLinkTag</tag-class>
		<body-content>JSP</body-content>
	</tag>
</taglib>

JSPに定義(SAStrutsではcommon.jspに追加)

<%@taglib prefix="okahirotag" uri="/okahirotag" %>


実際に使う箇所では下記のようにします。

<okahirotag:openmailer>メール送信</okahirotag:openmailer>


非常に単純なサンプルです。
Strutsのhtml系タグみたいにstyleIdとかあるといいかもしれません。