第二部分:
1.DWR的配置:
DWR是Java一个比较常用的Ajax框架,与Spring集成更是得心应手,可以非常方便的操作注册在Spring配置文件中的Bean。这我们导入DWR包后,就可以在web.xml里面配置它了。
web.xml关于DWR配置。
xml 代码
- <servlet>
- <servlet-name>dwr-invokerservlet-name>
- <servlet-class>
- org.directwebremoting.servlet.DwrServlet
- servlet-class>
- <init-param>
- <param-name>debugparam-name>
- <param-value>infoparam-value>
- init-param>
- <init-param>
- <param-name>maxWaitAfterWriteparam-name>
- <param-value>500param-value>
- init-param>
- <load-on-startup>1load-on-startup>
- servlet>
-
- <servlet-mapping>
- <servlet-name>dwr-invokerservlet-name>
- <url-pattern>/dwr/*url-pattern>
- servlet-mapping>
DWR还有一个叫dwr.xml文件,放在WEB-INF下,里面都是配置DWR与JS交互使用的Bean或Entity。 当然DWR也支持Anotation,可以在web.xml配置。但我们这个例子使用的是xml配置。
以下是dwr.xml代码:
xml 代码
- <!---->xml version="1.0" encoding="UTF-8"?>
- <!---->
- "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
- "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
- <dwr>
- <allow>
- <create creator="spring" javascript="ArticleService">
- <param name="beanName" value="articleService" />
- create>
-
-
- allow>
- dwr>
这个ArticleService就是我们要通过JS调用Spring注册的Bean。
现在可以看看这个源文件:
java 代码
-
-
-
- package com.leo.service.impl;
-
- import java.util.Date;
- import java.util.List;
-
- import org.apache.log4j.Logger;
-
- import com.leo.po.Article;
- import com.leo.po.Log;
- import com.leo.po.Mail;
- import com.leo.service.IArticleService;
- import com.leo.service.ILogService;
- import com.leo.service.IMailService;
- import com.leo.util.JavaMailSenderUtil;
- import com.leo.util.RegEx;
-
-
-
-
-
- public class ArticleServiceImpl extends GenericServiceImpl
- implements IArticleService {
-
- private static Logger logger = Logger.getLogger(ArticleServiceImpl.class);
-
- private IMailService mailService;
-
- private ILogService logService;
-
- private JavaMailSenderUtil javaMailSenderUtil;
-
- public JavaMailSenderUtil getJavaMailSenderUtil() {
- return javaMailSenderUtil;
- }
-
- public void setJavaMailSenderUtil(JavaMailSenderUtil javaMailSenderUtil) {
- this.javaMailSenderUtil = javaMailSenderUtil;
- }
-
- public ILogService getLogService() {
- return logService;
- }
-
- public void setLogService(ILogService logService) {
- this.logService = logService;
- }
-
- public IMailService getMailService() {
- return mailService;
- }
-
- public void setMailService(IMailService mailService) {
- this.mailService = mailService;
- }
-
- public ArticleServiceImpl(Class<article></article> type) {
- super(type);
- }
-
- public int storeByURL() throws Exception {
- int count = RegEx.doRegEx();
-
- Article article = new Article();
- article.setCount(count);
- article.setCreateDate(new Date());
- this.store(article);
-
-
- List<mail></mail> mails = mailService.findByAll();
- logger.info("开始群发邮件........");
- for (Mail mail : mails) {
- Log log = new Log();
- log.setCreateDate(new Date());
- log.setLog("正在向 [" + mail.getName() + "] 发送邮件......");
- log.setMail(mail);
- javaMailSenderUtil.sendMail(mail, count);
- this.logService.store(log);
- }
- logger.info("群发结束邮件........");
-
- return count;
-
- }
-
- }
其实主要就一个很简单的方法:收集信息 -> 发邮件 -> 记录日志 -> 结束。 大家会问我为什么不用Action来实现?因为需要里要每5分钟重新采集一次信息后发送邮件,通过使用Quartz 也能实现,但本人对DWR更熟悉点,所以这里使用的是DWR版本了。
这个时候,相应的页面为:
xml 代码
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ include file="include/includes.jsp" %>
- <html>
- <head>
- <s:head/>
-
-
- <title>欢迎进入后台系统title>
-
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-
- <style type="text/css">
-
- body,td,th {
- font-size: 12px;
- }
-
- a {
- font-size: 12px;
- }
-
- a:link {
- text-decoration: none;
- }
-
- a:visited {
- text-decoration: none;
- }
-
- a:hover {
- text-decoration: underline;
- }
-
- a:active {
- text-decoration: none;
- }
-
- style>
-
- <script type='text/javascript' src='dwr/engine.js'>script>
- <script type='text/javascript' src='dwr/util.js'>script>
- <script type='text/javascript' src='dwr/interface/ArticleService.js'>script>
- <script>
- /** 初始化*/
- function init(){
- dwr.util.useLoadingMessage("正在加载....");
- }
-
- /** 发信函数*/
- function doWork(){
- ArticleService.storeByURL(showResult);
- }
-
- /** 每5分钟回调发信函数*/
- function showResult(count){
- alert("这一次取得的结果为:" + count + " " + new Date());
- $("result").innerHTML = "这一次取得的结果为:" + count + " " + new Date();
- setTimeout("doWork()",1000 * 60 * 5);
- }
-
- script>
-
- head>
-
- <body onLoad="init();">
-
-
- <div style="float: left; width: 1000px; height: 600px; margin: 0px">
- <div style="float: left; width: 300px; height: 600px">
- <ul>
- <li>
- 欢迎进入后台系统.
- li>
- <li>
- <a href="#" onClick="doWork();">运行程序.a>
- li>
- ul>
- div>
- <div style="float: left; width: 700px; height: 600px">
- 说明:每5分钟遍历一下页面<br />
- (http://post.baidu.com/f?z=0&ct=318767104&lm=11&sc=0&rn=50&tn=baiduKeywordSearch&rs3=0&rs4=0&word=%D2%EC%C8%CB%B0%C1%CA%C0%C2%BC&pn=50), <br />
- 请统计页面中"《"和"》"之间有多少个"异人"这两个字,<br />
- 将这数值弹出一个alert并且发信给指定的邮件地址 获取页面内所有地方的以"<< >>"为标示的这个符号代表书名号,
- 获取之中的内容 <<使用正则表达式>> 功能点: <br />
- 1)URL抓取页面 <br />
- 2)正则表达式匹配 <br />
- 3)邮件系统调用 请注意设计思路和代码规范 <br />
-
- <p>p>
- <div id="result">这里显示每次抓取的结果......div>
-
- div>
- div>
- body>
- html>
非常简单的页面。 通过JS函数的setTimeout("doWork()",1000 * 60 * 5) 很容易每5分钟来重发一次请求。
2.JavaMail的配置:
一旦使用了Spring似乎感觉不到JavaMail需要做更多事了。确实,Spring已经简化了很多JavaMail的配置。
xml 代码
- <bean id="mailSender"
- class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host" value="${mail.smtp}" />
- <property name="username" value="${mail.username}" />
- <property name="password" value="${mail.password}" />
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.auth">trueprop>
- props>
- property>
- bean>
使用的话,只需要简单注入一下,就OK了。我把这个写在util包下了。
java 代码
- package com.leo.util;
-
- import org.apache.log4j.Logger;
- import org.springframework.mail.MailException;
- import org.springframework.mail.MailSender;
- import org.springframework.mail.SimpleMailMessage;
-
- import com.leo.po.Mail;
-
- public class JavaMailSenderUtil {
-
- private static Logger logger = Logger.getLogger(JavaMailSenderUtil.class);
-
- private static final String MAIL_FROM = "kyo100900@163.com";
-
- private MailSender mailSender;
-
-
-
-
-
-
-
-
-
- public void sendMail(Mail mail, int count) {
-
- logger.info("-----------发送邮件!---------");
- SimpleMailMessage msg = new SimpleMailMessage();
- String subject = "亲爱的:" + mail.getName() + " ,我将告诉你《异人傲世录》的情况。";
- String text = "帖子中,包括《*异人*》的共有:" + count + " 个";
-
- String email = mail.getEmail();
- logger.info(subject);
- logger.info(text);
- logger.info("发送给:" + email);
-
- msg.setSubject(subject);
- msg.setText(text);
- msg.setTo(email);
- msg.setFrom(MAIL_FROM);
-
- try {
- logger.info("mailSender为空:" + mailSender == null);
- mailSender.send(msg);
- } catch (MailException e) {
- e.printStackTrace();
- }
- logger.info("-----------发送成功!---------");
- }
-
- public MailSender getMailSender() {
- return mailSender;
- }
-
- public void setMailSender(MailSender mailSender) {
- this.mailSender = mailSender;
- }
-
- }
这个例子远远不够,还有很多可以完善与改进的地方,这个例子只是想让那些初学者如何比较全面的了解SSH之间的相互工作。零散的学习后,还需要系统的整合在一起。