📄 tag-mail.xtp
字号:
<s1 title="Mail Tag"><p>Body tags can grab JSP results and send them to an entirely differentlocation. It's easy to create a custom tag that sends part of a JSP toa survey account. The required attribute <var/to/> will specify themail address for the mail and <var/subject/> specifies the subject. Theenclosed JSP contains the body of the mail.</p><p>The contents of the tag can be any JSP code. You can still use arbitraryscriptlets writing to <var/out/>.</p><s2 title="Using the mail tag"><p>This example use of the mail tag writes a simple message with theresults of a form. The user and the vote are mailed to john.doe.</p><example title="mail.jsp"><%@ taglib prefix="ct" uri="/WEB-INF/tags.tld" %><h1>Testing Mail</h1><ct:mail to="john.doe@localhost" subject="survey response"><item> <user><%= request.getParameter("user") %></user> <vote><%= request.getParameter("vote") %></vote></item></ct:mail></example><results>Subject: survey Date: Mon, 11 Sep 2000 18:06:52 -0700 From: Resin Account <resin@localhost> To: john.doe@localhost<item> <user>Fred Smith</user> <vote>Gryffindor</vote></item></results></s2><s2 title="Implementation of the Mail Tag"><p>The MailTag has two required attributes, <var/subject/> and <var/to/>.As usual, they are set using the <var/bean/> design patterns<var/setSubject()/> and <var/setTo()/>.</p><p>Mailing uses Resin's VFS. The <var/mailto:/> scheme sends mailto any user account.</p><example title="test/MailTag.java">package test;import java.io.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;import com.caucho.vfs.*;public class MailTag extends BodyTagSupport { private String to; private String subject; public void setTo(String to) { this.to = to; } public void setSubject(String subject) { this.subject = subject; } public int doAfterBody() throws JspException { String body = getBodyContent().getString(); Path mailto = Vfs.lookup("mailto:" + to); try { WriteStream os = mailto.openWrite(); os.setAttribute("subject", subject); os.print(body); os.close(); } catch (IOException e) { throw new JspException(e.toString()); } return SKIP_BODY; }}</example></s2><s2 title="Attribute Configuration"><p>Each expected attribute needs an entry in the .tld. In this case,both <var/to/> and <var/subject/> are required.</p><example title="WEB-INF/tag.tld"><taglib> <tag> <name>mail</name> <tagclass>test.MailTag</tagclass> <attribute> <name>to</name> <required>true</required> </attribute> <attribute> <name>subject</name> <required>true</required> </attribute> </tag></taglib></example></s2><s2 title="Summary"><ul><li><var/BodyTags/> can capture arbitrary JSP output.<li>After the contents complete, JSP calls <var/doAfterBody/>.<li><var/getBodyContent()/> contains the JSP body.<li>VFS <var/mailto:/> sends mail.</ul></s2></s1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -