struts1的总结

为以后用到的时候有个参照,仅此而已。

   框架:struts1+Spring


情形(1)从后台取出数据放入select中并默认选中之前选的

<select name="fullname" style="width: 140px;"class="selectWidth" id="fullname" onChange="changeName()">
	 <option value="0"></option>
<logic:iterate id="list" name="names">
	<option value="<bean:write name="list" property="employeeid"/>" <c:if test="${app.employeeid==list.employeeid }">selected </c:if> >
	    <bean:write name="list" property="employeeName" />
	</option>
</logic:iterate>
</select>

说明一点,后台是request.setAttribute("names",names),所以这里name=“names”即是要遍历的后台request里的List,便利出来的对象定义为list   (id="list")

<bean:write name="list" property="employeeid" />即取出对象的employeeid属性  

这里需要注意,struts1也是支持ognl的,网上有好多人说不可以,但这却真真实实的“${app.fullname==list.employeeid}”可以,您也可以在百度里看jstl中也有跟el的混合使用。


情形(2) 点击某个链接 显示或隐藏

<div onClick='showhidediv("msg")' id="unfold" >+show or hidden</div>

<tbody id="msg" style="display:none;" mce_style="display:none;">

。。。。。。<!--这里是需要显示或者隐藏的-->

</tbody>

js代码:

function showhidediv(id){
        var sbtitle=document.getElementById(xx);
        var unfold=document.getElementById("xx");
        if(sbtitle){
           if(sbtitle.style.display=='block'){
           sbtitle.style.display='none';
            unfold.innerHTML="+show";
           
           }else{
           sbtitle.style.display='block';
           unfold.innerHTML="-hidden";
           }
        }
        }

情形3  ajax实现选择某人后出现此人信息 

function changeName(){
    var em_id = document.getElementById("xx").value;
    var urlStr = "<%=WEBPATH%>/changeName.fb?em_id="+em_id;
    var xmlhttp;
    if (window.XMLHttpRequest){                // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else{                                    // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            var mid = xmlhttp.responseText;
            var buf = mid.split("$$");
            
            var str=buf[1].split(";;");
            var out = document.getElementById("xx");
            out.options.length=0;
            var ojobRole = document.getElementById("xx");
            var owork = document.getElementById("owork");
            
            for(var i=0;i<str.length-1;i++){
                var proj=str[i].split(",");
                out.options.add(new Option(proj[2],proj[0]));
                ojobRole.value=proj[1];
                owork.value=proj[3];
            }        
        }
    }
    xmlhttp.open("get",urlStr,true);
    xmlhttp.send(em_id);
}
是$$数据1;数据2;....$$

所以这里进行了分割传回的数据

情形4  用Spring发送邮件

先配置邮件的邮箱

mail.smtp.auth=true  
mail.smtp.timeout=25000  
mail.smtp.starttls.enable=true  
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory  
mail.smtp.socketFactory.fallback=false  

email.smtpHost=smtp.gmail.com
email.port=465
email.from=xx@gmail.com
email.username=xx@gmail.com
email.password=xx


下面定义发送邮件的类

public class NewEmailSender implements Runnable {
	
	//get info from resource file
	static String host=ResourceBundle.getBundle("email").getString("email.smtpHost");
	static String port=ResourceBundle.getBundle("email").getString("email.port");
	static String mailFrom=ResourceBundle.getBundle("email").getString("email.from");
	final static String username =ResourceBundle.getBundle("email").getString("email.username");
	final static String password = ResourceBundle.getBundle("email").getString("email.password");
	
	
	private List<String> mailTo;
	private List<String> cc;
	private String subject;
	private String msgContent;
	
	
	public NewEmailSender(){
		
	}
	public NewEmailSender(List<String> mailTo,List<String> cc,String subject,String msgContent){
		this.mailTo=mailTo;
		this.cc=cc;
		this.subject=subject;
		this.msgContent=msgContent;
	}
	public static Session getMailSession()
	{
		String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", host);
		props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.port", port);
		props.setProperty("mail.smtp.socketFactory.port", port);
		props.put("mail.smtp.auth", "true");
		Session sendMailSession= Session.getInstance(props, new Authenticator(){
			protected PasswordAuthentication getPasswordAuthentication() {
			    return new PasswordAuthentication(username, password);
			}
		});
		
		return sendMailSession;
	}
	
	
	public void sentEmail(List<String> mailTo,List<String> cc,String subject,String msgContent)
	{
		try{
			Session sendMailSession=getMailSession();
			Message msg = new MimeMessage(sendMailSession);
			
			msg.setFrom(new InternetAddress(username));
			// set the mailTo recipients
			InternetAddress[] address = new InternetAddress[mailTo.size()];
			for (int i = 0; i<mailTo.size(); ++i)
			{
				address[i] = new InternetAddress(mailTo.get(i));
			}
			msg.setRecipients(Message.RecipientType.TO, address); 
			// set the cc recipients
			if (cc != null)
			{
				System.out.println("bcc:"+cc.size());
				address = new InternetAddress[cc.size()];
				for (int i = 0; i<cc.size(); ++i)
				{
					address[i] = new InternetAddress(cc.get(i));
				}
				msg.setRecipients(Message.RecipientType.CC, address);
			} 

			/****/
			msg.setSubject(subject);
			//msg.setText(msgContent);
			msg.setContent(msgContent, "text/html");
			msg.setSentDate(new Date());
			Transport.send(msg);
			
			System.out.println("Message sent.");
		} 
		catch(MessagingException m) 
		{ 
			System.out.println(m.toString()); 
		}
	}

	@Override
	public void run() {
		sentEmail(mailTo, cc,subject, msgContent);	
	}
}

下面用到发送邮件的地方,比如一些事件完成  调用这个类

List<String> mailTo=new ArrayList();//要发送的目的邮箱
List<String> cc=new ArrayList();//抄送的邮箱

.....这里把邮箱的List放入mailTo和cc 

比如mailTo.add(xx@gmail.com);

String subject="xxxxx";//邮件的主题
  StringBuffer sb=new StringBuffer(); //邮件的内容

sb.append("xxxxx");

 NewEmailSender sender=new NewEmailSender(mailTo, cc, subject, sb.toString()); //参数分别为目标邮箱,抄送邮箱,主题,内容
 Thread t = new Thread(sender);  //定义线程  发送
 t.start();


这样就可以发送了,默认是从配置文件里的邮箱给这个邮箱发




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值