回顾:
1.用户—角色
Span 标签的样式:display:inline-block;width:200px
2. 角色…模块
zTree树展示权限列表
实际项目用得多,zTree的使用步骤:
1.导入js/css
2.jsp中引入js/css
3.组织 json 数据,手动拼接 json 串[{id:"”,pld:"”,name:〃”,checked:“true|false”}]
4.将这个json串向浏览器输出:response对象手动输出
5.客户端可以发出ajax请求,来得到这个json串
$,ajax({url:"",dataType:“text”,type:"GET”,success:function(){} });
注意多对多操作时,如果要操作中间表的关系,不需要加cascade
3.struts2异常处理框架
struts.xml文件中配置:
〈global-results〉
/WEB-INF/pages/error.jsp
<exception-mapping exception=,zcn.itcast.jk.exception.SysException,z result= “error”/〉
</ global-exception-mappings>
Action类中
throw new SysException。、'7);
一.什么是 JavaMail
JavaMail是提供给开发人员在应用程序中实现邮件发送和接收功能而提供的一 套标准开发类库,支持常用的邮件协议,如SMTP、POP3、IMAP ,开发人员使用 JavaMail编写邮件程序时,无需考虑底层的通信细节(Socket) , JavaMail也提供了能够 创建出各种复杂MIME格式的邮件内容的API。使用JavaMail ,我们可以实现类似 OutLook、FoxMail 的软件。
二.JavaMail的基本概述
1.邮件开发的相关协议
SMTP : Simple Message Transfer Protocal 发送协议默认端口 : 25
POP : Post Office Protocal邮局协议。POP3这个版本用的最多,接收协议默认端口 : 110
2 .邮件发送接收的过程分析
三.邮件开发的准备工作
1.申请邮箱
我这里申请的是新浪、网易邮箱,进入后开通P0P3/SMTP服务
P0P3/SMTP 服务
2.引入 JavaMail
在jk2601_parentT程的pom.xml中添加如下依赖
<!-- Javamail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
为了让Spring与JavaMaii集成,还需要在jk260—parent工程的pom.xml引入如下依 赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
如果是web项目,引入如下jar包
四.传统的邮件开发
//再开启一个新的线程完成邮件发送功能
Thread th = new Thread(new Runnable() {
public void run() {
try {
MailUtil.sendMessage(entity.getUserinfo().getEmail(), "新员工入职的系统账户通知", "欢迎您加入本集团,您的用户名:"+entity.getUserName()+",初始密码:"+SysConstant.DEFAULT_PASS);
} catch (Exception e) {
e.printStackTrace();
}
}
});
th.start();
五.邮件发送的工具类的提取 MailUtil.sendMessage()
public class MailUtil {
public static void sendMessage(String toAddr,String subject,String content) throws Exception{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.163.com");//指定邮件的发送服务器地址
props.put("mail.smtp.auth", "true");//服务器是否要验证用户的身份信息
Session session = Session.getInstance(props);//得到Session
session.setDebug(true);//代表启用debug模式,可以在控制台输出smtp协议应答的过程
//创建一个MimeMessage格式的邮件
MimeMessage message = new MimeMessage(session);
//设置发送者
Address fromAddress = new InternetAddress("itheima14@163.com");//邮件地址
message.setFrom(fromAddress);//设置发送的邮件地址
//设置接收者
Address toAddress = new InternetAddress(toAddr);//邮件地址
message.setRecipient(RecipientType.TO, toAddress);//设置接收者的地址
//设置邮件的主题
message.setSubject(subject);
//设置邮件的内容
message.setText(content);
//保存邮件
message.saveChanges();
//得到发送邮件的火箭
Transport transport = session.getTransport("smtp");
//火箭连接到服务器上
transport.connect("smtp.163.com","itheima14","iamsorry");
//火箭点火,发送
transport.sendMessage(message, message.getAllRecipients());
//关闭通道
transport.close();
}
}
六.使用JavaMail实现员工登录信息的发送
业务需求:在员工信息添加时,同时需要向员工发送一封通知的邮件,以进行提示!
1.修改PO类与映射文件
2.修改 jUserCreate.jsp 页面
3,进入业务逻辑层UserServicelmpI中
七.JavaMail与Spring集成开发
Spring邮件抽象层的主要包为org. springframework. mai
它包括了发送电子邮件的 主要接口 MailSender ,和其实现类SimpleMailMessage ,
它封装了简单邮件的属性如 from, to,cc, subject,text
包里还包含一棵以 MailException 为根的 checked Exception继承树,
它们提供了对底层邮件系统异常的高级别抽象。要获得关于邮件 异常层次的更丰富的信息。
为了使用JavaMail中的一些特色,比如MIME类型的信件,spring提供了 MailSender 的一个子接口,org. springframework, mail, javamail. JavaMailSender,
Spring还提供了一个回调接口 org. springframework. mail, javamail. MimeMessagePreparator,
用于准备 JavaMail 的 MIME 信件。
这里简单的介绍了如何使用spring发送各种形式的邮件以及配置。
1、在src目录下建立mail.properties文件里边包含一下内容
mail.smtp.host=smtp.163.com
mail.smtp.auth=true
mail.username=itheimacca
mail.password=RGOKGSVFHCHJHDHH
mail.from=itheimacca@163.com
2、使用spring配置 applicationContext-mail.xml
/jk28_web/src/main/resources/spring/applicationContext-mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<description>JavaMail的配置</description>
<!-- 使用context加载mail.properties -->
<context:property-placeholder location="classpath:mail.properties"/>
<!-- 配置一个用于发送简单邮件的bean from代表邮件的发送者-->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="${mail.from}"></property>
</bean>
<!-- 邮件发送的Sender实现类的配置 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}"></property>
<property name="username" value="${mail.username}"></property>
<property name="password" value="${mail.password}"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">0</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
</beans>
1.发送简单邮件 JavaMail02Test.testJavaMail()
public class JavaMail02Test {
@Test
public void testJavaMail() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-mail.xml");
SimpleMailMessage message = (SimpleMailMessage) ac.getBean("mailMessage");//加载简单邮件对象
JavaMailSender sender = (JavaMailSender) ac.getBean("mailSender"); //得到邮件的发送对象,专门用于邮件发送
//设置简单邮件对象的属性
message.setSubject("spring与javamail的测试");//主题
message.setText("hello,spring and javamail ");//内容
message.setTo("723422021@qq.com");//收件箱
//发送邮件
sender.send(message);
}
}
2.发送带有图片的邮件,以嵌入HTML的方式
@Test
public void testJavaMail() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-mail.xml");
JavaMailSender sender = (JavaMailSender) ac.getBean("mailSender"); // 得到邮件的发送对象,专门用于邮件发送
// 发送一个允许带图片,同时带附件的邮件
MimeMessage message = sender.createMimeMessage();// 创建一封允许带图片,同时带附件的邮件对象
// 为了更好的操作MimeMessage对象,借用一个工具类来操作它
MimeMessageHelper helper = new MimeMessageHelper(message, true);
// 通过工具类设置主题,内容,图片,附件
helper.setFrom("itheimacch@163.com");
helper.setTo("723422021@qq.com");
helper.setSubject("这是来自bh网22的一个请求");
helper.setText("<html><head></head><body><h1>hello!!baby </h1>" + "<a href=http://www.itheima.com>黑马程序员</a>"
+ "<img src=cid:image/></body></html>", true);// 第二个参数说明内容要解析为html代码
// 添加图片
FileSystemResource resource = new FileSystemResource(
new File("D://mailpic.jpg"));
helper.addInline("image", resource);
sender.send(message);
}
3.发送带附件的邮件
@Test
public void testMimeMessageHelper() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-mail.xml");
JavaMailSenderImpl mailSender = (JavaMailSenderImpl) ac.getBean("mailSender");
// 3.创建一封允许带附件的邮件对象
MimeMessage mimeMessage = mailSender.createMimeMessage();// 创建出允许带附件的邮件对象
// 4.创建出一个用于操作MimeMessage的帮助类的对象
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// 5.设置邮件的相关内容 (发送者,拼接者,主题,内容 )
helper.setFrom("itheimacch@163.com");
helper.setTo("723422021@qq.com");
helper.setSubject("带图片和附件的邮件测试");
helper.setText("<html><head></head><body><h1>hello!!spring image html mail</h1>"
+ "<a href=http://www.itheima.com>黑马程序员</a>" + "</body></html>");// cid:是固定的,后面的image是自己定义的
//helper.setText("<html><head></head><body><h1>hello!!spring image html mail</h1>"
// + "<a href=http://www.itheima.com>黑马程序员</a>" + "<img src=cid:image/></body></html>", true);// cid:是固定的,后面的image是自己定义的
// 指定image所在的位置(是指本地电脑)
//FileSystemResource img = new FileSystemResource(new File("D:\\mailpic.jpg"));// 将本地的图片转化成一个图片资源
//helper.addInline("image", img);// image的参数来自上面cid的取值
// 发送时带附件
FileSystemResource zipResource = new FileSystemResource(new File("D:\\mail.rar"));
helper.addAttachment("mail.rar", zipResource);
// 发送邮件
mailSender.send(mimeMessage);
}
八.案例改写
1.要在UserServicelmpI中注入相关的对象 SimpleMailMessage ,JavaMailSender
〃注入邮件发送相关的对象
private SimpleMailMessage mailMessage;
private JavaMailSender mailSender;
public void setMailMessage(SimpleMailMessage mailMessage) {
this.mailMessage = mailMessage;
}
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
2.修改相关的配置
applicationContext-service.xml
<bean id="userService" class="cn.itcast.jk.service.impl.UserServiceImpl">
<property name="baseDao" ref="baseDao"></property>
<property name="mailMessage" ref="mailMessage"></property>
<property name="mailSender" ref="mailSender"></property>
</bean>
3.添加spring与javaMail集成的配置文件
applicationContext-mail.xml
4.同时将邮件相关信息抽取至U mail.properties文件中
mail.properties
5.使用线程实现邮件发送
//spring集成javaMail
Thread th = new Thread(new Runnable() {
public void run() {
try {
mailMessage.setTo(entity.getUserinfo().getEmail());
mailMessage.setSubject("新员工入职的系统账户通知");
mailMessage.setText("欢迎您加入本集团,您的用户名:"+entity.getUserName()+",初始密码:"+SysConstant.DEFAULT_PASS);
mailSender.send(mailMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
});
th.start();