﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-闵毓-随笔分类-Spring in action</title><link>http://www.blogjava.net/shmily432685/category/4001.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 08 Oct 2007 04:10:08 GMT</lastBuildDate><pubDate>Mon, 08 Oct 2007 04:10:08 GMT</pubDate><ttl>60</ttl><item><title>使用Spring邮件抽象层发送简单邮件(通过自己编写，更新后发布)1</title><link>http://www.blogjava.net/shmily432685/archive/2005/12/30/26041.html</link><dc:creator>闵毓</dc:creator><author>闵毓</author><pubDate>Fri, 30 Dec 2005 04:49:00 GMT</pubDate><guid>http://www.blogjava.net/shmily432685/archive/2005/12/30/26041.html</guid><wfw:comment>http://www.blogjava.net/shmily432685/comments/26041.html</wfw:comment><comments>http://www.blogjava.net/shmily432685/archive/2005/12/30/26041.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/shmily432685/comments/commentRss/26041.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shmily432685/services/trackbacks/26041.html</trackback:ping><description><![CDATA[Spring提供了一个发送电子邮件的高级抽象层，它向用户屏蔽了底层邮件系统的一些细节，同时负责低层次的代表客户端的资源处理。Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender和 封装了简单邮件的属性如from, to,cc, subject, text的值对象叫做SimpleMailMessage。 <br />1、我们定义一个发送邮件的接口:OrderManager.java <br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">1</span> <span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">interface</span><span style="COLOR: #000000"> OrderManager </span><span style="COLOR: #0000ff">extends</span><span style="COLOR: #000000"> BaseManager{<br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">3</span> <span style="COLOR: #008000">*email,要发送的邮件地址;<br /></span><span style="COLOR: #008080">4</span> <span style="COLOR: #008000">*Code:激活码<br /></span><span style="COLOR: #008080">5</span> <span style="COLOR: #008000"></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">6</span> <span style="COLOR: #000000">     </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> placeOrder(String email);<br /></span><span style="COLOR: #008080">7</span> <span style="COLOR: #000000">}</span></div><p>2、我们需要对该接口进行实现的方法:OrderManagerImpl.java</p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> javax.mail.Message;<br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> javax.mail.MessagingException;<br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> javax.mail.internet.InternetAddress;<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> javax.mail.internet.MimeMessage;<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.springframework.mail.MailException;<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.springframework.mail.javamail.JavaMailSender;<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.springframework.mail.javamail.MimeMessagePreparator;<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> service.OrderManager;<br /></span><span style="COLOR: #008080"> 9</span>  <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> OrderManagerImpl </span><span style="COLOR: #0000ff">extends</span><span style="COLOR: #000000"> BaseManagerImpl </span><span style="COLOR: #0000ff">implements</span><span style="COLOR: #000000"> OrderManager {<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> JavaMailSender mailsender;<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> MyMailMessage message;<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setMessage(CityMailMessage message)<br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">    {<br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.message </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> message;<br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setMailsender(JavaMailSender mailsender) {<br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.mailsender </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> mailsender;<br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> placeOrder(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> String email</span><span style="COLOR: #000000">) {<br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">        <br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">        MimeMessagePreparator preparator </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> MimeMessagePreparator() {<br /></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">            </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> prepare(MimeMessage mimeMessage) </span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> MessagingException {<br /></span><span style="COLOR: #008080">29</span> <span style="COLOR: #000000">                mimeMessage.setRecipient(Message.RecipientType.TO, <br /></span><span style="COLOR: #008080">30</span> <span style="COLOR: #000000">                        </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> InternetAddress(email));<br /></span><span style="COLOR: #008080">31</span> <span style="COLOR: #000000">                mimeMessage.setFrom(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> InternetAddress(message.getFrom()));<br /></span><span style="COLOR: #008080">32</span> <span style="COLOR: #000000">                /**转换编码为GBK*/<br /></span><span style="COLOR: #008080">33</span> <span style="COLOR: #000000">                mimeMessage.setSubject(message.getSubject(),</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">GBK</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080">36</span> <span style="COLOR: #000000">                mimeMessage.setText(email</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;br&gt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">message.getSubject()</span><span style="COLOR: #000000">+message.getText()</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">GBK</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080">37</span> <span style="COLOR: #000000">                <br /></span><span style="COLOR: #008080">38</span> <span style="COLOR: #000000">            }<br /></span><span style="COLOR: #008080">39</span> <span style="COLOR: #000000">        };<br /></span><span style="COLOR: #008080">40</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">41</span> <span style="COLOR: #000000">            mailsender.send(preparator);<br /></span><span style="COLOR: #008080">42</span> <span style="COLOR: #000000">        }<br /></span><span style="COLOR: #008080">43</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000">(MailException ex) {<br /></span><span style="COLOR: #008080">44</span> <span style="COLOR: #000000">            </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">log it and go on</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">45</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">            System.err.println(ex.getMessage());            <br /></span><span style="COLOR: #008080">46</span> <span style="COLOR: #000000">        }<br /></span><span style="COLOR: #008080">47</span> <span style="COLOR: #000000">    }<br /></span><span style="COLOR: #008080">48</span> <span style="COLOR: #000000">}</span></div><br /><p>3、spring配置发送email的applicationContext-email.xml</p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #000000">&lt;?</span><span style="COLOR: #000000">xml version</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">1.0</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> encoding</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">UTF-8</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">?&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"></span><span style="COLOR: #000000">&lt;!</span><span style="COLOR: #000000">DOCTYPE beans PUBLIC </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">-//SPRING//DTD BEAN//EN</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">    </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">http://www.springframework.org/dtd/spring-beans.dtd</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">beans</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">    </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">bean id</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">mailSender</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">org.springframework.mail.javamail.JavaMailSenderImpl</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">property name</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">host</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">smtp.</span><span style="COLOR: #000000">163</span><span style="COLOR: #000000">.com</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">property</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">property name</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">username</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">username</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">property</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">property name</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">password</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">password</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">property</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">property name</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">javaMailProperties</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">props</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">                </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">prop key</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">mail.smtp.auth</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">prop</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">                </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">prop key</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">mail.smtp.timeout</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">25000</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">prop</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">            </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">props</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">property</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">    </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">bean</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000">    </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">bean id</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">mailMessage</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">org.springframework.mail.SimpleMailMessage</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">property name</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">from</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;Email</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">property</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">property name</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">subject</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">29</span> <span style="COLOR: #000000">            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;标题</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">30</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">property</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">31</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">property name</span><span style="COLOR: #000000">="text</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">32</span> <span style="COLOR: #000000">            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;内容</span><span style="COLOR: #008000">&lt;/value&gt;</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">33</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">property</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">46</span> <span style="COLOR: #000000">    </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">bean</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">47</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">48</span> <span style="COLOR: #000000">    </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">bean id</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">orderManager</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">cn.cityyouth.service.impl.OrderManagerImpl</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">49</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">property name</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">mailsender</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">50</span> <span style="COLOR: #000000">            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">ref bean</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">mailSender</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">/&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">51</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">property</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">52</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">property name</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">message</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">53</span> <span style="COLOR: #000000">            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">ref bean</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">mailMessage</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">/&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">54</span> <span style="COLOR: #000000">        </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">property</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">55</span> <span style="COLOR: #000000">    </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">bean</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">56</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">57</span> <span style="COLOR: #000000"></span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">beans</span><span style="COLOR: #000000">&gt;</span></div><p>4、最后配置自己的jsp页面以及action</p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #0000ff">package</span><span style="COLOR: #000000"> cn.cityyouth.web.action;<br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> javax.servlet.http.HttpServletRequest;<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> javax.servlet.http.HttpServletResponse;<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.apache.struts.action.ActionForm;<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.apache.struts.action.ActionForward;<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.apache.struts.action.ActionMapping;<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.apache.struts.action.ActionMessage;<br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> org.apache.struts.action.ActionMessages;<br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> com.test.service.OrderManager;<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> SendMailAction </span><span style="COLOR: #0000ff">extends</span><span style="COLOR: #000000"> BaseAction {<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #008000">     * Method execute<br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #008000">     * <br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #008000">     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> mapping<br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #008000">     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> form<br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #008000">     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> request<br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #008000">     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> response<br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #008000">     * </span><span style="COLOR: #808080">@return</span><span style="COLOR: #008000"> ActionForward<br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #008000">     </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> ActionForward execute(ActionMapping mapping, ActionForm form,<br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000">            HttpServletRequest request, HttpServletResponse response) {<br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">        OrderManager omi</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(OrderManager)</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.getBean(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">orderManager</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">        String useremail</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">123@163.com</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">         omi.placeOrder(useremail);<br /></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">       }<br /></span><span style="COLOR: #008080">29</span> <span style="COLOR: #000000">}</span></div><p>到此所有的开发以结束。<br /></p><p>Sring邮件抽象层的主要包是：org.springframework.mail 包。它包含叫MailSender为发送邮件的核心接口和包含简单邮件属性例如from,to,cc,subject,text叫SimpleMailMessage的值对象. 这个包也包含一个检查异常的层次，它支持一个更高级别的抽象超过低级别的邮件系统异常伴随根异常存在MailException. 请参考JavaDocs为更多的信息杂邮件异常层次。</p><p><a title="心得与体会" href="/shmily432685"><font color="#5d5d5d"><a title="心得与体会" href="?catid=4001">spring in action</a> in action</font></a> also provides a sub-interface of MailSender for specialized JavaMail features such as MIME messages, namely org.springframework.mail.javamail.JavaMailSender It also provides a callback interface for preparation of JavaMail MIME messages, namely org.springframework.mail.javamail.MimeMessagePreparator</p><p>Spring也支持一个MailSender的专用于JavaMail特征例如MIME消息子接口，命名为org.springframework.javamail.JavaMailerSener。它也支持一个为JavaMail MIME信息的准备回调接口，命名为org.springframework.mail.JavaMail.MimeMessagePreparator.<br /></p><img src ="http://www.blogjava.net/shmily432685/aggbug/26041.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shmily432685/" target="_blank">闵毓</a> 2005-12-30 12:49 <a href="http://www.blogjava.net/shmily432685/archive/2005/12/30/26041.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JMS 消息服务配置</title><link>http://www.blogjava.net/shmily432685/archive/2005/12/05/22502.html</link><dc:creator>闵毓</dc:creator><author>闵毓</author><pubDate>Mon, 05 Dec 2005 01:42:00 GMT</pubDate><guid>http://www.blogjava.net/shmily432685/archive/2005/12/05/22502.html</guid><wfw:comment>http://www.blogjava.net/shmily432685/comments/22502.html</wfw:comment><comments>http://www.blogjava.net/shmily432685/archive/2005/12/05/22502.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shmily432685/comments/commentRss/22502.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shmily432685/services/trackbacks/22502.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align=left><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 14pt">JMS </SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">简介</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 14pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20.7pt; mso-char-indent-count: 1.97"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">JMS</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">即消息服务，是能够异步处理客户请求的开发模型。为支持消息的异步处理，</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">Java/J2EE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">平台引入了</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">JMS API</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">。</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">JMS</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">支持两种消息模型：点对点和发布</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">/</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">订阅模式。</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 14pt">JMS </SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">抽象对象</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 14pt"><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">从</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><a title="心得与体会" href="http://www.blogjava.net/shmily432685/admin/EditPosts.aspx?catid=4001" >spring in action</a></SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">框架中</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">jms</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">相关包结构看，主要包括如下内容：</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">●<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">org.springframework.jms:<o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">●<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">org.springframework.jms.connection:<o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">●<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">org.springframework.jms.core:<o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">●<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">org.springframework.jms.core.support:<o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">●<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">org.springframework.jms.support<o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">●<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">org.springframework.jms.support.converter:<o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 39.0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">●<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">org.springframework.jms.support.destination<o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">发送消息</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 14pt"><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">JmsTemplate</B></SPAN><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">模板的使用：</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">了解配置：</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;beans&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>&lt;!—</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">定义连接工厂</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">--&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN></SPAN></B><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>&lt;bean id=”connectionFactory” class=”org.springframework.jndi.JndiObjectFactoryBean”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>&lt;property name=”jndiName”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;value&gt;ConnectionFactory&lt;/value&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>&lt;/property&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;property name=”jndiTemplate”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;ref<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>local=”jndiTemplate”/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;/property&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;/bean&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;!—</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">定义目的地</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">--&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>&lt;bean id=”destination” class=”org.springframework.jndi.JndiObjectFactoryBean”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>&lt;property name=”jndiName”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;value&gt;queue/testQueue&lt;/value&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>&lt;/property&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 26.25pt; mso-char-indent-count: 2.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;property name=”jndiTemplate”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;ref<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>local=”jndiTemplate”/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 26.25pt; mso-char-indent-count: 2.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;/property&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;/bean&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;!—jmsTemplate</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">模板</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">--&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;bean id=”jmsTemplate” class=”org.springframework.jms.core.JmsTemplate”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;property name=”connectionFactory”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;ref local=”connectionFactory”/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;/property&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 26.25pt; mso-char-indent-count: 2.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;property name=”defaultDestination”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;ref<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>local=”destination”/&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 26.25pt; mso-char-indent-count: 2.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;/property&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;/bean&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;bean id=”jndiTemplate” class=”org.springframework.jms.jndi.JndiTemplate”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;property name=”environment”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;props&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;prop key = “java.naming.factory.initial”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Org.jnp.interfaces.NamingContextFactory<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;/prop&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;prop key = “java.naming.provider.url”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Jnp://url<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;/prop&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;prop key = “java.naming.factory.url.pkgs”&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Org.jboss.naming:org.jnp.interfaces<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;/prop&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>&lt;/props&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 26.25pt; mso-char-indent-count: 2.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;/property&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;/bean&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">&lt;beans&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">以上配置通过</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><a title="心得与体会" href="http://www.blogjava.net/shmily432685/admin/EditPosts.aspx?catid=4001" >spring in action</a> JNDI</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">抽象服务中的</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">org.springframework.jndi.JndiObjectFactoryBean</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">获得。最后定义同客户应用交互的</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">jmsTemplate.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">即</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">org.springframework.jms.core.JmsTemplate,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">它将引用以上配置中的目的地和连接工厂。</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">编写客户端代码</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 14pt"><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">JmsTemplate Jt = (JmsTemplate)this.getBean();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">Jt.sent(new MessageCreator())<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>Public Message createMessage(Session session) throws JMSException{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>Return session.createTextMessge(“</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">文本消息</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">,queue/testQueue”);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>Log.info(“</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">发送消息成功；</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">”);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36.75pt; mso-char-indent-count: 3.5"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">从</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><a title="心得与体会" href="http://www.blogjava.net/shmily432685/admin/EditPosts.aspx?catid=4001" >spring in action</a></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">框架提供的</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">Jms</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">抽象中，</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">JmsTemplate</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">同</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">MessageCreator</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">结合能够发送消息，因此需要实现</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">MessageCreator</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">回调接口定义的</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">createMessage</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.5pt">发法。</SPAN><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></SPAN></P><img src ="http://www.blogjava.net/shmily432685/aggbug/22502.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shmily432685/" target="_blank">闵毓</a> 2005-12-05 09:42 <a href="http://www.blogjava.net/shmily432685/archive/2005/12/05/22502.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>myeclipse中weblogic服务简介</title><link>http://www.blogjava.net/shmily432685/archive/2005/11/26/21511.html</link><dc:creator>闵毓</dc:creator><author>闵毓</author><pubDate>Sat, 26 Nov 2005 08:11:00 GMT</pubDate><guid>http://www.blogjava.net/shmily432685/archive/2005/11/26/21511.html</guid><wfw:comment>http://www.blogjava.net/shmily432685/comments/21511.html</wfw:comment><comments>http://www.blogjava.net/shmily432685/archive/2005/11/26/21511.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.blogjava.net/shmily432685/comments/commentRss/21511.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shmily432685/services/trackbacks/21511.html</trackback:ping><description><![CDATA[<H2 class=diaryTitle>myeclipse配置weblogic&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </H2>
<P>
<P>
<P>weblogic安装参见</P>
<P><A href="http://zhangstar.blogbus.com/logs/2004/09/413796.html">http://zhangstar.blogbus.com/logs/2004/09/413796.html</A></P>
<P>
<P>MyEclipse默认的应用服务器为JBoss3，这里我们使用WebLogic8.1。启动Eclipse，选择“窗口\首选项”菜单，打开首选项对话框。展开MyEclipse下的Application Servers结点，点击JBoss 3，选中右面的Disable单选按钮，停用JBoss 3。然后点击WebLogic 8，选中右边的Enable单选按钮，启用WebLogic服务器。同时下面的配置如下： <BR>(1)BEA home directory：C:\BEA。假定WebLogic安装在D:\BEA文件夹中。 <BR>(2)WebLogic installation directory：C:\BEA\weblogic81。 <BR>(3)Admin username：用户名。 <BR>(4)Admin password：密码。 <BR>(5)Execution domain root：安装后，启动服务的路径。 <BR>(6)Execution domain name：mydomain。 <BR>(7)Execution server name：myserver。 <BR>(8)Hostname:PortNumber：localhost:7001。 <BR>(9)Security policy file：D:\BEA\weblogic81\server\lib\weblogic.policy。 <BR>(10)JAAS login configuration file：省略。 <BR>以上配置根据安装情况不同而不同。<BR>如图：<BR><IMG height=529 alt=myeclipse_weblogic.JPG src="http://www.blogjava.net/images/blogjava_net/shmily432685/subject/myeclipse_weblogic.JPG" width=759 border=0><BR>接着展开WebLogic 8结点，点击JDK，在右边的WLS JDK name处选择WebLogic 8的默认JDK。这里组合框中缺省为j2re1.4.2_03，即之前单独安装的jre。单击Add按钮，弹出WebLogic &gt; Add JVM对话框，在JRE名称处随便输入一个名字，如jre1.4.1_02。然后在JRE主目录处选择WebLogic安装文件夹中的JDK文件夹，如D:\BEA\jdk141_02，程序会自动填充Javadoc URL文本框和JRE系统库列表框。单击确定按钮关闭对话框。这时候就可以在WLS JDK name组合框中选择jre1.4.1_02了。之后还要在下面的Optional Java VM arguments，如-ms64m -mx64m -Djava.library.path="D:/BEA/weblogic81/server/bin" -Dweblogic.management.discover=false -Dweblogic.ProductionModeEnabled=false <BR>最后点击Paths，在右边的Prepend to classpath列表框中，通过Add JAR/ZIP按钮，加入D:\BEA\weblogic81\server\lib\weblogic.jar、D:\BEA\weblogic81\server\lib\webservices.jar。如果用到数据库，还需把数据库的驱动类库加进来，这里我们用WebLogic自带的SQL Server数据库驱动库D:\BEA\weblogic81\server\lib\mssqlserver4v65.jar。 <BR>至此，MyEclipse中WebLogic8的配置工作就算完成了。下面可以看看在Eclipse中能否启动WebLogic了？自从安装了MyEclipse之后，Eclipse工具栏中就会有一个Run/Stop Servers下拉按钮。点击该按钮的下拉部分，选择“WebLogic 8\Start”菜单，即开始启动WebLogic了。通过查看下面的控制台消息，就可以知道启动是否成功，或有什么异常发生。停止WebLogic可选择“WebLogic\Stop”菜单</P><img src ="http://www.blogjava.net/shmily432685/aggbug/21511.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shmily432685/" target="_blank">闵毓</a> 2005-11-26 16:11 <a href="http://www.blogjava.net/shmily432685/archive/2005/11/26/21511.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>web.xml规则经过整理总结如下：</title><link>http://www.blogjava.net/shmily432685/archive/2005/11/11/19258.html</link><dc:creator>闵毓</dc:creator><author>闵毓</author><pubDate>Fri, 11 Nov 2005 02:29:00 GMT</pubDate><guid>http://www.blogjava.net/shmily432685/archive/2005/11/11/19258.html</guid><wfw:comment>http://www.blogjava.net/shmily432685/comments/19258.html</wfw:comment><comments>http://www.blogjava.net/shmily432685/archive/2005/11/11/19258.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shmily432685/comments/commentRss/19258.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shmily432685/services/trackbacks/19258.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">1、部署描述符文件就像所有</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">XML</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">文件一样，必须以一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">XML</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">头开始。这个头声明可以使用的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">XML</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">版本并给出文件的字符编码。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>2、DOCYTPE</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">声明必须立即出现在此头之后。这个声明告诉服务器适用的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">servlet</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">规范的版本（如</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">2.2</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">或</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">2.3</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">）并指定管理此文件其余部分内容的语法的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">DTD(Document Type Definition</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">，文档类型定义</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">)</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR></SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">所有部署描述符文件的顶层（根）元素为</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">web-app</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">。请注意，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">XML</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素不像</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">HTML</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">，他们是大小写敏感的。因此，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">web-App</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">WEB-APP</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">都是不合法的，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">web-app</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">必须用小写。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">1</SPAN>&nbsp;<SPAN style="COLOR: #000000">&lt;?</SPAN><SPAN style="COLOR: #000000">xml&nbsp;version</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">1.0</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;encoding</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">UTF-8</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">?&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">2</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">3</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">web</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">app&nbsp;xmlns</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">http://java.sun.com/xml/ns/j2ee</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;xmlns:xsi</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">http://www.w3.org/2001/XMLSchema-instance</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;version</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">2.4</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;xsi:schemaLocation</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">http://java.sun.com/xml/ns/j2ee&nbsp;&nbsp;&nbsp;http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">4</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">5</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">web</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">app</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">6</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">7</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN></DIV>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt">2、 </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">部署描述符文件内的元素次序</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 22pt; mso-char-indent-count: 2.0"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">XML </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素不仅是大小写敏感的，而且它们还对出现在其他元素中的次序敏感。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 22pt; mso-char-indent-count: 2.0"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">XML</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">头必须是文件中的第一项，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">DOCTYPE</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">声明必须是第二项，而</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">web- app</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素必须是第三项。在</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">web-app</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素内，元素的次序也很重要。服务器不一定强制要求这种次序，但它们允许（实际上有些服务器就是这样做的）完全拒绝执行含有次序不正确的元素的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">Web</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">应用。这表示使用非标准元素次序的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">web.xml</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">文件是不可移植的。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 22pt; mso-char-indent-count: 2.0"><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">下面的列表给出了所有可直接出现在</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">web-app</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素内的合法元素所必需的次序。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 22pt; mso-char-indent-count: 2.0"><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">此列表说明</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">servlet</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素必须出现在所有</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">servlet-mapping</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素之前。请注意，所有这些元素都是可选的。因此，可以省略掉某一元素，但不能把它放于不正确的位置。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l icon icon</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素指出</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">IDE</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">GUI</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">工具用来表示</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">Web</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">应用的一个和两个图像文件的位置。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l display-name display-name</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素提供</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">GUI</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">工具可能会用来标记这个特定的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">Web</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">应用的一个名称。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l description description</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素给出与此有关的说明性文本。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l context-param context-param</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素声明应用范围内的初始化参数。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l filter </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">过滤器元素将一个名字与一个实现</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">javax.servlet.Filter</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">接口的类相关联。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l filter-mapping </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">一旦命名了一个过滤器，就要利用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">filter-mapping</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素把它与一个或多个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">servlet</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">或</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">JSP</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">页面相关联。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l listener servlet API</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">的版本</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">2.3</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">增加了对事件监听程序的支持，事件监听程序在建立、修改和删除会话或</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">servlet</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">环境时得到通知。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">Listener</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素指出事件监听程序类。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l servlet </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">在向</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">servlet</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">或</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">JSP</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">页面制定初始化参数或定制</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">URL</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">时，必须首先命名</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">servlet</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">或</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">JSP</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">页面。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">Servlet</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素就是用来完成此项任务的。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l servlet-mapping </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">服务器一般为</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">servlet</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">提供一个缺省的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">URL</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><A href="http://host/webAppPrefix/servlet/ServletName。但是，常常会更改这个URL，以便servlet可以访问初始化参数或更容易地处理相对URL。在更改缺省URL时，使用servlet-mapping元素。" target=_blank>http://host/webAppPrefix/servlet/ServletName<SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana"><SPAN lang=EN-US>。但是，常常会更改这个</SPAN></SPAN>URL<SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana"><SPAN lang=EN-US>，以便</SPAN></SPAN>servlet<SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana"><SPAN lang=EN-US>可以访问初始化参数或更容易地处理相对</SPAN></SPAN>URL<SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana"><SPAN lang=EN-US>。在更改缺省</SPAN></SPAN>URL<SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana"><SPAN lang=EN-US>时，使用</SPAN></SPAN>servlet-mapping<SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana"><SPAN lang=EN-US>元素。</SPAN></SPAN></A><BR>l session-config </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">如果某个会话在一定时间内未被访问，服务器可以抛弃它以节省内存。可通过使用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">HttpSession</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">setMaxInactiveInterval</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">方法明确设置单个会话对象的超时值，或者可利用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">session-config</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素制定缺省超时值。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l mime-mapping </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">如果</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">Web</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">应用具有想到特殊的文件，希望能保证给他们分配特定的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">MIME</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">类型，则</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">mime-mapping</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素提供这种保证。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l welcom-file-list welcome-file-list</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素指示服务器在收到引用一个目录名而不是文件名的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">URL</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">时，使用哪个文件。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l error-page error-page</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素使得在返回特定</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">HTTP</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">状态代码时，或者特定类型的异常被抛出时，能够制定将要显示的页面。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l taglib taglib</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素对标记库描述符文件（</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">Tag Libraryu Descriptor file</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">）指定别名。此功能使你能够更改</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">TLD</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">文件的位置，而不用编辑使用这些文件的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">JSP</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">页面。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l resource-env-ref resource-env-ref</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素声明与资源相关的一个管理对象。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l resource-ref resource-ref</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素声明一个资源工厂使用的外部资源。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l security-constraint security-constraint</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素制定应该保护的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">URL</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">。它与</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">login-config</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素联合使用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l login-config </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">login-config</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素来指定服务器应该怎样给试图访问受保护页面的用户授权。它与</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">sercurity-constraint</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素联合使用。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l security-role security-role</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素给出安全角色的一个列表，这些角色将出现在</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">servlet</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素内的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">security-role-ref</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">role-name</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">子元素中。分别地声明角色可使高级</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">IDE</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">处理安全信息更为容易。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l env-entry env-entry</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素声明</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">Web</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">应用的环境项。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l ejb-ref ejb-ref</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素声明一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">EJB</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">的主目录的引用。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana"><BR>l ejb-local-ref ejb-local-ref</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">元素声明一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Verdana">EJB</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">的本地主目录的应用。<BR>3、</SPAN></P><img src ="http://www.blogjava.net/shmily432685/aggbug/19258.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shmily432685/" target="_blank">闵毓</a> 2005-11-11 10:29 <a href="http://www.blogjava.net/shmily432685/archive/2005/11/11/19258.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring JMS 消息处理及配置</title><link>http://www.blogjava.net/shmily432685/archive/2005/11/10/19071.html</link><dc:creator>闵毓</dc:creator><author>闵毓</author><pubDate>Wed, 09 Nov 2005 16:14:00 GMT</pubDate><guid>http://www.blogjava.net/shmily432685/archive/2005/11/10/19071.html</guid><wfw:comment>http://www.blogjava.net/shmily432685/comments/19071.html</wfw:comment><comments>http://www.blogjava.net/shmily432685/archive/2005/11/10/19071.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shmily432685/comments/commentRss/19071.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shmily432685/services/trackbacks/19071.html</trackback:ping><description><![CDATA[<P><A name=IDAKT00><SPAN class=atitle><FONT face=Arial size=4><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> JMS</FONT></SPAN></A></P>
<P><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 JMS 抽象框架简化了 JMS API 的使用，并与 JMS 提供者（比如 IBM 的 WebSphere MQ 5.3）平滑地集成。<I>org.springframework.jms.core</I> 包提供了在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 中使用 JMS 的核心功能。它的模板类处理资源的创建和释放，简化了 JMS 的使用。</P>
<P>像其他大多数 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 模板类一样，JMS 模板类提供了执行公共操作的 helper 方法。在需要更复杂应用的情况下，类把处理任务的核心委托给用户实现的回调接口。JMS 类提供了方便的方法，用来发送消息、同步地使用消息以及向用户公开 JMS 会话和消息的制作者。</P>
<P>以下 JMS 包和 <I>org.springframework.jms.core</I> 一起构成了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> JMS 的功能：</P>
<DL>
<DT><B>org.springframework.jms.support</B> 
<DD>提供转换 <CODE>JMSException</CODE> 的功能。转换代码把检测到的 <CODE>JMSException</CODE> 层次结构转换成未检测到异常的镜像层次结构。 
<DT><B>org.springframework.jms.support.converter</B> 
<DD>提供 <CODE>MessageConverter</CODE> 抽象，以在 Java 对象和 JMS 消息之间进行转换。 
<DT><B>org.springframework.jms.support.destination</B> 
<DD>提供管理 JMS 目标的不同策略，比如针对 JNDI 中保存的目标的服务定位器。 
<DT><B>org.springframework.jms.connection</B> 
<DD>提供适合在独立应用程序中使用的 <CODE>ConnectionFactory</CODE> 实现。connection 还包含针对 JMS 的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> <CODE>PlatformTransactionManager</CODE> 实现。它允许把 JMS 作为事务性资源集成到 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的事务管理机制中。</DD></DL><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAZU00><SPAN class=atitle><FONT face=Arial size=4>IBM WebSphere MQ</FONT></SPAN></A></P>
<P>就像前面提到的，示例应用程序会用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 JMS 框架通过 JMS 接口与 IBM 的 WebSphere MQ 集成。通过在应用程序和 Web 服务之间传递消息，WebSphere MQ 提供了可靠的、有恢复能力的应用程序集成。它使用队列和事务性工具帮助保持消息跨网络的完整性。WebSphere MQ 降低了信息丢失的风险和调和通信 IT 系统的需要。</P>
<P>WebSphere MQ 在它所支持的所有平台上提供了一致的应用程序编程接口，这有助于让集成的程序可移植。除了标准接口外，WebSphere MQ 还完整实现了JMS 接口，包括对发布-订阅消息传递的支持。WebSphere MQ Explorer 工具可以远程地管理和配置整个 MQ 网络。管理和配置工具基于开放源码的 Eclipse 框架，而且是可扩展的。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDABV00><SPAN class=atitle><FONT face=Arial size=4><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> JMS 模板</FONT></SPAN></A></P>
<P><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架提供了 <CODE>JmsTemplate</CODE> 的两个实现。<CODE>JmsTemplate</CODE> 类使用 JMS 1.1 API，子类 <CODE>JmsTemplate102</CODE> 则使用 JMS 1.0.2 API。我的示例应用程序使用的是 <CODE>JmsTemplate102</CODE>。</P>
<P>JMS 模板被用来发送和接收 JMS 消息。Spring 采用回调机制对 JMS 信息传递进行协调。<CODE>MessageCreator</CODE> 回调接口用 <CODE>JmsTemplate</CODE> 中的调用代码提供的 <CODE>Session</CODE> 创建消息。为了支持 JMS API 更复杂的应用，回调 <CODE>SessionCallback</CODE> 向用户提供了 JMS 会话，而 <CODE>callback ProducerCallback</CODE> 则公开了 <CODE>Session</CODE> 和 <CODE>MessageProducer</CODE> 组合。 </P>
<P>清单 1 显示了示例应用程序使用的 JMS 模板的配置。清单摘自 <I><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a>-mqseries-jms.xml</I> 文件（请参阅 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#download"><FONT color=#996699>下载</FONT></A>）。 </P><BR><A name=code1><B>清单 1. JMS 模板配置</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>

<FONT face="Lucida Console">  &lt;!-- JMS Queue Template --&gt;
  &lt;bean id="jmsQueueTemplate" 
          class="org.springframework.jms.core.JmsTemplate102"&gt;
    &lt;property name="connectionFactory"&gt;
      &lt;ref bean="jmsQueueConnectionFactory"/&gt;
    &lt;/property&gt;
    &lt;property name="destinationResolver"&gt;
      &lt;ref bean="jmsDestinationResolver"/&gt;
    &lt;/property&gt;
    &lt;property name="pubSubDomain"&gt;
      &lt;value&gt;false&lt;/value&gt;
    &lt;/property&gt;
    &lt;property name="receiveTimeout"&gt;
      &lt;value&gt;20000&lt;/value&gt;
    &lt;/property&gt;
  &lt;/bean&gt;

</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P><CODE>jmsQueueTemplate</CODE> bean 与 JMS 连接工厂和 JMS 目标解析器绑定在一起，用于解析 JMS 客户机通过 JNDI 提供的目标队列名。<CODE>connectionFactory</CODE> 属性指定了如何获得到 JMS 提供者的连接。在本例中，清单 2 显示了如何从 JNDI 检索连接工厂。</P><BR><A name=IDATX00><B>清单 2. 通过 JNDI 配置 JMS 连接工厂</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>

<FONT face="Lucida Console"> &lt;!-- JMS Queue Connection Factory --&gt;
  &lt;bean id="internalJmsQueueConnectionFactory"
          class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
    &lt;property name="jndiTemplate"&gt;
      &lt;ref bean="jndiTemplate"/&gt;
    &lt;/property&gt;
    &lt;property name="jndiName"&gt;
      &lt;value&gt;MQ_JMS_MANAGER&lt;/value&gt;
    &lt;/property&gt;
  &lt;/bean&gt;

</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>可以看到，<CODE>JndiObjectFactoryBean</CODE> 被绑定到 <CODE>internalJmsQueueConnectionFactory</CODE>。<CODE>JndiObjectFactoryBean</CODE> 用 <CODE>JndiTemplate</CODE> 属性进行 JNDI 查询。Spring 将用 <CODE>JndiTemplate</CODE> 中指定的环境属性和初始上下文在 JNDI 中查询连接工厂。清单 3 显示了 <CODE>JndiTemplate</CODE> 配置 bean 的配置。</P><BR><A name=IDAZAC1><B>清单 3. JNDI 查询的 JNDI 模板配置</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>

<FONT face="Lucida Console">&lt;bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"&gt;
    &lt;property name="environment"&gt;
      &lt;props&gt;
        &lt;prop key="java.naming.factory.initial"&gt;
            com.sun.jndi.fscontext.RefFSContextFactory
        &lt;/prop&gt;
        &lt;prop key="java.naming.provider.url"&gt;
            file:/C:/JNDI-Directory
        &lt;/prop&gt;
      &lt;/props&gt;
    &lt;/property&gt;
  &lt;/bean&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>以上配置进行 JNDI 查询时用 <CODE>com.sun.jndi.fscontext.RefFSContextFactory</CODE> 指定初始上下文工厂，用基于文件的 <I>file:/C:/JNDI-Directory</I> 作为提供者 URL。根据示例应用程序的意图，JNDI 访问会采用基于文件的 <CODE>FSContext</CODE> 版本（请参阅 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#resources"><FONT color=#996699>参考资料</FONT></A>）的配置把 MQ 队列绑定到 JNDI。</P>
<P>有了定义好的 JMS 模板，下一步就是把它绑定到示例应用程序中，然后就可以用它发送和接收消息了。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAQBC1><SPAN class=atitle><FONT face=Arial size=4><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> JMS 实现</FONT></SPAN></A></P>
<P>JMS 模板可以绑定到应用程序中，以发送和接收 JMS 消息。在清单 4 中可以看出我如何把 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#code1"><FONT color=#996699>清单 1</FONT></A> 中的 JMS 模板绑定到示例应用程序中。</P><BR><A name=IDA5BC1><B>清单 4. 把 JmsTemplate 绑定到应用程序中</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>

<FONT face="Lucida Console">  &lt;bean id="jmsSender" 
          class="springexample.client.JMSSender"&gt;
    &lt;property name="jmsTemplate102"&gt;
      &lt;ref bean="jmsQueueTemplate"/&gt;
    &lt;/property&gt;

    &lt;/bean&gt;

    &lt;bean id="jmsReceiver" 
          class="springexample.client.JMSReceiver"&gt;
    &lt;property name="jmsTemplate102"&gt;
        &lt;ref bean="jmsQueueTemplate"/&gt;
      &lt;/property&gt;

    &lt;/bean&gt;

</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>可以看到，我把 <CODE>jmsQueueTemplate</CODE> 绑定到用来发送和接收消息的 <CODE>JmsSender</CODE> 应用程序 bean 和 <CODE>JmsReceiver</CODE> bean。清单 5 显示了与 <CODE>JMSSender</CODE> 类有关的代码。</P><BR><A name=IDA0CC1><B>清单 5. 用 JmsTemplate 发送 JMS 消息的 JMSSender </B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>

<FONT face="Lucida Console"> public class JMSSender {

   private JmsTemplate102 jmsTemplate102;

   public JmsTemplate102 getJmsTemplate102() {
     return jmsTemplate102;
   }

   public void setJmsTemplate102(JmsTemplate102 jmsTemplate102) {
     this.jmsTemplate102 = jmsTemplate102;
   }

   public void sendMesage(){
     jmsTemplate102.send("JMS_RequestResponseQueue", 
                  new MessageCreator() {
        public Message createMessage(Session session) 
                  throws JMSException {
          return session.createTextMessage("This is a sample message");
        }
      });


   }
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P><CODE>JMSSender</CODE> 类用 <CODE>jmsTemplate102.send()</CODE> 方法发送 JMS 消息。<CODE>send()</CODE> 方法的第一个参数是 JNDI 队列名，队列名指定了消息应当发送到哪里。（很快就会看到如何把 WebSphere MQ 的队列名绑定到 JNDI。）<CODE>send()</CODE> 方法的第二个参数是 <CODE>MessageCreator</CODE> 类。<CODE>JmsTemplate</CODE> 中的调用代码提供了 <CODE>Session</CODE> 类，这个类提供了一个创建 JMS 消息的回调接口。</P>
<P>下一步是用 JMS 的 <CODE>Session</CODE> 类创建一个简单的文本消息。在代码执行时，消息会传递给 WebSphere MQ 服务器的队列。清单 6 显示了使用 <CODE>JmsTemplate</CODE> 检索 JMS 消息的 <CODE>JMSReceiver</CODE> 应用程序 bean 的代码。</P><BR><A name=IDAOEC1><B>清单 6. 用 JmsTemplate 检索 JMS 消息的 JMSReceiver</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>

<FONT face="Lucida Console">  public class JMSReceiver {

    private JmsTemplate102 jmsTemplate102;

    public JmsTemplate102 getJmsTemplate102() {
      return jmsTemplate102;
    }

    public void setJmsTemplate102(JmsTemplate102 jmsTemplate102) {
     this.jmsTemplate102 = jmsTemplate102;
    }

    public void processMessage(){
      Message msg = jmsTemplate102.receive("JMS_RequestResponseQueue");
      try{
        TextMessage textMessage = (TextMessage) msg;
        if( msg!=null){
        System.out.println(" Message Received --&gt;" + 
                    textMessage.getText());
        }


      }catch(Exception e){
            e.printStackTrace();
      }


    }
}
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P><CODE>JMSReceiver</CODE> 类用 <CODE>jmsTemplate102.receive()</CODE> 方法同步地接收 JMS 消息。<CODE>receive()</CODE> 方法指定 JNDI 队列名，并从中检索消息。<CODE>JMSTemplate</CODE> 类的 <CODE>processMessage()</CODE> 方法由接收 JMS 客户机调用。<CODE>JSMTemplate</CODE> bean 的属性 <I>receiveTimeout</I>（列在 <CODE>JMSTemplate</CODE> 配置中）指定接收客户机同步地从队列中接收消息时要等候的时间。</P>
<P>现在应用程序的代码已完成！下一步就是配置 WebSphere MQ 队列并把它们绑定到 JNDI 对象。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAUFC1><SPAN class=atitle><FONT face=Arial size=4>队列管理器的设置</FONT></SPAN></A></P>
<P>在运行应用程序之前，需要设置 WebSphere MQ 的队列管理器和队列，并把它们绑定到 JNDI。如果喜欢的话，可以按照这部分的示例做：只需 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#download"><FONT color=#996699>下载</FONT></A> 设置 WebSphere MQ 队列的批文件和应用程序的源代码和部署描述符即可。把 zip 文件解压到驱动器 C:。</P>
<P><B>设置队列</B><BR>运行 <I>C:\SpringSeriesPart4JMS\batch</I> 文件夹中的 <I>mqsetup.bat</I> 文件。这个批文件要求在 path 环境变量中设置好 MQ 安装的 bin 文件夹（例如 <I>C:\mqseries\bin</I>）。运行了批文件之后，应当看到消息 “<CODE>All valid MQSC commands were processed</CODE>”。要打开 MQ Explorer 并检查已经创建的队列管理器和队列，请选择 <B>Start -&gt; Programs -&gt; IBM MQSeries -&gt; MQSeriesExplorer</B>。图 1 显示出示例应用程序 <CODE>QueueManager</CODE><CODE>MQJMS.QManager</CODE> 已经创建并正在运行。</P><BR><A name="spring framework"><B>图 1. WebSphere MQ 的 QueueManager 配置</B></A><BR><IMG height=527 alt="WebSphere MQ 的 QueueManager 配置" src="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/mq_quesetup.jpg" width=542><BR>
<P>请在应用程序屏幕左侧面板上点击 <CODE>MQJMS.QManager</CODE> 下的 <B>Queues</B> 文件夹。应当看到已经创建了一个队列 <CODE>RequestResponseQueue</CODE>，如图 2 所示。</P><BR><A name="spring framework"><B>图 2. WebSphere MQ 的请求/响应队列配置</B></A><BR><IMG height=401 alt="WebSphere MQ 的请求/响应队列配置" src="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/mq_series.gif" width=572><BR>
<P>这就完成了队列的设置。</P>
<P><B>设置 JMS 和 JNDI 管理</B><BR><BR>在示例应用程序中，JNDI 的访问利用了可以从 JNDI 主页得到的基于文件的 <CODE>FSContext</CODE> 版本（请参阅 <A href=""><FONT color=#5c81a7>参考资料</FONT></A>）。<I>FSContext.jar</I> 文件也包含在 WebSphere MQ 的 JMS 支持当中。请添加文件夹 <I>\MQSeriesInstallable\MQSeries\Java\lib</I> 和 <I>\MQSeriesInstallable\MQSeries\Java\bin</I> 到系统的 PATH 环境变量中。而且，请把 <I>\MQSeriesInstallable\MQSeries\Java\lib</I> 文件夹中的所有 jar 文件添加到系统的 CLASSPATH 环境变量中。还可以运行 <I>C:\SpringSeriesPart4JMS\batch</I> 文件夹中的 <I>classpath.cmd</I> 文件，它会设置必要的 path 和 CLASSPATH 变量。要做到这点，只需要修改 <I>classpath.cmd</I> 文件中的 <CODE>MQ_JAVA_INSTALL_PATH</CODE>，把它指到 WebSphere MQ JMS 的安装目录。</P>
<P>接下来，修改 <I>\MQSeriesInstallableDirectory\Java\bin</I> 中的 <I>JMSAdmin.config</I> 配置文件，MQSeries JMS 管理程序用它指明应用程序要使用的上下文工厂和 JNDI 实现的地址。请取消以下行的注释：</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">INITIAL_CONTEXT_FACTORY=com.sun.jndi.fscontext.RefFSContextFactory
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>并注释掉其余两个 <CODE>INITIAL_CONTEXT_FACTORY</CODE> 变量。还要取消以下行的注释：</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">PROVIDER_URL=file:/C:/JNDI-Directory
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>并注释掉其余两个 <CODE>PROVIDER_URL</CODE> 变量。</P>
<P>可以在 <I>C:\SpringSeriesPart4JMS\batch</I> 文件夹中发现参考的示例配置文件。</P>
<P>为了保存 JNDI 对象，请在驱动器 C: 上创建名为 <I>JNDI-Directory</I> 的目录。切换到 <I>\MQSeriesInstallableDirectory\Java\bin</I> 目录并运行 <I>JMSAdmin</I> 批文件，应当看到 <I>InitCtx</I> 变量。</P>
<P>逐个输入以下内容：</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">def qcf(MQ_JMS_MANAGER) qmgr(MQJMS.QManager)<BR>
按回车<BR>

def q(JMS_RequestResponseQueue) qmgr(MQJMS.QManager) 
               queue(RequestResponseQueue)<BR>
按回车<BR></FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>现在已经把 WebSphere MQ 队列绑定到 JNDI 对象，作为应用程序客户可以通过 JNDI 查询对象。现在剩下的就是看代码的实际作用了！</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDASKC1><SPAN class=atitle><FONT face=Arial size=4>运行示例</FONT></SPAN></A></P>
<P>要运行示例，请从 <A href="http://prdownloads.sourceforge.net/springframework/spring-framework-1.2-rc2-with-dependencies.zip?download" target=_new><FONT color=#5c81a7><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> sourceforge download</FONT></A> 下载 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架和它的所有依赖文件并解压，例如解压到 c:\。会创建文件夹 <I>C:\spring-framework-1.2-rc2</I>（或最新版本）。</P>
<P>要运行 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 应用程序，请把本文的源代码解压到任意文件夹，例如 <I>c:\</I>。会创建文件夹 <I>SpringSeriesPart4JMS</I>。就像前面提到过的，还需要安装 Apache Ant 和它的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 依赖 jar 文件。请把 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 库 —— 即 <I><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a>.jar</I>（在 <I>C:\spring-framework-1.2-rc2\dist</I> 中）和 <I>commons-logging.jar</I>（在 <I>C:\spring-framework-1.2-rc2\lib\jakarta-commons</I> 中）拷贝到 <I>SpringSeriesPart4JMS\lib</I> 文件夹。还要把所有的 jar 库从 <I>\MQSeriesInstallableDirectory\Java\lib</I> 目录拷贝到 <I>SpringSeriesPart4JMS\lib</I> 文件夹。其中包含 MQseries 和 JMS 的相关库。现在就拥有了构建的依赖集。</P>
<P>接下来，打开命令提示符，切换到 <I>SpringProject4</I> 目录，并在命令提示符下输入以下命令：</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&gt; ant -f build-jmssender.xml.
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>这会构建并运行 <CODE>SendMQSpringJMS</CODE> 类，它会调用 <CODE>JMSSender</CODE> 类，发送消息到 WebSphere MQ <CODE>RequestResponse</CODE> 队列。<CODE>SendMQSpringJMS</CODE> 还会通过它的 <CODE>ClassPathXmlApplicationContext</CODE> 装入 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置文件。一旦 bean 全部装载，就可以通过 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 ApplicationContext 的 <CODE>getBean()</CODE> 方法访问 JMSSender（请参阅清单 7）。</P><BR><A name=IDAYMC1><B>清单 7. 装入示例应用程序的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>

<FONT face="Lucida Console">ClassPathXmlApplicationContext appContext = 
                   new ClassPathXmlApplicationContext(new String[] {
     "<a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a>-mqseries-jms.xml"
    });

JMSSender jmsSender = (JMSSender)
        appContext.getBean("jmsSender");
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>消息传递到队列上之后，请运行 JMS 接收方客户机以检索消息。请打开命令提示符，切换到目录 <I>SpringProject4</I>，并输入：</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&gt; ant -f build-jmsreceiver.xml
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>这会构建并运行 <CODE>ReceiveMQSpringJMS</CODE> 类，该类会调用 <CODE>JMSReceiver</CODE> 类，以从 WebSphere MQ 的 <CODE>RequestResponse</CODE> 队列接收文本消息。在控制台上会打印出以下消息：</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">Message Received --&gt; This is a sample message.
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P><A name=IDAWNC1><SPAN class=smalltitle><STRONG><FONT face=Arial>结束语</FONT></STRONG></SPAN></A></P>
<P>在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 系列的最后这篇文章中，您学习了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> JMS 框架的基础。我首先介绍了示例应用程序的核心组件 —— <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> JMS 框架和 IBM 的 WebSphere MQ 5.3，然后介绍了如何用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> JMS 模板向 WebSphere MQ 队列发送消息和从中接收消息。虽然这个示例非常简单，但是可以把这里介绍的步骤应用到更复杂的应用程序。</P>
<P>我希望介绍 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架核心模块的这一系列对您有所帮助。请参阅 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#resources"><FONT color=#996699>参考资料</FONT></A> 学习更多有关 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> JMS 的内容。</P><BR><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><SPAN class=atitle><A name=download><FONT face=Arial size=4>下载</FONT></A></SPAN></P>
<TABLE class=data-table-1 cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TH>描述</TH>
<TH>名字</TH>
<TH style="TEXT-ALIGN: right">大小</TH>
<TH>&nbsp;下载方法</TH></TR>
<TR>
<TD class=tb-row>Example code, <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> files, and build scripts</TD>
<TD noWrap>wa-spring4-SpringSeriesPart4JMS.zip</TD>
<TD style="TEXT-ALIGN: right" noWrap>17 KB</TD>
<TD noWrap>&nbsp;<A class=fbox href="ftp://www6.software.ibm.com/software/developer/library/wa-spring4-SpringSeriesPart4JMS.zip"><B><FONT color=#5c81a7>FTP</FONT></B></A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR vAlign=top>
<TD colSpan=5><FONT color=#5c81a7><IMG height=12 alt="" src="http://www.ibm.com/i/c.gif" width=12 border=0></FONT></TD></TR>
<TR>
<TD><FONT color=#5c81a7><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/fw.gif" width=16></FONT></TD>
<TD><A class=fbox href="http://www-128.ibm.com/developerworks/cn/whichmethod.html"><FONT color=#5c81a7>关于下载方法的信息</FONT></A></TD>
<TD><FONT color=#5c81a7><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=50></FONT></TD>
<TD><FONT color=#5c81a7><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/sout.gif" width=16></FONT></TD>
<TD><A class=fbox href="http://www.adobe.com/products/acrobat/readstep2.html"><FONT color=#5c81a7>获取 Adobe® Reader®</FONT></A></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><FONT color=#5c81a7><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></FONT></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><FONT color=#5c81a7><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></FONT></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring4/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=resources><SPAN class=atitle><FONT face=Arial size=4>参考资料 </FONT></SPAN></A></P><B>学习</B><BR>
<UL>
<LI>您可以参阅本文在 developerWorks 全球站点上的 <A href="http://www.ibm.com/developerworks/web/library/wa-spring4/" target=_blank><FONT color=#5c81a7>英文原文</FONT></A>。<BR><BR>
<LI>“<A href="http://www-128.ibm.com/developerworks/cn/opensource/os-lightweight3/"><FONT color=#5c81a7>轻量级开发的成功秘诀，第 3 部分: <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 露出水面</FONT></A>”（Bruce Tate，developerWorks，2005 年 6 月）：Bruce Tate 介绍了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 作为轻量级容器的意义。<BR><BR>
<LI>“<A href="http://www.ibm.com/developerworks/ibm/library/i-ad5mq53/"><FONT color=#5c81a7>Writing Java Message Service programs using WebSphere MQ V5.3</FONT></A>”（Willy Farrell，developerWorks，2003 年 10 月）：安装和配置 WebSphere MQ 5.3 以进行 JMS 开发和测试的详细指导。<BR><BR>
<LI>“<A href="http://www-128.ibm.com/developerworks/cn/webservices/ws-jms/"><FONT color=#5c81a7>用 AXIS 编写 JMS 应用程序</FONT></A>”（Naveen Balani，developerWorks，2003 年 2 月）：开发同步 JMS SOAP 应用程序。<BR><BR>
<LI><A href="http://www.ibm.com/developerworks/web/"><FONT color=#5c81a7>Web 架构专区</FONT></A>：数百篇涉及基于 Web 的解决方案的文章。</LI></UL><BR><B>获得产品和技术</B><BR>
<UL>
<LI><A href="http://prdownloads.sourceforge.net/springframework/"><FONT color=#5c81a7><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 主页</FONT></A>：下载 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架。<BR><BR>
<LI><A href="http://java.sun.com/products/jms/index.jsp"><FONT color=#5c81a7>JMS 主页</FONT></A>：下载 Java 消息服务。<BR><BR>
<LI><A href="http://java.sun.com/products/jndi/index.html"><FONT color=#5c81a7>JNDI 主页</FONT></A>：下载 JNDI 和 FSContext。<BR><BR>
<LI><A href="http://www.ibm.com/software/integration/wmq/"><FONT color=#5c81a7>WebSphere MQ</FONT></A>：得到可信的、可靠的 JMS 提供者。<BR><BR></LI></UL><BR><B>讨论</B><BR>
<UL>
<LI>加入本文的<A href="javascript:void forumWindow()"><FONT color=#5c81a7>论坛</FONT></A> 。(您也可以通过点击文章顶部或者底部的论坛链接参加讨论。)<BR><BR>
<LI><A href="http://www.ibm.com/developerworks/blogs/"><FONT color=#5c81a7>developerWorks blogs</FONT></A>：加入 developerWorks 社区。<BR></LI></UL><img src ="http://www.blogjava.net/shmily432685/aggbug/19071.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shmily432685/" target="_blank">闵毓</a> 2005-11-10 00:14 <a href="http://www.blogjava.net/shmily432685/archive/2005/11/10/19071.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring MVC 框架以及基本配置</title><link>http://www.blogjava.net/shmily432685/archive/2005/11/09/19070.html</link><dc:creator>闵毓</dc:creator><author>闵毓</author><pubDate>Wed, 09 Nov 2005 15:59:00 GMT</pubDate><guid>http://www.blogjava.net/shmily432685/archive/2005/11/09/19070.html</guid><wfw:comment>http://www.blogjava.net/shmily432685/comments/19070.html</wfw:comment><comments>http://www.blogjava.net/shmily432685/archive/2005/11/09/19070.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shmily432685/comments/commentRss/19070.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shmily432685/services/trackbacks/19070.html</trackback:ping><description><![CDATA[<P>&nbsp; <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 可插入的 MVC 架构，可以选择是使用内置的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> Web 框架还是 Struts 这样的 Web 框架。通过策略接口，Spring 框架是高度可配置的，而且包含多种视图技术，例如 JavaServer Pages（JSP）技术、Velocity、Tiles、iText 和 POI。Spring MVC 框架并不知道使用的视图，所以不会强迫您只使用 JSP 技术。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色，这种分离让它们更容易进行定制。</P>
<P>&nbsp; <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 Web MVC 框架是围绕 <CODE>DispatcherServlet</CODE> 设计的，它把请求分派给处理程序，同时带有可配置的处理程序映射、视图解析、本地语言、主题解析以及上载文件支持。默认的处理程序是非常简单的 <CODE>Controller</CODE> 接口，只有一个方法 <CODE>ModelAndView handleRequest(request, response)</CODE>。Spring 提供了一个控制器层次结构，可以派生子类。如果应用程序需要处理用户输入表单，那么可以继承 <CODE>AbstractFormController</CODE>。如果需要把多页输入处理到一个表单，那么可以继承 <CODE>AbstractWizardFormController</CODE>。</P>
<P>&nbsp; 示例应用程序有助于直观地学习这些特性。银行应用程序允许用户检索他们的帐户信息。在构建银行应用程序的过程中，可以学到如何配置 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 框架和实现框架的视图层，视图层包括 JSTL 标记（用于显示输出的数据）和JavaServer Pages 技术。<BR></P>
<P><A name=IDAFUWW><SPAN class=atitle><FONT face=Arial size=4>配置 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC</FONT></SPAN></A></P>
<P>要开始构建示例应用程序，请配置 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的 <CODE>DispatcherServlet</CODE>。请在 <I>web.xml</I> 文件中注册所有配置。清单 1 显示了如何配置 <CODE>sampleBankingServlet</CODE>。</P>
<P><BR><A name=code1><B>清单 1. 配置 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC DispatcherServlet</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;servlet&gt;
   &lt;servlet-name&gt;sampleBankingServlet&lt;/servlet-name&gt;  
   &lt;servlet-class&gt;
      org.springframework.we.servlet.DispatcherServlet
   &lt;servlet-class&gt;
   &lt;load-on-startup&gt;1&lt;load-on-startup&gt;
&lt;servlet&gt;        
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR></P>
<P><CODE>DispatcherServlet</CODE> 从一个 XML 文件装入 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 应用程序上下文，XML 文件的名称是 servlet 的名称后面加上 <I>-servlet</I> 。在这个示例中，<CODE>DispatcherServlet</CODE> 会从 <I>sampleBankingServlet-servlet.xml</I> 文件装入应用程序上下文。 </P>
<P><A name=IDANVWW><SPAN class=smalltitle><STRONG><FONT face=Arial>配置应用程序的 URL</FONT></STRONG></SPAN></A></P>
<P>下一步是配置想让 <CODE>sampleBankingServlet</CODE> 处理的 URL。同样，还是要在 <I>web.xml</I> 中注册所有这些信息。</P>
<P><BR><A name=code2><B>清单 2. 配置想要处理的 URL</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;servlet-mapping&gt;
&lt;servlet-name&gt; sampleBankingServlet&lt;servlet-name&gt;
&lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR></P>
<P><A name=loadcfgfile><SPAN class=smalltitle><STRONG><FONT face=Arial>装入配置文件</FONT></STRONG></SPAN></A></P>
<P>下面，装入配置文件。为了做到这点，请为 Servlet 2.3 规范注册 <CODE>ContextLoaderListener</CODE> 或为 Servlet 2.2 及以下的容器注册 <CODE>ContextLoaderServlet</CODE>。为了保障后向兼容性，请用 <CODE>ContextLoaderServlet</CODE>。在启动 Web 应用程序时，<CODE>ContextLoaderServlet</CODE> 会装入 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置文件。清单 3 注册了 <CODE>ContextLoaderServlet</CODE>。</P>
<P><BR><A name=code3><B>清单 3. 注册 ContextLoaderServlet</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;servlet&gt;
  &lt;servlet-name&gt;context&gt;servlet-name&gt;
  &lt;servlet-class&gt;
     org.springframework.web.context.ContextLoaderServlet
  &lt;/servlet-class&gt;
&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;

</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR></P>
<P><CODE>contextConfigLocation</CODE> 参数定义了要装入的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置文件，如下面的 servlet 上下文所示。</P>
<P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;context-param&gt;
&lt;param-value&gt;contextConfigLocation&lt;/param-value&gt;
&lt;param-value&gt;/WEB-INF/sampleBanking-services.xml&lt;/param-value&gt;
&lt;/context-param&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR></P>
<P><I>sampleBanking-services.xml</I> 文件代表示例银行应用程序服务的配置和 bean 配置。如果想装入多个配置文件，可以在 <CODE>&lt;param-value&gt;</CODE> 标记中用逗号作分隔符。<BR>-------------------------------------------------------------------------------------------------------------------------<BR></P>
<P><A name=IDAZXWW><SPAN class=atitle><FONT face=Arial size=4><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 示例</FONT></SPAN></A></P>
<P>示例银行应用程序允许用户根据惟一的 ID 和口令查看帐户信息。虽然 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 提供了其他选项，但是我将采用 JSP 技术作为视图页面。这个简单的应用程序包含一个视图页用于用户输入（ID 和口令），另一页显示用户的帐户信息。</P>
<P>我从 <CODE>LoginBankController</CODE> 开始，它扩展了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的 <CODE>SimpleFormController</CODE>。<CODE>SimpleFormContoller</CODE> 提供了显示从 <CODE>HTTP GET</CODE> 请求接收到的表单的功能，以及处理从 <CODE>HTTP POST</CODE> 接收到的相同表单数据的功能。<CODE>LoginBankController</CODE> 用 <CODE>AuthenticationService</CODE> 和 <CODE>AccountServices</CODE> 服务进行验证，并执行帐户活动。“ <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#cnfgvwprop"><FONT color=#996699>配置视图属性</FONT></A> ”一节中的 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#code5"><FONT color=#996699>清单 5</FONT></A> 描述了如何把 <CODE>AuthenticationService</CODE> 和 <CODE>AccountServices</CODE> 连接到 <CODE>LoginBankController</CODE>。 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/sidefile1.html"><FONT color=#5c81a7>清单 4</FONT></A> 显示了 <CODE>LoginBankController</CODE> 的代码。</P>
<P><A name=cnfgvwprop><SPAN class=smalltitle><STRONG><FONT face=Arial>配置视图属性</FONT></STRONG></SPAN></A></P>
<P>下面，我必须注册在接收到 <CODE>HTTP GET</CODE> 请求时显示的页面。我在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置中用 <CODE>formView</CODE> 属性注册这个页面，如清单 5 所示。<CODE>sucessView</CODE> 属性代表表单数据提交而且 <CODE>doSubmitAction()</CODE> 方法中的逻辑成功执行之后显示的页面。<CODE>formView</CODE> 和 <CODE>sucessView</CODE> 属性都代表被定义的视图的逻辑名称，逻辑名称映射到实际的视图页面。</P>
<P><BR><A name=code5><B>清单 5. 注册 LoginBankController</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section><FONT face="Lucida Console">  
   &lt;bean id="loginBankController"
         class="springexample.controller.LoginBankController"&gt;
      &lt;property name="sessionForm"&gt;&lt;value&gt;true&lt;/value&gt;&lt;/property&gt;
   &lt;property name="commandName"&gt;&lt;value&gt;loginCommand&lt;/value&gt;&lt;/property&gt;
   &lt;property name="commandClass"&gt;
      &lt;value&gt;springexample.commands.LoginCommand&lt;/value&gt;
   &lt;/property&gt;

      &lt;property name="authenticationService"&gt;
         &lt;ref bean="authenticationService" /&gt;
      &lt;/property&gt;
      &lt;property name="accountServices"&gt;
         &lt;ref bean="accountServices" /&gt;
      &lt;/property&gt;
      &lt;property name="formView"&gt;
         &lt;value&gt;login&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="successView"&gt;
         &lt;value&gt;accountdetail&lt;/value&gt;
      &lt;/property&gt;

   &lt;/bean&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR></P>
<P><CODE>commandClass</CODE> 和 <CODE>commandName</CODE> 标记决定将在视图页面中活动的 bean。例如，可以通过 <I>login.jsp</I> 页面访问 <CODE>loginCommand</CODE> bean，这个页面是应用程序的登录页面。一旦用户提交了登录页面，应用程序就可以从 <CODE>LoginBankController</CODE> 的 <CODE>onSubmit()</CODE> 方法中的命令对象检索出表单数据。</P>
<P><A name=IDACECUB><SPAN class=smalltitle><STRONG><FONT face=Arial>视图解析器</FONT></STRONG></SPAN></A></P>
<P><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的 <I>视图解析器</I> 把每个逻辑名称解析成实际的资源，即包含帐户信息的 JSP 文件。我用的是 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE>InternalResourceViewResolver</CODE>，如 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/sidefile2.html"><FONT color=#5c81a7>清单 6</FONT></A> 所示。</P>
<P>因为我在 JSP 页面中使用了 JSTL 标记，所以用户的登录名称解析成资源 <I>/jsp/login.jsp</I>，而 <CODE>viewClass</CODE> 成为 <CODE>JstlView</CODE>。</P>
<P><A name=IDABFCUB><SPAN class=smalltitle><STRONG><FONT face=Arial>验证和帐户服务</FONT></STRONG></SPAN></A></P>
<P>就像前面提到的，<CODE>LoginBankController</CODE> 内部连接了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE>AccountServices</CODE> 和 <CODE>AuthenticationService</CODE>。<CODE>AuthenticationService</CODE> 类处理银行应用程序的验证。<CODE>AccountServices</CODE> 类处理典型的银行服务，例如查找交易和电汇。清单 7 显示了银行应用程序的验证和帐户服务的配置。</P>
<P><BR><A name=code7><B>清单 7. 配置验证和帐户服务</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;beans&gt;

   &lt;bean id="accountServices" 
      class="springexample.services.AccountServices"&gt;

   &lt;/bean&gt;

   &lt;bean id="authenticationService" 
      class="springexample.services.AuthenticationService"&gt;

   &lt;/bean&gt;

&lt;/beans&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR></P>
<P>以上服务在 <I>sampleBanking-services.xml</I> 中注册，然后装入 <I>web.xml</I> 文件中，就像 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#loadcfgfile"><FONT color=#996699>前面讨论的那样</FONT></A>。控制器和服务配置好后，这个简单的应用程序就完成了。现在我们来看看部署和测试它时会发生什么!<BR>-------------------------------------------------------------------------------------------------------------------------<BR></P>
<P><A name=IDAQGCUB><SPAN class=atitle><FONT face=Arial size=4>部署应用程序</FONT></SPAN></A></P>
<P>我把示例应用程序部署在 Tomcat servlet 容器中。Tomcat 是 Java Servlet 和 Java ServerPagest 技术的官方参考实现中使用的 servlet 容器。如果以前没这么做过，请 <A href="http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi" target=_new><FONT color=#5c81a7>下载 jakarta-tomcat-5.0.28.exe</FONT></A> 并运行它把 Tomcat 安装到自己喜欢的任何位置，例如 <CODE>c:\tomcat5.0</CODE>。</P>
<P>接下来，<A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#download"><FONT color=#996699>下载示例代码</FONT></A> 并释放到驱动器（例如 <I>c:\</I> ）上。创建了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 项目的文件夹之后，打开它并把 <I><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a>-banking</I> 子文件夹拷贝到 <I>c:\tomvat5.0\webapps</I>。spring-banking 文件夹是一个 Web 档案，里面包含 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 示例应用程序。lib 文件夹包含应用程序需要的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架、与Spring 相关的 MVC 库以及 JSTL 标记库和 jar 文件。</P>
<P>要启动 Tomcat 服务器，请使用以下命令：</P>
<P><CODE>cd bin C:\Tomcat 5.0\bin&gt; catalina.bat start</CODE> </P>
<P>Tomcat 应当启动并部署 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 示例应用程序。<BR>=================================================================================<BR></P>
<P><A name=IDASHCUB><SPAN class=atitle><FONT face=Arial size=4>测试应用程序</FONT></SPAN></A></P>
<P>要测试应用程序，请打开 Web 浏览器，指向 http://localhost:<I>tomcatport</I>/springbanking 并用 Tomcat 服务器实际运行的端口替换 <I>tomcatport</I>。应当看到图 1 所示的登录屏幕。输入用户 ID “admin”和口令“password”，并按下登录按钮。其他用户 ID 或口令会造成来自验证服务的错误。</P>
<P><BR><A name="spring framework"><B>图 1. <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 示例登录屏幕</B></A><BR><IMG height=445 alt="Spring MVC 示例登录屏幕的截屏" src="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/image1.jpg" width=600><BR></P>
<P>登录成功之后，会看到图 2 所示的帐户细节页面。</P>
<P><BR><A name="spring framework"><B>图 2. <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 示例帐户细节页面</B></A><BR><IMG height=200 alt="Spring MVC 示例帐户细节页面的截屏" src="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/image2.jpg" width=600><BR>================================================================================<BR></P>
<P><A name=IDA1ICUB><SPAN class=atitle><FONT face=Arial size=4>结束语</FONT></SPAN></A></P>
<P>在三部分的 <I><A href="http://www.ibm.com/developerworks/views/web/libraryview.jsp?search_by=The+Spring+series" target=_new><FONT color=#5c81a7><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 系列</FONT></A></I> 的第三篇文章中，我介绍了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 框架的特性。我演示了如何配置和开发 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 应用程序、如何配置 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 控制器和向其中插入依赖项、如何用 JavaServer Pages 技术开发应用程序视图，以及如何把自己的页面与 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的视图层集成。总结这篇文章时，我演示了如何在 Tomcat servlet 容器中部署应用程序以及如何在浏览器中测试它。</P>
<P>请继续关注 <I><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 系列</I> 的下一篇文章。在下一篇文章中，我将介绍如何把基于 JMS 的应用程序与 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架集成。同时，请参阅 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#resources"><FONT color=#996699>参考资料</FONT></A> 学习更多有关 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的内容。<BR>=================================================================================<BR></P>
<P><SPAN class=atitle><A name=download><FONT face=Arial size=4>下载</FONT></A></SPAN></P>
<P>
<TABLE class=data-table-1 cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TH>描述</TH>
<TH>名字</TH>
<TH style="TEXT-ALIGN: right">大小</TH>
<TH>&nbsp;下载方法</TH></TR>
<TR>
<TD class=tb-row>Example source code, <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> files, jar files</TD>
<TD noWrap>wa-spring3-SpringProjectPart3.zip</TD>
<TD style="TEXT-ALIGN: right" noWrap>1966 KB</TD>
<TD noWrap>&nbsp;<A class=fbox href="ftp://www6.software.ibm.com/software/developer/library/wa-spring2-SpringProjectPart2.zip"><B><FONT color=#5c81a7>FTP</FONT></B></A></TD></TR></TBODY></TABLE></P>=================================================================================<BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD width=10></TD>
<TD width="100%">
<TABLE class=no-print cellSpacing=0 cellPadding=0 width=160 align=right border=0>
<TBODY>
<TR>
<TD width=10><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=10></TD>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
<TBODY>
<TR>
<TD class=v14-header-1-small>文档选项</TD></TR></TBODY></TABLE>
<TABLE class=v14-gray-table-border cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD class=no-padding width=150>
<TABLE cellSpacing=0 cellPadding=0 width=143 border=0><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8> 
<FORM name=email action=https://www-130.ibm.com/developerworks/secure/email-it.jsp><INPUT type=hidden value="在 Naveen Balani 的 Spring 系列的第 3 部分中，学习如何用 Spring 框架开发基于 MVC 的 应用程序。" name=body><INPUT type=hidden value="Spring 系列，第 3 部分: 进入 Spring MVC" name=subject><INPUT type=hidden value=cn name=lang>
<SCRIPT language=JavaScript type=text/javascript>
<!--
document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="将此页作为电子邮件发送" /></td><td width="122"><p><a class="smallplainlink" href="javascript:document.email.submit();"><b>将此页作为电子邮件发送</b></a></p></td></tr>');
//-->
</SCRIPT>
 
<TBODY>
<TR vAlign=top>
<TD width=8><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8></TD>
<TD width=16><IMG height=16 alt=将此页作为电子邮件发送 src="http://www.ibm.com/i/v14/icons/em.gif" width=16 vspace=3></TD>
<TD width=122>
<P><A class=smallplainlink href="javascript:document.email.submit();"><B><FONT color=#5c81a7 size=2>将此页作为电子邮件发送</FONT></B></A></P></TD></TR><NOSCRIPT><tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt="" /></td><td width="16"><img src="//www.ibm.com/i/c.gif" height="16" width="16" alt="" /></td><td width="122" class="small"><p><span class="ast">未显示需要 JavaScript 的文档选项</span></p></td></tr></NOSCRIPT></FORM>
<TR vAlign=top>
<TD width=8><FONT color=#5c81a7 size=2><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8></FONT></TD>
<TD width=16><FONT color=#5c81a7 size=2><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/fw_bold.gif" width=16 vspace=3 border=0></FONT></TD>
<TD width=122>
<P><A class=smallplainlink href="javascript:void forumWindow()"><B><FONT color=#5c81a7 size=2>讨论</FONT></B></A></P></TD></TR>
<TR vAlign=top>
<TD width=8><FONT color=#5c81a7 size=2><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8></FONT></TD>
<TD width=16><FONT color=#5c81a7 size=2><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/dn.gif" width=16 vspace=3 border=0></FONT></TD>
<TD width=122>
<P><A class=smallplainlink href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#download"><B><FONT color=#996699 size=2>样例代码</FONT></B></A></P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
<TBODY>
<TR>
<TD class=v14-header-1-small>对此页的评价</TD></TR></TBODY></TABLE>
<TABLE class=v14-gray-table-border cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD class=no-padding width=150>
<TABLE cellSpacing=0 cellPadding=0 width=143 border=0>
<TBODY>
<TR vAlign=top>
<TD width=8><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8></TD>
<TD><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/d_bold.gif" width=16 vspace=3 border=0></TD>
<TD width=125>
<P><A class=smallplainlink href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#rate"><B><FONT color=#996699 size=2>帮助我们改进这些内容</FONT></B></A></P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR></TD></TR></TBODY></TABLE>
<P>级别: 中级</P>
<P><A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#author"><FONT color=#996699>Naveen Balani </FONT></A>, 技术架构师, Webify Solutions<BR></P>
<P>2005 年 10 月 13 日</P>
<BLOCKQUOTE>在 Naveen Balani 的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 系列的第 3 部分中，学习如何用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架开发基于 MVC 的 应用程序。</BLOCKQUOTE>
<P>在 <I><A href="http://www.ibm.com/developerworks/views/web/libraryview.jsp?search_by=The+Spring+Series" target=_new><FONT color=#996699><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 系列</FONT></A></I> 的第 3 部分中，我介绍 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 框架。就像在以前的文章中一样，我用银行示例介绍如何建模和构建简单的应用程序。示例应用程序包含了已经学过的一些技术（例如依赖注入），但是主要演示 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的特性。</P>
<P>在开始之前，请 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#download"><FONT color=#996699>下载这篇文章的源代码</FONT></A>。请参阅 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#resources"><FONT color=#996699>参考资料</FONT></A> 访问 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架和 Tomcat 5.0，运行示例需要它们。</P>
<P><A name=IDAHTWW><SPAN class=atitle><FONT face=Arial size=4><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 框架</FONT></SPAN></A></P>
<P><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 可插入的 MVC 架构，可以选择是使用内置的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> Web 框架还是 Struts 这样的 Web 框架。通过策略接口，Spring 框架是高度可配置的，而且包含多种视图技术，例如 JavaServer Pages（JSP）技术、Velocity、Tiles、iText 和 POI。Spring MVC 框架并不知道使用的视图，所以不会强迫您只使用 JSP 技术。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色，这种分离让它们更容易进行定制。</P>
<P><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 Web MVC 框架是围绕 <CODE>DispatcherServlet</CODE> 设计的，它把请求分派给处理程序，同时带有可配置的处理程序映射、视图解析、本地语言、主题解析以及上载文件支持。默认的处理程序是非常简单的 <CODE>Controller</CODE> 接口，只有一个方法 <CODE>ModelAndView handleRequest(request, response)</CODE>。Spring 提供了一个控制器层次结构，可以派生子类。如果应用程序需要处理用户输入表单，那么可以继承 <CODE>AbstractFormController</CODE>。如果需要把多页输入处理到一个表单，那么可以继承 <CODE>AbstractWizardFormController</CODE>。</P>
<P>示例应用程序有助于直观地学习这些特性。银行应用程序允许用户检索他们的帐户信息。在构建银行应用程序的过程中，可以学到如何配置 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 框架和实现框架的视图层，视图层包括 JSTL 标记（用于显示输出的数据）和JavaServer Pages 技术。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAFUWW><SPAN class=atitle><FONT face=Arial size=4>配置 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC</FONT></SPAN></A></P>
<P>要开始构建示例应用程序，请配置 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的 <CODE>DispatcherServlet</CODE>。请在 <I>web.xml</I> 文件中注册所有配置。清单 1 显示了如何配置 <CODE>sampleBankingServlet</CODE>。</P><BR><A name=code1><B>清单 1. 配置 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC DispatcherServlet</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;servlet&gt;
   &lt;servlet-name&gt;sampleBankingServlet&lt;/servlet-name&gt;  
   &lt;servlet-class&gt;
      org.springframework.we.servlet.DispatcherServlet
   &lt;servlet-class&gt;
   &lt;load-on-startup&gt;1&lt;load-on-startup&gt;
&lt;servlet&gt;        
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P><CODE>DispatcherServlet</CODE> 从一个 XML 文件装入 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 应用程序上下文，XML 文件的名称是 servlet 的名称后面加上 <I>-servlet</I> 。在这个示例中，<CODE>DispatcherServlet</CODE> 会从 <I>sampleBankingServlet-servlet.xml</I> 文件装入应用程序上下文。 </P>
<P><A name=IDANVWW><SPAN class=smalltitle><STRONG><FONT face=Arial>配置应用程序的 URL</FONT></STRONG></SPAN></A></P>
<P>下一步是配置想让 <CODE>sampleBankingServlet</CODE> 处理的 URL。同样，还是要在 <I>web.xml</I> 中注册所有这些信息。</P><BR><A name=code2><B>清单 2. 配置想要处理的 URL</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;servlet-mapping&gt;
&lt;servlet-name&gt; sampleBankingServlet&lt;servlet-name&gt;
&lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P><A name=loadcfgfile><SPAN class=smalltitle><STRONG><FONT face=Arial>装入配置文件</FONT></STRONG></SPAN></A></P>
<P>下面，装入配置文件。为了做到这点，请为 Servlet 2.3 规范注册 <CODE>ContextLoaderListener</CODE> 或为 Servlet 2.2 及以下的容器注册 <CODE>ContextLoaderServlet</CODE>。为了保障后向兼容性，请用 <CODE>ContextLoaderServlet</CODE>。在启动 Web 应用程序时，<CODE>ContextLoaderServlet</CODE> 会装入 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置文件。清单 3 注册了 <CODE>ContextLoaderServlet</CODE>。</P><BR><A name=code3><B>清单 3. 注册 ContextLoaderServlet</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;servlet&gt;
  &lt;servlet-name&gt;context&gt;servlet-name&gt;
  &lt;servlet-class&gt;
     org.springframework.web.context.ContextLoaderServlet
  &lt;/servlet-class&gt;
&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;

</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P><CODE>contextConfigLocation</CODE> 参数定义了要装入的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置文件，如下面的 servlet 上下文所示。</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;context-param&gt;
&lt;param-value&gt;contextConfigLocation&lt;/param-value&gt;
&lt;param-value&gt;/WEB-INF/sampleBanking-services.xml&lt;/param-value&gt;
&lt;/context-param&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P><I>sampleBanking-services.xml</I> 文件代表示例银行应用程序服务的配置和 bean 配置。如果想装入多个配置文件，可以在 <CODE>&lt;param-value&gt;</CODE> 标记中用逗号作分隔符。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAZXWW><SPAN class=atitle><FONT face=Arial size=4><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 示例</FONT></SPAN></A></P>
<P>示例银行应用程序允许用户根据惟一的 ID 和口令查看帐户信息。虽然 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 提供了其他选项，但是我将采用 JSP 技术作为视图页面。这个简单的应用程序包含一个视图页用于用户输入（ID 和口令），另一页显示用户的帐户信息。</P>
<P>我从 <CODE>LoginBankController</CODE> 开始，它扩展了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的 <CODE>SimpleFormController</CODE>。<CODE>SimpleFormContoller</CODE> 提供了显示从 <CODE>HTTP GET</CODE> 请求接收到的表单的功能，以及处理从 <CODE>HTTP POST</CODE> 接收到的相同表单数据的功能。<CODE>LoginBankController</CODE> 用 <CODE>AuthenticationService</CODE> 和 <CODE>AccountServices</CODE> 服务进行验证，并执行帐户活动。“ <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#cnfgvwprop"><FONT color=#996699>配置视图属性</FONT></A> ”一节中的 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#code5"><FONT color=#996699>清单 5</FONT></A> 描述了如何把 <CODE>AuthenticationService</CODE> 和 <CODE>AccountServices</CODE> 连接到 <CODE>LoginBankController</CODE>。 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/sidefile1.html"><FONT color=#5c81a7>清单 4</FONT></A> 显示了 <CODE>LoginBankController</CODE> 的代码。</P>
<P><A name=cnfgvwprop><SPAN class=smalltitle><STRONG><FONT face=Arial>配置视图属性</FONT></STRONG></SPAN></A></P>
<P>下面，我必须注册在接收到 <CODE>HTTP GET</CODE> 请求时显示的页面。我在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置中用 <CODE>formView</CODE> 属性注册这个页面，如清单 5 所示。<CODE>sucessView</CODE> 属性代表表单数据提交而且 <CODE>doSubmitAction()</CODE> 方法中的逻辑成功执行之后显示的页面。<CODE>formView</CODE> 和 <CODE>sucessView</CODE> 属性都代表被定义的视图的逻辑名称，逻辑名称映射到实际的视图页面。</P><BR><A name=code5><B>清单 5. 注册 LoginBankController</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section><FONT face="Lucida Console">  
   &lt;bean id="loginBankController"
         class="springexample.controller.LoginBankController"&gt;
      &lt;property name="sessionForm"&gt;&lt;value&gt;true&lt;/value&gt;&lt;/property&gt;
   &lt;property name="commandName"&gt;&lt;value&gt;loginCommand&lt;/value&gt;&lt;/property&gt;
   &lt;property name="commandClass"&gt;
      &lt;value&gt;springexample.commands.LoginCommand&lt;/value&gt;
   &lt;/property&gt;

      &lt;property name="authenticationService"&gt;
         &lt;ref bean="authenticationService" /&gt;
      &lt;/property&gt;
      &lt;property name="accountServices"&gt;
         &lt;ref bean="accountServices" /&gt;
      &lt;/property&gt;
      &lt;property name="formView"&gt;
         &lt;value&gt;login&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="successView"&gt;
         &lt;value&gt;accountdetail&lt;/value&gt;
      &lt;/property&gt;

   &lt;/bean&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P><CODE>commandClass</CODE> 和 <CODE>commandName</CODE> 标记决定将在视图页面中活动的 bean。例如，可以通过 <I>login.jsp</I> 页面访问 <CODE>loginCommand</CODE> bean，这个页面是应用程序的登录页面。一旦用户提交了登录页面，应用程序就可以从 <CODE>LoginBankController</CODE> 的 <CODE>onSubmit()</CODE> 方法中的命令对象检索出表单数据。</P>
<P><A name=IDACECUB><SPAN class=smalltitle><STRONG><FONT face=Arial>视图解析器</FONT></STRONG></SPAN></A></P>
<P><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的 <I>视图解析器</I> 把每个逻辑名称解析成实际的资源，即包含帐户信息的 JSP 文件。我用的是 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE>InternalResourceViewResolver</CODE>，如 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/sidefile2.html"><FONT color=#5c81a7>清单 6</FONT></A> 所示。</P>
<P>因为我在 JSP 页面中使用了 JSTL 标记，所以用户的登录名称解析成资源 <I>/jsp/login.jsp</I>，而 <CODE>viewClass</CODE> 成为 <CODE>JstlView</CODE>。</P>
<P><A name=IDABFCUB><SPAN class=smalltitle><STRONG><FONT face=Arial>验证和帐户服务</FONT></STRONG></SPAN></A></P>
<P>就像前面提到的，<CODE>LoginBankController</CODE> 内部连接了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE>AccountServices</CODE> 和 <CODE>AuthenticationService</CODE>。<CODE>AuthenticationService</CODE> 类处理银行应用程序的验证。<CODE>AccountServices</CODE> 类处理典型的银行服务，例如查找交易和电汇。清单 7 显示了银行应用程序的验证和帐户服务的配置。</P><BR><A name=code7><B>清单 7. 配置验证和帐户服务</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;beans&gt;

   &lt;bean id="accountServices" 
      class="springexample.services.AccountServices"&gt;

   &lt;/bean&gt;

   &lt;bean id="authenticationService" 
      class="springexample.services.AuthenticationService"&gt;

   &lt;/bean&gt;

&lt;/beans&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>以上服务在 <I>sampleBanking-services.xml</I> 中注册，然后装入 <I>web.xml</I> 文件中，就像 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#loadcfgfile"><FONT color=#996699>前面讨论的那样</FONT></A>。控制器和服务配置好后，这个简单的应用程序就完成了。现在我们来看看部署和测试它时会发生什么!</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAQGCUB><SPAN class=atitle><FONT face=Arial size=4>部署应用程序</FONT></SPAN></A></P>
<P>我把示例应用程序部署在 Tomcat servlet 容器中。Tomcat 是 Java Servlet 和 Java ServerPagest 技术的官方参考实现中使用的 servlet 容器。如果以前没这么做过，请 <A href="http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi" target=_new><FONT color=#5c81a7>下载 jakarta-tomcat-5.0.28.exe</FONT></A> 并运行它把 Tomcat 安装到自己喜欢的任何位置，例如 <CODE>c:\tomcat5.0</CODE>。</P>
<P>接下来，<A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#download"><FONT color=#996699>下载示例代码</FONT></A> 并释放到驱动器（例如 <I>c:\</I> ）上。创建了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 项目的文件夹之后，打开它并把 <I><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a>-banking</I> 子文件夹拷贝到 <I>c:\tomvat5.0\webapps</I>。spring-banking 文件夹是一个 Web 档案，里面包含 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 示例应用程序。lib 文件夹包含应用程序需要的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架、与Spring 相关的 MVC 库以及 JSTL 标记库和 jar 文件。</P>
<P>要启动 Tomcat 服务器，请使用以下命令：</P><CODE>cd bin C:\Tomcat 5.0\bin&gt; catalina.bat start</CODE> 
<P>Tomcat 应当启动并部署 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 示例应用程序。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDASHCUB><SPAN class=atitle><FONT face=Arial size=4>测试应用程序</FONT></SPAN></A></P>
<P>要测试应用程序，请打开 Web 浏览器，指向 http://localhost:<I>tomcatport</I>/springbanking 并用 Tomcat 服务器实际运行的端口替换 <I>tomcatport</I>。应当看到图 1 所示的登录屏幕。输入用户 ID “admin”和口令“password”，并按下登录按钮。其他用户 ID 或口令会造成来自验证服务的错误。</P><BR><A name="spring framework"><B>图 1. <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 示例登录屏幕</B></A><BR><IMG height=445 alt="Spring MVC 示例登录屏幕的截屏" src="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/image1.jpg" width=600><BR>
<P>登录成功之后，会看到图 2 所示的帐户细节页面。</P><BR><A name="spring framework"><B>图 2. <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 示例帐户细节页面</B></A><BR><IMG height=200 alt="Spring MVC 示例帐户细节页面的截屏" src="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/image2.jpg" width=600><BR><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDA1ICUB><SPAN class=atitle><FONT face=Arial size=4>结束语</FONT></SPAN></A></P>
<P>在三部分的 <I><A href="http://www.ibm.com/developerworks/views/web/libraryview.jsp?search_by=The+Spring+series" target=_new><FONT color=#5c81a7><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 系列</FONT></A></I> 的第三篇文章中，我介绍了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 框架的特性。我演示了如何配置和开发 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 应用程序、如何配置 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 控制器和向其中插入依赖项、如何用 JavaServer Pages 技术开发应用程序视图，以及如何把自己的页面与 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的视图层集成。总结这篇文章时，我演示了如何在 Tomcat servlet 容器中部署应用程序以及如何在浏览器中测试它。</P>
<P>请继续关注 <I><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 系列</I> 的下一篇文章。在下一篇文章中，我将介绍如何把基于 JMS 的应用程序与 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架集成。同时，请参阅 <A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#resources"><FONT color=#996699>参考资料</FONT></A> 学习更多有关 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC 的内容。</P><BR><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><SPAN class=atitle><A name=download><FONT face=Arial size=4>下载</FONT></A></SPAN></P>
<TABLE class=data-table-1 cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TH>描述</TH>
<TH>名字</TH>
<TH style="TEXT-ALIGN: right">大小</TH>
<TH>&nbsp;下载方法</TH></TR>
<TR>
<TD class=tb-row>Example source code, <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> files, jar files</TD>
<TD noWrap>wa-spring3-SpringProjectPart3.zip</TD>
<TD style="TEXT-ALIGN: right" noWrap>1966 KB</TD>
<TD noWrap>&nbsp;<A class=fbox href="ftp://www6.software.ibm.com/software/developer/library/wa-spring2-SpringProjectPart2.zip"><B><FONT color=#5c81a7>FTP</FONT></B></A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR vAlign=top>
<TD colSpan=5><FONT color=#5c81a7><IMG height=12 alt="" src="http://www.ibm.com/i/c.gif" width=12 border=0></FONT></TD></TR>
<TR>
<TD><FONT color=#5c81a7><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/fw.gif" width=16></FONT></TD>
<TD><A class=fbox href="http://www-128.ibm.com/developerworks/cn/whichmethod.html"><FONT color=#5c81a7>关于下载方法的信息</FONT></A></TD>
<TD><FONT color=#5c81a7><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=50></FONT></TD>
<TD><FONT color=#5c81a7><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/sout.gif" width=16></FONT></TD>
<TD><A class=fbox href="http://www.adobe.com/products/acrobat/readstep2.html"><FONT color=#5c81a7>获取 Adobe® Reader®</FONT></A></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><FONT color=#5c81a7><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></FONT></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><FONT color=#5c81a7><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></FONT></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/#main"><B><FONT color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=resources><SPAN class=atitle><FONT face=Arial size=4>参考资料 </FONT></SPAN></A></P><B>学习</B><BR>
<UL>
<LI>您可以参阅本文在 developerWorks 全球站点上的 <A href="http://www.ibm.com/developerworks/web/library/wa-spring3/" target=_blank><FONT color=#5c81a7>英文原文</FONT></A>。<BR><BR>
<LI>“<A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring1/"><FONT color=#5c81a7><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 系列，第 1 部分: <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架简介</FONT></A>”（developerWorks，2005 年 6 月）和“<A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring2/"><FONT color=#5c81a7><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 系列，第 2 部分: 当 Hibernate 遇上 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a></FONT></A>” （developerWorks，2005 年 8 月）：介绍了如何用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 技术构建轻量级的、健壮的 J2EE 应用程序以及如何把 Hibernate 事务与 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的面向方面编程（AOP）集成形成可靠的持久性框架。<BR><BR>
<LI>“<A href="http://www-128.ibm.com/developerworks/cn/java/j-aopwork1"><FONT color=#5c81a7>AOP@Work: AOP 工具比较，第 1 部分</FONT></A>”（Mik Kersten，developerWorks，2005 年 2 月）：比较了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> AOP 和 AOP 框架。<BR><BR>
<LI>“<A href="http://www-128.ibm.com/developerworks/cn/opensource/os-lightweight3/"><FONT color=#5c81a7>轻量级开发的成功秘诀，第 3 部分: <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 露出水面</FONT></A>”（Bruce Tate，developerWorks，2005 年 6 月）：演示了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 作为轻量级容器的意义。<BR><BR>
<LI>“<A href="http://www-128.ibm.com/developerworks/cn/java/wa-rubyonrails"><FONT color=#5c81a7>Ruby on Rails 和 J2EE：两者能否共存？</FONT></A>”（Aaron Rustad， developerWorks，2005 年 7 月）：比较了不太为人所知的 J2EE 框架替代品，像 EJB 和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a>。<BR><BR>
<LI><A href="http://www.ibm.com/developerworks/java/"><FONT color=#5c81a7>Java 技术专区</FONT></A>：有数百篇文章，覆盖基于 Java Web 的解决方案。</LI></UL><BR><B>获得产品和技术</B><BR>
<UL>
<LI><A href="http://prdownloads.sourceforge.net/springframework/"><FONT color=#5c81a7><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 主页</FONT></A>：下载 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架。<BR><BR>
<LI><A href="http://ant.apache.org/"><FONT color=#5c81a7>Ant 主页</FONT></A>：下载 Apache Ant。<BR><BR></LI></UL><BR><B>讨论</B><BR>
<UL>
<LI>加入本文的<A href="javascript:void forumWindow()"><FONT color=#5c81a7>论坛</FONT></A> 。(您也可以通过点击文章顶部或者底部的论坛链接参加讨论。)<BR><BR>
<LI><A href="http://www.ibm.com/developerworks/blogs/"><FONT color=#5c81a7>developerWorks blogs</FONT></A>：加入 developerWorks 社区。 <BR></LI></UL></TD></TR></TBODY></TABLE>===============================================================================<BR>
<P><A name=author><SPAN class=atitle><FONT face=Arial size=4>关于作者</FONT></SPAN></A></P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD colSpan=2><FONT face=Arial size=4><IMG height=5 alt="" src="http://www.ibm.com/i/c.gif" width="100%"></FONT></TD></TR>
<TR vAlign=top align=left>
<TD>
<P><FONT face=Arial size=4></FONT></P></TD>
<TD>
<P>Naveen Balani 把他的大部分时间花在设计和开发基于 J2EE 的框架和产品上。他为 IBM developerWorks 撰写了各类文章，涉及的主题有 ESB、SOA、JMS、Web 服务架构、CICS、AXIS、DB2、XML Extender、WebSphere Studio、MQSeries、Java 无线设备和 DB2 Everyplace for Palm、J2ME、MIDP、Java-Nokia、Visual Studio .Net 以及无线数据同步。可以给他发邮件，他的地址是：<A href="mailto:naveenbalani@rediffmail.com?cc=htc@us.ibm.com"><FONT color=#5c81a7>naveenbalani@rediffmail.com</FONT></A> 。</P></TD></TR></TBODY></TABLE>================================================================================<BR>原文出处：<A href="http://www-128.ibm.com/developerworks/cn/java/wa-spring3/">http://www-128.ibm.com/developerworks/cn/java/wa-spring3/</A><img src ="http://www.blogjava.net/shmily432685/aggbug/19070.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shmily432685/" target="_blank">闵毓</a> 2005-11-09 23:59 <a href="http://www.blogjava.net/shmily432685/archive/2005/11/09/19070.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用 Spring 更好地处理 Struts 动作 三种整合 Struts 应用程序与 Spring 的方式</title><link>http://www.blogjava.net/shmily432685/archive/2005/11/09/19069.html</link><dc:creator>闵毓</dc:creator><author>闵毓</author><pubDate>Wed, 09 Nov 2005 15:53:00 GMT</pubDate><guid>http://www.blogjava.net/shmily432685/archive/2005/11/09/19069.html</guid><wfw:comment>http://www.blogjava.net/shmily432685/comments/19069.html</wfw:comment><comments>http://www.blogjava.net/shmily432685/archive/2005/11/09/19069.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shmily432685/comments/commentRss/19069.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shmily432685/services/trackbacks/19069.html</trackback:ping><description><![CDATA[<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD width="100%"></TD>
<TD class=no-print width=192></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD width=10><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=10></TD>
<TD width="100%">
<TABLE class=no-print cellSpacing=0 cellPadding=0 width=160 align=right border=0>
<TBODY>
<TR>
<TD width=10><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=10></TD>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
<TBODY>
<TR>
<TD class=v14-header-1-small></TD></TR></TBODY></TABLE>
<TABLE class=v14-gray-table-border cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD class=no-padding width=150>
<TABLE cellSpacing=0 cellPadding=0 width=143 border=0><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8> 
<FORM name=email action=https://www-130.ibm.com/developerworks/secure/email-it.jsp>
<SCRIPT language=JavaScript type=text/javascript>
<!--
document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="将此页作为电子邮件发送" /></td><td width="122"><p><a class="smallplainlink" href="javascript:document.email.submit();"><b>将此页作为电子邮件发送</b></a></p></td></tr>');
//-->
</SCRIPT>
&nbsp;
<TBODY>
<TR vAlign=top>
<TD width=8><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8></TD>
<TD width=16></TD>
<TD width=122></TD></TR><NOSCRIPT><tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt="" /></td><td width="16"><img src="//www.ibm.com/i/c.gif" height="16" width="16" alt="" /></td><td width="122" class="small"><p><span class="ast">未显示需要 JavaScript 的文档选项</span></p></td></tr></NOSCRIPT></FORM>
<TR vAlign=top>
<TD width=8><FONT face=Verdana color=#5c81a7 size=2><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8></FONT></TD>
<TD width=16><FONT face=Verdana color=#5c81a7 size=2></FONT></TD>
<TD width=122>
<P>&nbsp;</P></TD></TR>
<TR vAlign=top>
<TD width=8><FONT face=Verdana color=#5c81a7 size=2><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8></FONT></TD>
<TD width=16><FONT face=Verdana color=#5c81a7 size=2></FONT></TD>
<TD width=122>
<P><A class=smallplainlink href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#download"><B><FONT face=Verdana color=#996699 size=2></FONT></B></A>&nbsp;</P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
<TBODY>
<TR>
<TD class=v14-header-1-small></TD></TR></TBODY></TABLE>
<TABLE class=v14-gray-table-border cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD class=no-padding width=150>
<TABLE cellSpacing=0 cellPadding=0 width=143 border=0>
<TBODY>
<TR vAlign=top>
<TD width=8><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8></TD>
<TD></TD>
<TD width=125></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR></TD></TR></TBODY></TABLE>
<P>级别: 中级</P>
<P><A href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#author"><FONT color=#996699>George Franciscus </FONT></A>, 负责人, Nexcel<BR></P>
<P>2005 年 11 月 03 日</P>
<BLOCKQUOTE><I>Struts Recipes</I> 的合著者 George Franciscus 将介绍另一个重大的 Struts 整合窍门 —— 这次是将 Struts 应用程序导入 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架。请跟随 George，他将向您展示如何改变 Struts 动作，使得管理 Struts 动作就像管理 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> beans 那样。结果是一个增强的 web 框架，这个框架可以方便地利用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> AOP 的优势。</BLOCKQUOTE>
<P>您肯定已经听说过控制反转 (IOC) 设计模式，因为很长一段时间以来一直在流传关于它的信息。如果您在任何功能中使用过 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架，那么您就知道其原理的作用。在本文中，我利用这一原理把一个 Struts 应用程序注入 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架，您将亲身体会到 IOC 模式的强大。</P>
<P>将一个 Struts 应用程序整合进 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架具有多方面的优点。首先，Spring 是为解决一些关于 JEE 的真实世界问题而设计的，比如复杂性、低性能和可测试性，等等。第二，Spring 框架包含一个 AOP 实现，允许您将面向方面技术应用于面向对象的代码。第三，一些人可能会说 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架只有<I>处理</I> Struts 比 Struts 处理自己好。但是这是观点问题，我演示三种将 Struts 应用程序整合到 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架的方法后，具体由您自己决定使用哪一种。</P>
<P>我所演示的方法都是执行起来相对简单的，但是它们却具有明显不同的优点。我为每一种方法创建了一个独立而可用的例子，这样您就可以完全理解每种方法。请参阅 <A href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#resources"><FONT color=#996699>下载</FONT></A> 部分获得完整例子源代码。请参阅 <A href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#resources"><FONT color=#996699>参考资料</FONT></A>，下载 Struts MVC 和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架。</P>
<P><A name=IDAFEWW><SPAN class=atitle><STRONG><FONT size=4>为什么 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 这么了不起？</FONT></STRONG></SPAN></A></P>
<P><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的创立者 Rod Johnson 以一种批判的眼光看待 Java™ 企业软件开发，并且提议很多企业难题都能够通过战略地使用 IOC 模式（也称作依赖注入）来解决。当 Rod 和一个具有奉献精神的开放源码开发者团队将这个理论应用于实践时，结果就产生了 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架。简言之，Spring 是一个轻型的容器，利用它可以使用一个外部 XML 配置文件方便地将对象连接在一起。每个对象都可以通过显示一个 JavaBean 属性收到一个到依赖对象的引用，留给您的简单任务就只是在一个 XML 配置文件中把它们连接好。</P>
<TABLE cellSpacing=0 cellPadding=0 width="40%" align=right border=0>
<TBODY>
<TR>
<TD width=10><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=10></TD>
<TD>
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=1>
<TBODY>
<TR>
<TD bgColor=#eeeeee><A name=IDAMEWW><B>IOC 和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a></B></A><BR>
<P>IOC 是一种使应用程序逻辑外在化的设计模式，所以它是被注入而不是被写入客户机代码中。将 IOC 与接口编程应用结合，就像 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架那样，产生了一种架构，这种架构能够减少客户机对特定实现逻辑的依赖。请参阅 <A href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#resources"><FONT color=#996699>参考资料</FONT></A> 了解更多关于 IOC 和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的信息。</P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<P>依赖注入是一个强大的特性，但是 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架能够提供更多特性。Spring 支持可插拔的事务管理器，可以给您的事务处理提供更广泛的选择范围。它集成了领先的持久性框架，并且提供一个一致的异常层次结构。Spring 还提供了一种使用面向方面代码代替正常的面向对象代码的简单机制。</P>
<P><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> AOP 允许您使用<I>拦截器</I> 在一个或多个执行点上拦截应用程序逻辑。加强应用程序在拦截器中的日志记录逻辑会产生一个更可读的、实用的代码基础，所以拦截器广泛用于日志记录。您很快就会看到，为了处理横切关注点，Spring AOP 发布了它自己的拦截器，您也可以编写您自己的拦截器。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDA2EWW><SPAN class=atitle><STRONG><FONT size=4>整合 Struts 和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a></FONT></STRONG></SPAN></A></P>
<P>与 Struts 相似，Spring 可以作为一个 MVC 实现。这两种框架都具有自己的优点和缺点，尽管大部分人同意 Struts 在 MVC 方面仍然是最好的。很多开发团队已经学会在时间紧迫的时候利用 Struts 作为构造高品质软件的基础。Struts 具有如此大的推动力，以至于开发团队宁愿整合 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架的特性，而不愿意转换成 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> MVC。没必要进行转换对您来说是一个好消息。Spring 架构允许您将 Struts 作为 Web 框架连接到基于 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的业务和持久层。最后的结果就是现在一切条件都具备了。</P>
<P>在接下来的小窍门中，您将会了解到三种将 Struts MVC 整合到 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架的方法。我将揭示每种方法的缺陷并且对比它们的优点。 一旦您了解到所有三种方法的作用，我将会向您展示一个令人兴奋的应用程序，这个程序使用的是这三种方法中我最喜欢的一种。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDADFWW><SPAN class=atitle><STRONG><FONT size=4>三个小窍门</FONT></STRONG></SPAN></A></P>
<P>接下来的每种整合技术（或者窍门）都有自己的优点和特点。我偏爱其中的一种，但是我知道这三种都能够加深您对 Struts 和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的理解。在处理各种不同情况的时候，这将给您提供一个广阔的选择范围。方法如下：</P>
<UL>
<LI>使用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE><FONT face=Courier size=2>ActionSupport</FONT></CODE> 类整合 Structs 
<LI>使用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE><FONT face=Courier size=2>DelegatingRequestProcessor</FONT></CODE> 覆盖 Struts 的 <CODE><FONT face=Courier size=2>RequestProcessor </FONT></CODE>
<LI>将 Struts <CODE><FONT face=Courier size=2>Action</FONT></CODE> 管理委托给 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架</LI></UL>
<P><A name=IDAAGWW><SPAN class=smalltitle><STRONG>装载应用程序环境</STRONG></SPAN></A></P>
<P>无论您使用哪种技术，都需要使用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE><FONT face=Courier size=2>ContextLoaderPlugin</FONT></CODE> 为 Struts 的 <CODE><FONT face=Courier size=2>ActionServlet</FONT></CODE> 装载 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 应用程序环境。就像添加任何其他插件一样，简单地向您的 struts-config.xml 文件添加该插件，如下所示：</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;plug-in className=
  "org.springframework.web.struts.ContextLoaderPlugIn"&gt;
    &lt;set-property property=
      "contextConfigLocation" value="/WEB-INF/beans.xml"/&gt;
 &lt;/plug-in&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>前面已经提到过，在 <A href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#download"><FONT color=#996699>下载</FONT></A> 部分，您能够找到这三个完全可使用的例子的完整源代码。每个例子都为一个书籍搜索应用程序提供一种不同的 Struts 和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的整合方法。您可以在这里看到例子的要点，但是您也可以下载应用程序以查看所有的细节。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAZGWW><SPAN class=atitle><STRONG><FONT size=4>窍门 1. 使用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 ActionSupport</FONT></STRONG></SPAN></A></P>
<P>手动创建一个 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 环境是一种整合 Struts 和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的最直观的方式。为了使它变得更简单，Spring 提供了一些帮助。为了方便地获得 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 环境，<CODE><FONT face=Courier size=2>org.springframework.web.struts.ActionSupport</FONT></CODE> 类提供了一个 <CODE><FONT face=Courier size=2>getWebApplicationContext()</FONT></CODE> 方法。您所做的只是从 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE><FONT face=Courier size=2>ActionSupport</FONT></CODE> 而不是 Struts <CODE><FONT face=Courier size=2>Action</FONT></CODE> 类扩展您的动作，如清单 1 所示：</P><BR><A name=IDAUHWW><B>清单 1. 使用 ActionSupport 整合 Struts</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">package ca.nexcel.books.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.springframework.context.ApplicationContext;
import org.springframework.web.struts.ActionSupport;

import ca.nexcel.books.beans.Book;
import ca.nexcel.books.business.BookService;

public class SearchSubmit extends ActionSupport {   <SPAN class=boldcode><STRONG>|(1)</STRONG></SPAN>


  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

    DynaActionForm searchForm = (DynaActionForm) form;
    String isbn = (String) searchForm.get("isbn");
		
    //the old fashion way
    //BookService bookService = new BookServiceImpl();
		
    ApplicationContext ctx = 
      getWebApplicationContext();    <SPAN class=boldcode><STRONG>|(2)</STRONG></SPAN>
    BookService bookService = 
      (BookService) ctx.getBean("bookService");   <SPAN class=boldcode><STRONG>|(3)</STRONG></SPAN>
        
  Book book = bookService.read(isbn.trim());

    if (null == book) {
      ActionErrors errors = new ActionErrors();
      errors.add(ActionErrors.GLOBAL_ERROR,new ActionError
        ("message.notfound"));
      saveErrors(request, errors);
      return mapping.findForward("failure") ;
  }

    request.setAttribute("book", book);
    return mapping.findForward("success");
  }
}
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>让我们快速思考一下这里到底发生了什么。在 (1) 处，我通过从 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE><FONT face=Courier size=2>ActionSupport</FONT></CODE> 类而不是 Struts 的 <CODE><FONT face=Courier size=2>Action</FONT></CODE> 类进行扩展，创建了一个新的 <CODE><FONT face=Courier size=2>Action</FONT></CODE>。在 (2) 处，我使用 <CODE><FONT face=Courier size=2>getWebApplicationContext()</FONT></CODE> 方法获得一个 <CODE><FONT face=Courier size=2>ApplicationContext</FONT></CODE>。为了获得业务服务，我使用在 (2) 处获得的环境在 (3) 处查找一个 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> bean。</P>
<P>这种技术很简单并且易于理解。不幸的是，它将 Struts 动作与 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架耦合在一起。如果您想替换掉 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a>，那么您必须重写代码。并且，由于 Struts 动作不在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的控制之下，所以它不能获得 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> AOP 的优势。当使用多重独立的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 环境时，这种技术可能有用，但是在大多数情况下，这种方法不如另外两种方法合适。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDA0A4W><SPAN class=atitle><STRONG><FONT size=4>窍门 2. 覆盖 RequestProcessor</FONT></STRONG></SPAN></A></P>
<P>将 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 从 Struts 动作中分离是一个更巧妙的设计选择。分离的一种方法是使用 <CODE><FONT face=Courier size=2>org.springframework.web.struts.DelegatingRequestProcessor</FONT></CODE> 类来覆盖 Struts 的 <CODE><FONT face=Courier size=2>RequestProcessor</FONT></CODE> 处理程序，如清单 2 所示：</P><BR><A name=IDANB4W><B>清单 2. 通过 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 DelegatingRequestProcessor 进行整合</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;

&lt;!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"&gt;

&lt;struts-config&gt;
 &lt;form-beans&gt;
    &lt;form-bean name="searchForm" 
      type="org.apache.struts.validator.DynaValidatorForm"&gt;
               &lt;form-property name="isbn"    type="java.lang.String"/&gt;
    &lt;/form-bean&gt;
  
  &lt;/form-beans&gt;

 &lt;global-forwards type="org.apache.struts.action.ActionForward"&gt;
     &lt;forward   name="welcome"                path="/welcome.do"/&gt;
     &lt;forward   name="searchEntry"            path="/searchEntry.do"/&gt;
     &lt;forward   name="searchSubmit"           path="/searchSubmit.do"/&gt;
 &lt;/global-forwards&gt;

 &lt;action-mappings&gt;
    &lt;action    path="/welcome" forward="/WEB-INF/pages/welcome.htm"/&gt;
    &lt;action    path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/&gt;
    &lt;action    path="/searchSubmit" 
               type="ca.nexcel.books.actions.SearchSubmit"
               input="/searchEntry.do"
               validate="true"
               name="searchForm"&gt;
              &lt;forward name="success" path="/WEB-INF/pages/detail.jsp"/&gt;
              &lt;forward name="failure" path="/WEB-INF/pages/search.jsp"/&gt;
    &lt;/action&gt;  

 &lt;/action-mappings&gt;

 &lt;message-resources parameter="ApplicationResources"/&gt;

 &lt;controller processorClass="org.springframework.web.struts.
   DelegatingRequestProcessor"/&gt; <SPAN class=boldcode><STRONG>|(1)</STRONG></SPAN>

 &lt;plug-in className="org.apache.struts.validator.ValidatorPlugIn"&gt;
    &lt;set-property property="pathnames" 
      value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/&gt;
 &lt;/plug-in&gt;


 &lt;plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"&gt;
    &lt;set-property property="csntextConfigLocation" value="/WEB-INF/beans.xml"/&gt;
 &lt;/plug-in&gt;
 
&lt;/struts-config&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>我利用了 <CODE><FONT face=Courier size=2>&lt;controller&gt;</FONT></CODE> 标记来用 <CODE><FONT face=Courier size=2>DelegatingRequestProcessor</FONT></CODE> 覆盖默认的 Struts <CODE><FONT face=Courier size=2>RequestProcessor</FONT></CODE>。下一步是在我的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置文件中注册该动作，如清单 3 所示：</P><BR><A name=IDAGC4W><B>清单 3. 在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置文件中注册一个动作</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
  "http://www.springframework.org/dtd/spring-beans.dtd"&gt;

&lt;beans&gt;
  &lt;bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/&gt;

  &lt;bean name="/searchSubmit" 
    class="ca.nexcel.books.actions.SearchSubmit"&gt; <SPAN class=boldcode><STRONG>|(1)</STRONG></SPAN>
     &lt;property name="bookService"&gt;
        &lt;ref bean="bookService"/&gt;
     &lt;/property&gt;
  &lt;/bean&gt;
&lt;/beans&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>注意：在 (1) 处，我使用名称属性注册了一个 bean，以匹配 struts-config 动作映射名称。<CODE><FONT face=Courier size=2>SearchSubmit</FONT></CODE> 动作揭示了一个 JavaBean 属性，允许 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 在运行时填充属性，如清单 4 所示：</P><BR><A name=IDAXC4W><B>清单 4. 具有 JavaBean 属性的 Struts 动作</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">package ca.nexcel.books.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import ca.nexcel.books.beans.Book;
import ca.nexcel.books.business.BookService;

public class SearchSubmit extends Action {
	
  private BookService bookService;
  public BookService getBookService() {
    return bookService;
  }

  public void setBookService(BookService bookService) { <SPAN class=boldcode><STRONG>| (1)</STRONG></SPAN>
    this.bookService = bookService; 
  } 

  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

    DynaActionForm searchForm = (DynaActionForm) form;
    String isbn = (String) searchForm.get("isbn");
		
  Book book = getBookService().read(isbn.trim());  <SPAN class=boldcode><STRONG>|(2)</STRONG></SPAN>

    if (null == book) {
      ActionErrors errors = new ActionErrors();
      errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("message.notfound"));
      saveErrors(request, errors);
      return mapping.findForward("failure") ;
  }

      request.setAttribute("book", book);
      return mapping.findForward("success");
  }

}
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>在清单 4 中，您可以了解到如何创建 Struts 动作。在 (1) 处，我创建了一个 JavaBean 属性。<CODE><FONT face=Courier size=2>DelegatingRequestProcessor</FONT></CODE>自动地配置这种属性。这种设计使 Struts 动作并不知道它正被 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 管理，并且使您能够利用 Sping 的动作管理框架的所有优点。由于您的 Struts 动作注意不到 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的存在，所以您不需要重写您的 Struts 代码就可以使用其他控制反转容器来替换掉 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a>。</P>
<P><CODE><FONT face=Courier size=2>DelegatingRequestProcessor</FONT></CODE> 方法的确比第一种方法好，但是仍然存在一些问题。如果您使用一个不同的 <CODE><FONT face=Courier size=2>RequestProcessor</FONT></CODE>，则需要手动整合 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE><FONT face=Courier size=2>DelegatingRequestProcessor</FONT></CODE>。添加的代码会造成维护的麻烦并且将来会降低您的应用程序的灵活性。此外，还有过一些使用一系列命令来代替 Struts <CODE><FONT face=Courier size=2>RequestProcessor</FONT></CODE> 的传闻。 这种改变将会对这种解决方法的使用寿命造成负面的影响。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAYD4W><SPAN class=atitle><STRONG><FONT size=4>窍门 3. 将动作管理委托给 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a></FONT></STRONG></SPAN></A></P>
<P>一个更好的解决方法是将 Strut 动作管理委托给 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a>。您可以通过在 <CODE><FONT face=Courier size=2>struts-config</FONT></CODE> 动作映射中注册一个代理来实现。代理负责在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 环境中查找 Struts 动作。由于动作在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的控制之下，所以它可以填充动作的 JavaBean 属性，并为应用诸如 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 AOP 拦截器之类的特性带来了可能。 </P>
<P>清单 5 中的 <CODE><FONT face=Courier size=2>Action</FONT></CODE> 类与清单 4 中的相同。但是 struts-config 有一些不同：</P><BR><A name=IDANE4W><B>清单 5. <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 整合的委托方法</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;

&lt;!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"&gt;

&lt;struts-config&gt;
 &lt;form-beans&gt;
    &lt;form-bean name="searchForm" 
      type="org.apache.struts.validator.DynaValidatorForm"&gt;
               &lt;form-property name="isbn"    type="java.lang.String"/&gt;
    &lt;/form-bean&gt;
  
  &lt;/form-beans&gt;

 &lt;global-forwards type="org.apache.struts.action.ActionForward"&gt;
     &lt;forward   name="welcome"                path="/welcome.do"/&gt;
     &lt;forward   name="searchEntry"            path="/searchEntry.do"/&gt;
     &lt;forward   name="searchSubmit"           path="/searchSubmit.do"/&gt;
 &lt;/global-forwards&gt;

 &lt;action-mappings&gt;
    &lt;action    path="/welcome" forward="/WEB-INF/pages/welcome.htm"/&gt;
    &lt;action    path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/&gt;
    &lt;action    path="/searchSubmit" 
             type="org.springframework.web.struts.DelegatingActionProxy" <SPAN class=boldcode><STRONG>|(1)</STRONG></SPAN>
             input="/searchEntry.do"
             validate="true"
             name="searchForm"&gt;
             &lt;forward name="success" path="/WEB-INF/pages/detail.jsp"/&gt;
             &lt;forward name="failure" path="/WEB-INF/pages/search.jsp"/&gt;
    &lt;/action&gt;  

 &lt;/action-mappings&gt;

 &lt;message-resources parameter="ApplicationResources"/&gt;


 &lt;plug-in className="org.apache.struts.validator.ValidatorPlugIn"&gt;
    &lt;set-property 
    property="pathnames" 
    value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/&gt;
 &lt;/plug-in&gt;


 &lt;plug-in 
    className="org.springframework.web.struts.ContextLoaderPlugIn"&gt;
    &lt;set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/&gt;
 &lt;/plug-in&gt;

 
&lt;/struts-config&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>清单 5 是一个典型的 struts-config.xml 文件，只有一个小小的差别。它注册 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 代理类的名称，而不是声明动作的类名，如（1）处所示。DelegatingActionProxy 类使用动作映射名称查找 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 环境中的动作。这就是我们使用 <CODE><FONT face=Courier size=2>ContextLoaderPlugIn</FONT></CODE> 声明的环境。</P>
<P>将一个 Struts 动作注册为一个 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> bean 是非常直观的，如清单 6 所示。我利用动作映射使用 <CODE><FONT face=Courier size=2>&lt;bean&gt;</FONT></CODE> 标记的名称属性（在这个例子中是 "<CODE><FONT face=Courier size=2>/searchSubmit</FONT></CODE>"）简单地创建了一个 bean。这个动作的 JavaBean 属性像任何 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> bean 一样被填充： </P><BR><A name=IDAIF4W><B>清单 6. 在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 环境中注册一个 Struts 动作</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
 "http://www.springframework.org/dtd/spring-beans.dtd"&gt;

&lt;beans&gt;
  &lt;bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/&gt;

  &lt;bean name="/searchSubmit"   
        class="ca.nexcel.books.actions.SearchSubmit"&gt;
     &lt;property name="bookService"&gt;
        &lt;ref bean="bookService"/&gt;
     &lt;/property&gt;
  &lt;/bean&gt;

&lt;/beans&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><FONT face="Lucida Console"><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></FONT></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><FONT face="Lucida Console"><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></FONT></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAOF4W><SPAN class=atitle><STRONG><FONT size=4>动作委托的优点</FONT></STRONG></SPAN></A></P>
<P>动作委托解决方法是这三种方法中最好的。Struts 动作不了解 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a>，不对代码作任何改变就可用于非 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 应用程序中。<CODE><FONT face=Courier size=2>RequestProcessor</FONT></CODE> 的改变不会影响它，并且它可以利用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> AOP 特性的优点。 </P>
<P>动作委托的优点不止如此。一旦让 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 控制您的 Struts 动作，您就可以使用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 给动作补充更强的活力。例如，没有 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的话，所有的 Struts 动作都必须是线程安全的。如果您设置 <CODE><FONT face=Courier size=2>&lt;bean&gt;</FONT></CODE> 标记的 singleton 属性为“false”，那么不管用何种方法，您的应用程序都将在每一个请求上有一个新生成的动作对象。您可能不需要这种特性，但是把它放在您的工具箱中也很好。您也可以利用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的生命周期方法。例如，当实例化 Struts 动作时，<CODE><FONT face=Courier size=2>&lt;bean&gt;</FONT></CODE> 标记的 init-method 属性被用于运行一个方法。类似地，在从容器中删除 bean 之前，destroy-method 属性执行一个方法。这些方法是管理昂贵对象的好办法，它们以一种与 Servlet 生命周期相同的方式进行管理。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAEG4W><SPAN class=atitle><STRONG><FONT size=4>拦截 Struts</FONT></STRONG></SPAN></A></P>
<P>前面提到过，通过将 Struts 动作委托给 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架而整合 Struts 和 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的一个主要的优点是：您可以将 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 AOP 拦截器应用于您的 Struts 动作。通过将 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 拦截器应用于 Struts 动作，您可以用最小的代价处理横切关注点。</P>
<P>虽然 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 提供很多内置拦截器，但是我将向您展示如何创建自己的拦截器并把它应用于一个 Struts 动作。为了使用拦截器，您需要做三件事：</P>
<OL>
<LI>创建拦截器。 
<LI>注册拦截器。 
<LI>声明在何处拦截代码。</LI></OL>
<P>这看起来非常简单的几句话却非常强大。例如，在清单 7 中，我为 Struts 动作创建了一个日志记录拦截器。 这个拦截器在每个方法调用之前打印一句话：</P><BR><A name=IDAUG4W><B>清单 7. 一个简单的日志记录拦截器</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">package ca.nexcel.books.interceptors;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class LoggingInterceptor implements MethodBeforeAdvice {

   public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("logging before!");
    }
}
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>这个拦截器非常简单。<CODE><FONT face=Courier size=2>before()</FONT></CODE> 方法在拦截点中每个方法之前运行。在本例中，它打印出一句话，其实它可以做您想做的任何事。下一步就是在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置文件中注册这个拦截器，如清单 8 所示：</P><BR><A name=IDADH4W><B>清单 8. 在 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 配置文件中注册拦截器</B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>
<FONT face="Lucida Console">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
  "http://www.springframework.org/dtd/spring-beans.dtd"&gt;

&lt;beans&gt;
  &lt;bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/&gt;

  &lt;bean name="/searchSubmit" 
        class="ca.nexcel.books.actions.SearchSubmit"&gt;
     &lt;property name="bookService"&gt;
        &lt;ref bean="bookService"/&gt;
     &lt;/property&gt;
  &lt;/bean&gt;

  &lt;!--  Interceptors --&gt; 
  &lt;bean name="logger"    
    class="ca.nexcel.books.interceptors.LoggingInterceptor"/&gt; <SPAN class=boldcode><STRONG>|(1)</STRONG></SPAN>

  &lt;!-- AutoProxies --&gt;
  &lt;bean name="loggingAutoProxy" 
        class="org.springframework.aop.framework.autoproxy.
          BeanNameAutoProxyCreator"&gt; <SPAN class=boldcode><STRONG>|(2)</STRONG></SPAN>
    &lt;property name="beanNames"&gt;
          &lt;value&gt;/searchSubmit&lt;/valuesgt; <SPAN class=boldcode><STRONG>|(3)</STRONG></SPAN>
    &lt;/property&gt;
    &lt;property name="interceptorNames"&gt;
        &lt;list&gt;
          &lt;value&gt;logger&lt;/value&gt; <SPAN class=boldcode><STRONG>|(4)</STRONG></SPAN>
        &lt;/list&gt;
    &lt;/property&gt;
   &lt;/bean&gt;

&lt;/beans&gt;
</FONT></CODE></PRE></TD></TR></TBODY></TABLE><BR>
<P>您可能已经注意到了，清单 8 扩展了 <A href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#code6"><FONT color=#996699>清单 6</FONT></A> 中所示的应用程序以包含一个拦截器。具体细节如下：</P>
<UL>
<LI>在 (1) 处，我注册了这个拦截器。 
<LI>在 (2) 处，我创建了一个 bean 名称自动代理，它描述如何应用拦截器。还有其他的方法定义拦截点，但是这种方法常见而简便。 
<LI>在 (3) 处，我将 Struts 动作注册为将被拦截的 bean。如果您想要拦截其他的 Struts 动作，则只需要在 "beanNames" 下面创建附加的 <CODE><FONT face=Courier size=2>&lt;value&gt;</FONT></CODE> 标记。 
<LI>在 (4) 处，当拦截发生时，我执行了在 (1) 处创建的拦截器 bean 的名称。这里列出的所有拦截器都应用于“beanNames”。</LI></UL>
<P>就是这样。就像这个例子所展示的，将您的 Struts 动作置于 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架的控制之下，为处理您的 Struts 应用程序提供了一系列全新的选择。在本例中，使用动作委托可以轻松地利用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 拦截器提高 Struts 应用程序中的日志记录能力。</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=IDAFI4W><SPAN class=atitle><STRONG><FONT size=4>结束语</FONT></STRONG></SPAN></A></P>
<P>在本文中，您已经学习了将 Struts 动作整合到 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架中的三种窍门。使用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的 <CODE><FONT face=Courier size=2>ActionSupport</FONT></CODE> 来整合 Struts（第一种窍门中就是这样做的）简单而快捷，但是会将 Struts 动作与 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架耦合在一起。如果您需要将应用程序移植到一个不同的框架，则需要重写代码。第二种解决方法通过委托 <CODE><FONT face=Courier size=2>RequestProcessor</FONT></CODE> 巧妙地解开代码的耦合，但是它的可扩展性不强，并且当 Struts 的 <CODE><FONT face=Courier size=2>RequestProcessor</FONT></CODE> 变成一系列命令时，这种方法就持续不了很长时间。第三种方法是这三种方法中最好的：将 Struts 动作委托给 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架可以使代码解耦，从而使您可以在您的 Struts 应用程序中利用 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 的特性（比如日志记录拦截器）。</P>
<P>三种 Struts-Spring 整合窍门中的每一种都被实现成一个完整可用的应用程序。请参阅 <A href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#download"><FONT color=#996699>下载</FONT></A> 部分仔细研究它们。</P><BR><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><SPAN class=atitle><A name=download><STRONG><FONT size=4>下载</FONT></STRONG></A></SPAN></P>
<TABLE class=data-table-1 cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TH>描述</TH>
<TH>名字</TH>
<TH style="TEXT-ALIGN: right">大小</TH>
<TH>&nbsp;下载方法</TH></TR>
<TR>
<TD class=tb-row>ActionSupport sample code</TD>
<TD noWrap>j-sr2-actionsupport.zip</TD>
<TD style="TEXT-ALIGN: right" noWrap>5 MB</TD>
<TD noWrap>&nbsp;<A class=fbox href="ftp://www6.software.ibm.com/software/developer/library/j-sr2-actionsupport.zip"><B><FONT face=Verdana color=#5c81a7>FTP</FONT></B></A></TD></TR>
<TR>
<TD class=tb-row>RequestProcessor sample code</TD>
<TD noWrap>j-sr2-requestprocessor.zip</TD>
<TD style="TEXT-ALIGN: right" noWrap>5 MB</TD>
<TD noWrap>&nbsp;<A class=fbox href="ftp://www6.software.ibm.com/software/developer/library/j-sr2-requestprocessor.zip"><B><FONT face=Verdana color=#5c81a7>FTP</FONT></B></A></TD></TR>
<TR>
<TD class=tb-row>Delegate sample code</TD>
<TD noWrap>j-sr2-delegate.zip</TD>
<TD style="TEXT-ALIGN: right" noWrap>5 MB</TD>
<TD noWrap>&nbsp;<A class=fbox href="ftp://www6.software.ibm.com/software/developer/library/j-sr2-delegate.zip"><B><FONT face=Verdana color=#5c81a7>FTP</FONT></B></A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR vAlign=top>
<TD colSpan=5><FONT face=Verdana color=#5c81a7><IMG height=12 alt="" src="http://www.ibm.com/i/c.gif" width=12 border=0></FONT></TD></TR>
<TR>
<TD><FONT face=Verdana color=#5c81a7><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/fw.gif" width=16></FONT></TD>
<TD><A class=fbox href="http://www-128.ibm.com/developerworks/cn/whichmethod.html"><FONT face=Verdana color=#5c81a7>关于下载方法的信息</FONT></A></TD>
<TD><FONT face=Verdana color=#5c81a7><IMG height=1 alt="" src="http://www.ibm.com/i/c.gif" width=50></FONT></TD>
<TD><FONT face=Verdana color=#5c81a7><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/sout.gif" width=16></FONT></TD>
<TD><A class=fbox href="http://www.adobe.com/products/acrobat/readstep2.html"><FONT face=Verdana color=#5c81a7>获取 Adobe® Reader®</FONT></A></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><FONT face=Verdana color=#5c81a7><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></FONT></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><FONT face=Verdana color=#5c81a7><IMG height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><BR></FONT></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699>回页首</FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=resources><SPAN class=atitle><STRONG><FONT size=4>参考资料 </FONT></STRONG></SPAN></A></P><STRONG>学习</STRONG><BR>
<UL>
<LI>您可以参阅本文在 developerWorks 全球站点上的 <A href="http://www-128.ibm.com/developerworks/java/library/j-sr2.html" target=_blank><FONT color=#5c81a7>英文原文</FONT></A>。<BR><BR>
<LI><A href="http://www.ibm.com/developerworks/library/wa-spring1/"><FONT color=#5c81a7><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 系列</FONT></A>（Naveen Balani，developerWorks，2005 年 6 到 9 月）：对 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架的组件和特性（包括 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> AOP）的分为四部分的介绍。<BR><BR>
<LI><A href="http://www.ibm.com/developerworks/web/library/wa-struts/index.html"><FONT color=#5c81a7>Best practices in Struts development</FONT></A>（Palaniyappan Thiagarajan 和 Pagadala Suresh，developerWorks，2004 年 6 月）：概述了 Struts <CODE><FONT face=Courier size=2>Action</FONT></CODE> 组件。<BR><BR>
<LI><A href="http://www.martinfowler.com/articles/injection.html"><FONT color=#5c81a7>Inversion of Control Containers and the Dependency Injection pattern</FONT></A>（Martin Fowler，2004 年 1 月）：介绍 IOC 容器。<BR><BR>
<LI><A href="http://www.theserverside.com/articles/article.tss?l=SpringFramework"><FONT color=#5c81a7>Introduction to the <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> Framework</FONT></A>（Rod Johnson，The ServerSide，2005 年 5 月）：Spring 的创立者解释了为什么 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架很重要。<BR><BR>
<LI><A href="http://www.manning.com/franciscus"><FONT color=#5c81a7>Struts Recipes</FONT></A>（George Franciscus 和 Danilo Gurovich；Manning，2004 年）：关于 Struts 窍门和最佳实践的一个流行的纲要。<BR><BR>
<LI><A href="http://www.manning.com/husted"><FONT color=#5c81a7>Struts In Action</FONT></A>（Ted Husted、Cedric Dumoulin、George Franciscus、David Winterfeldt；Manning，2002 年）：针对专业 Struts 开发人员的综合资源。<BR><BR>
<LI><A href="http://www.manning.com/walls2"><FONT color=#5c81a7><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> In Action</FONT></A>（Craig Walls 和 Ryan Breidenbach；Manning，2005 年）：一本用于开发人员的 <a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 资源书籍。<BR><BR>
<LI><A href="http://www.ibm.com/developerworks/cn/java/"><FONT color=#5c81a7>Java 技术专区</FONT></A>：找到关于 Java 编程各个方面的文章。<BR><BR></LI></UL><BR><B>获得产品和技术</B><BR>
<UL>
<LI><A href="http://struts.apache.org/"><FONT color=#5c81a7>Struts 框架</FONT></A>：Apache Software Foundation 的一个项目。<BR><BR>
<LI><A href="http://www.springframework.org/"><FONT color=#5c81a7><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> 框架</FONT></A>：一个分层的 Java 企业应用程序框架。<BR><BR></LI></UL><BR><B>讨论</B><BR>
<UL>
<LI>加入本文的<A href="javascript:void forumWindow()"><FONT color=#5c81a7>论坛</FONT></A> 。(您也可以通过点击文章顶部或者底部的论坛链接参加讨论。)<BR><BR>
<LI><A href="http://www.ibm.com/developerworks/blogs/"><FONT color=#5c81a7>developerWorks blogs</FONT></A>：加入 developerWorks 社区。</LI></UL><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><BR></TD>
<TD vAlign=top align=right><A class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-sr2.html#main"><B><FONT face=Verdana color=#996699></FONT></B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<P><A name=author><SPAN class=atitle><STRONG><FONT size=4>关于作者</FONT></STRONG></SPAN></A></P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD colSpan=2><STRONG><FONT size=4><IMG height=5 alt="" src="http://www.ibm.com/i/c.gif" width="100%"></FONT></STRONG></TD></TR>
<TR vAlign=top align=left>
<TD>
<P><STRONG><FONT size=4></FONT></STRONG></P></TD>
<TD>
<P>George Franciscus 是 Java 公司的一名顾问和 Struts 方面的权威。他是 Manning 出版的 <A href="http://www.manning.com/franciscus"><FONT color=#5c81a7>Struts Recipes</FONT></A> 和 <A href="http://www.manning.com/husted"><FONT color=#5c81a7>Struts in Action</FONT></A> 的合著者。 George 通过 <A href="http://www.nexcel.ca/"><FONT color=#5c81a7>nexcel.ca</FONT></A> 提供有关技术和管理方面的咨询服务。</P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><img src ="http://www.blogjava.net/shmily432685/aggbug/19069.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shmily432685/" target="_blank">闵毓</a> 2005-11-09 23:53 <a href="http://www.blogjava.net/shmily432685/archive/2005/11/09/19069.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Myeclipse平台struts+hibernate+spring项目开发示例</title><link>http://www.blogjava.net/shmily432685/archive/2005/11/05/18233.html</link><dc:creator>闵毓</dc:creator><author>闵毓</author><pubDate>Fri, 04 Nov 2005 18:32:00 GMT</pubDate><guid>http://www.blogjava.net/shmily432685/archive/2005/11/05/18233.html</guid><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.blogjava.net/shmily432685/archive/2005/11/05/18233.html'>阅读全文</a><img src ="http://www.blogjava.net/shmily432685/aggbug/18233.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shmily432685/" target="_blank">闵毓</a> 2005-11-05 02:32 <a href="http://www.blogjava.net/shmily432685/archive/2005/11/05/18233.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用Spring邮件抽象层发送简单邮件(通过自己编写，更新后发布)</title><link>http://www.blogjava.net/shmily432685/archive/2005/11/04/18177.html</link><dc:creator>闵毓</dc:creator><author>闵毓</author><pubDate>Fri, 04 Nov 2005 09:49:00 GMT</pubDate><guid>http://www.blogjava.net/shmily432685/archive/2005/11/04/18177.html</guid><description><![CDATA[<P><FONT size=2></P>
<P>&nbsp; Spring提供了一个发送电子邮件的高级抽象层，它向用户屏蔽了底层邮件系统的一些细节，同时负责低层次的代表客户端的资源处理。Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender和 封装了简单邮件的属性如from, to,cc, subject, text的值对象叫做SimpleMailMessage。 <BR>1、我们定义一个发送邮件的接口:OrderManager.java <BR></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">1</SPAN>&nbsp;<SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">interface</SPAN><SPAN style="COLOR: #000000">&nbsp;OrderManager&nbsp;</SPAN><SPAN style="COLOR: #0000ff">extends</SPAN><SPAN style="COLOR: #000000">&nbsp;BaseManager{<BR></SPAN><SPAN style="COLOR: #008080">2</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">3</SPAN>&nbsp;<SPAN style="COLOR: #008000">*email,要发送的邮件地址;<BR></SPAN><SPAN style="COLOR: #008080">4</SPAN>&nbsp;<SPAN style="COLOR: #008000">*Code:激活码<BR></SPAN><SPAN style="COLOR: #008080">5</SPAN>&nbsp;<SPAN style="COLOR: #008000"></SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">6</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;placeOrder(String&nbsp;email);<BR></SPAN><SPAN style="COLOR: #008080">7</SPAN>&nbsp;<SPAN style="COLOR: #000000">}</SPAN></DIV>
<P>2、我们需要对该接口进行实现的方法:OrderManagerImpl.java</P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN>&nbsp;<SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;javax.mail.Message;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;javax.mail.MessagingException;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;javax.mail.internet.InternetAddress;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;javax.mail.internet.MimeMessage;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.springframework.mail.MailException;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.springframework.mail.javamail.JavaMailSender;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.springframework.mail.javamail.MimeMessagePreparator;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;service.OrderManager;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN>&nbsp;&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">11</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;OrderManagerImpl&nbsp;</SPAN><SPAN style="COLOR: #0000ff">extends</SPAN><SPAN style="COLOR: #000000">&nbsp;BaseManagerImpl&nbsp;</SPAN><SPAN style="COLOR: #0000ff">implements</SPAN><SPAN style="COLOR: #000000">&nbsp;OrderManager&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">13</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;JavaMailSender&nbsp;mailsender;<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;MyMailMessage&nbsp;message;<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">16</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">17</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;setMessage(CityMailMessage&nbsp;message)<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.message&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;message;<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;setMailsender(JavaMailSender&nbsp;mailsender)&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">22</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.mailsender&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;mailsender;<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;placeOrder(</SPAN><SPAN style="COLOR: #0000ff">final</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;email</SPAN><SPAN style="COLOR: #000000">)&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">26</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">27</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MimeMessagePreparator&nbsp;preparator&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;MimeMessagePreparator()&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">28</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;prepare(MimeMessage&nbsp;mimeMessage)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">throws</SPAN><SPAN style="COLOR: #000000">&nbsp;MessagingException&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">29</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mimeMessage.setRecipient(Message.RecipientType.TO,&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">30</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;InternetAddress(email));<BR></SPAN><SPAN style="COLOR: #008080">31</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mimeMessage.setFrom(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;InternetAddress(message.getFrom()));<BR></SPAN><SPAN style="COLOR: #008080">32</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/**转换编码为GBK*/<BR></SPAN><SPAN style="COLOR: #008080">33</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mimeMessage.setSubject(message.getSubject(),</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">GBK</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">36</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mimeMessage.setText(email</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&lt;br&gt;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">message.getSubject()</SPAN><SPAN style="COLOR: #000000">+message.getText()</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">GBK</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">37</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">38</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">39</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<BR></SPAN><SPAN style="COLOR: #008080">40</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">41</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mailsender.send(preparator);<BR></SPAN><SPAN style="COLOR: #008080">42</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">43</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">(MailException&nbsp;ex)&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">44</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">log&nbsp;it&nbsp;and&nbsp;go&nbsp;on</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">45</SPAN>&nbsp;<SPAN style="COLOR: #008000"></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(ex.getMessage());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">46</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">47</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">48</SPAN>&nbsp;<SPAN style="COLOR: #000000">}</SPAN></DIV><BR>
<P>3、spring配置发送email的applicationContext-email.xml</P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN>&nbsp;<SPAN style="COLOR: #000000">&lt;?</SPAN><SPAN style="COLOR: #000000">xml&nbsp;version</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">1.0</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;encoding</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">UTF-8</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">?&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #000000">&lt;!</SPAN><SPAN style="COLOR: #000000">DOCTYPE&nbsp;beans&nbsp;PUBLIC&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">-//SPRING//DTD&nbsp;BEAN//EN</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">http://www.springframework.org/dtd/spring-beans.dtd</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">beans</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">bean&nbsp;id</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mailSender</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">org.springframework.mail.javamail.JavaMailSenderImpl</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">property&nbsp;name</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">host</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">smtp.</SPAN><SPAN style="COLOR: #000000">163</SPAN><SPAN style="COLOR: #000000">.com</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">property</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">10</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">property&nbsp;name</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">username</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">11</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">username</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">12</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">property</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">13</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">property&nbsp;name</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">password</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">14</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">password</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">15</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">property</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">16</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">property&nbsp;name</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">javaMailProperties</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">17</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">props</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">18</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">prop&nbsp;key</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mail.smtp.auth</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">prop</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">19</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">prop&nbsp;key</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mail.smtp.timeout</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">25000</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">prop</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">20</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">props</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">21</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">property</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">22</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">bean</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">23</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">24</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">bean&nbsp;id</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mailMessage</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">org.springframework.mail.SimpleMailMessage</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">25</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">property&nbsp;name</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">from</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">26</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;Email</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">27</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">property</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">28</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">property&nbsp;name</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">subject</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">29</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;标题</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">30</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">property</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">31</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">property&nbsp;name</SPAN><SPAN style="COLOR: #000000">="text</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">32</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">&gt;内容</SPAN><SPAN style="COLOR: #008000">&lt;/value&gt;</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">33</SPAN>&nbsp;<SPAN style="COLOR: #008000"></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">property</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">46</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">bean</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">47</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">48</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">bean&nbsp;id</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">orderManager</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">cn.cityyouth.service.impl.OrderManagerImpl</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">49</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">property&nbsp;name</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mailsender</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">50</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">ref&nbsp;bean</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mailSender</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">51</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">property</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">52</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">property&nbsp;name</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">message</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">53</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">ref&nbsp;bean</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mailMessage</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">/&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">54</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">property</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">55</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">bean</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">56</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">57</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">beans</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN></DIV>
<P>4、最后配置自己的jsp页面以及action</P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN>&nbsp;<SPAN style="COLOR: #0000ff">package</SPAN><SPAN style="COLOR: #000000">&nbsp;cn.cityyouth.web.action;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;javax.servlet.http.HttpServletRequest;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;javax.servlet.http.HttpServletResponse;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.struts.action.ActionForm;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.struts.action.ActionForward;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.struts.action.ActionMapping;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.struts.action.ActionMessage;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.struts.action.ActionMessages;<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;com.test.service.OrderManager;<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">12</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;SendMailAction&nbsp;</SPAN><SPAN style="COLOR: #0000ff">extends</SPAN><SPAN style="COLOR: #000000">&nbsp;BaseAction&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">14</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">15</SPAN>&nbsp;<SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Method&nbsp;execute<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN>&nbsp;<SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN>&nbsp;<SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</SPAN><SPAN style="COLOR: #808080">@param</SPAN><SPAN style="COLOR: #008000">&nbsp;mapping<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN>&nbsp;<SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</SPAN><SPAN style="COLOR: #808080">@param</SPAN><SPAN style="COLOR: #008000">&nbsp;form<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN>&nbsp;<SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</SPAN><SPAN style="COLOR: #808080">@param</SPAN><SPAN style="COLOR: #008000">&nbsp;request<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN>&nbsp;<SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</SPAN><SPAN style="COLOR: #808080">@param</SPAN><SPAN style="COLOR: #008000">&nbsp;response<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN>&nbsp;<SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</SPAN><SPAN style="COLOR: #808080">@return</SPAN><SPAN style="COLOR: #008000">&nbsp;ActionForward<BR></SPAN><SPAN style="COLOR: #008080">22</SPAN>&nbsp;<SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">23</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;ActionForward&nbsp;execute(ActionMapping&nbsp;mapping,&nbsp;ActionForm&nbsp;form,<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpServletRequest&nbsp;request,&nbsp;HttpServletResponse&nbsp;response)&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderManager&nbsp;omi</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">(OrderManager)</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.getBean(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">orderManager</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">26</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;useremail</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">123@163.com</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">27</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;omi.placeOrder(useremail);<BR></SPAN><SPAN style="COLOR: #008080">28</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">29</SPAN>&nbsp;<SPAN style="COLOR: #000000">}</SPAN></DIV>
<P>到此所有的开发以结束。<BR></P>
<P>Sring邮件抽象层的主要包是：org.springframework.mail 包。它包含叫MailSender为发送邮件的核心接口和包含简单邮件属性例如from,to,cc,subject,text叫SimpleMailMessage的值对象. 这个包也包含一个检查异常的层次，它支持一个更高级别的抽象超过低级别的邮件系统异常伴随根异常存在MailException. 请参考JavaDocs为更多的信息杂邮件异常层次。</P>
<P><a title="心得与体会" href="http://www.blogjava.net/shmily432685" >spring in action</a> also provides a sub-interface of MailSender for specialized JavaMail features such as MIME messages, namely org.springframework.mail.javamail.JavaMailSender It also provides a callback interface for preparation of JavaMail MIME messages, namely org.springframework.mail.javamail.MimeMessagePreparator</P>
<P>Spring也支持一个MailSender的专用于JavaMail特征例如MIME消息子接口，命名为org.springframework.javamail.JavaMailerSener。它也支持一个为JavaMail MIME信息的准备回调接口，命名为org.springframework.mail.JavaMail.MimeMessagePreparator.<BR>&nbsp;</P></FONT><img src ="http://www.blogjava.net/shmily432685/aggbug/18177.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shmily432685/" target="_blank">闵毓</a> 2005-11-04 17:49 <a href="http://www.blogjava.net/shmily432685/archive/2005/11/04/18177.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>