SessionHelper

问题描述:

        strut2 的织入 Session 为原始 Map 类型,没有泛型化,在添加属性时就会有一个恼人的警告。

功能:

1、安全的消除警告

2、插入时检查类型,如果不符就提前报错(免得取值时才报转换异常的错误)


源代码:

package com.gq.util;

import java.util.Map;

public class SessionHelper {
	private Map session;
	private Class valueType;
	
	private SessionHelper( Map session, Class valueType ){
		this.session = session;
		this.valueType = valueType;
	}
	
	public static SessionHelper newInstance( Map session, Class valueType ){
		
		// valid parameter
		if( session == null ){
			throw new NullPointerException("session can't be null, but is null.");
		}
		if( valueType == null ){
			throw new NullPointerException("valueType can't be null, but is null.");
		}
		
		return new SessionHelper( session, valueType );
	}
	
	@SuppressWarnings("unchecked")
	// safe cast, because just put object of calss what you want.
	public void putIntoSession( Object key, Object value ){
		
		// valid parameter
		if( key == null || value == null ){
			throw new NullPointerException("key and value must not be null.");
		}
		
		// valid value's type weather what you want
		if( value.getClass() != valueType ){
			throw new RuntimeException( "need class: " + valueType.getName() + 
										" but is: " + value.getClass().getName() );
		}
		
		session.put(key, value);
	}

}

使用示例:

//session.put(USER_SESSION_KEY, user);
SessionHelper.newInstance( session, User.class ).putIntoSession( USER_SESSION_KEY, user );

优化代码 /// <summary> /// 获取验证码 /// </summary> /// <param name="phone"></param> /// <returns></returns> public ActionResult GetVerificationCode(string phone) { try { //if (phone.IsNullOrEmpty()) // return Error("未获取对应手机号,请刷新重试!"); ///生成六位随机码 var verifyCode = VerifyCode(); //存储临时验证码 SessionHelper.Session["verifyCode"] = verifyCode; string code = "{"code":""+ verifyCode + ""}"; //判断是否手机存在在系统重 if (phone.IsNullOrEmpty()) return Error("手机号码不允许为空"); //发送短信 //var result = PhoneMessage.PhoneMessageService("smsInvoice", "13003931757", verifyCode, 1, 0); var result = SmsHelper.SendSms(phone, "SMS_216730297", code); bindCustomerBusiness.GetLogger().WriteOperateLogNoUser("获取验证码", result.ToJson(), true); return Success(); } catch (Exception ex) { return Error(ex.StackTrace); } } public ActionResult GetVerificationCode2(string phone) { try { //if (phone.IsNullOrEmpty()) // return Error("未获取对应手机号,请刷新重试!"); ///生成六位随机码 var verifyCode = VerifyCode(); //存储临时验证码 SessionHelper.Session["verifyCode"] = verifyCode; string code = "{"code":"" + verifyCode + ""}"; //判断是否手机存在在系统重 if (phone.IsNullOrEmpty()) return Error("手机号码不允许为空"); else { var phoneCount = bindCustomerBusiness.Service.GetIQueryable<Base_User>(). Where(a => a.Phone == phone).Count(); if (phoneCount == 0) return Error("手机号未绑定,首次应扫码登录"); } //发送短信 //var result = PhoneMessage.PhoneMessageService("smsInvoice", "13003931757", verifyCode, 1, 0); var result = SmsHelper.SendSms(phone, "SMS_216730297", code); bindCustomerBusiness.GetLogger().WriteOperateLogNoUser("获取验证码", result.ToJson(), true); return Success(); } catch (Exception ex) { return Error(ex.StackTrace); } }
最新发布
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值