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 );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以对这段代码进行如下的优化: ``` if (theData.Id.IsNullOrEmpty()) { theData.Id = Guid.NewGuid().ToSequentialGuid(); if (theData.ifNeedVerifyCode == true && SessionHelper.Session["verifyCode"].IsNullOrEmpty()) { return Error("请先发送验证码"); } if (theData.ifNeedVerifyCode == true && SessionHelper.Session["verifyCode"].ToString() != theData.verifyCode) { return Error("验证码不正确"); } var unweightedCount = _wTapplicationBusiness.GetIQueryable() .Where(a => a.Carno == theData.Carno && a.ExpectedDate == theData.ExpectedDate && a.WeightTime.IsNullOrEmpty()) .Count(); if (unweightedCount > 0) { return Error("该车在该送货日期存在未过磅的信息,请过磅后再新增数据"); } _wTapplicationBusiness.AddData(theData); _wTapplicationBusiness.GetLogger().WriteOperateLog("WTapplication信息添加", "id:" + theData.Id, true); } else { var unweightedCount = _wTapplicationBusiness.GetIQueryable() .Where(a => a.Carno == theData.Carno && a.ExpectedDate == theData.ExpectedDate && a.WeightTime.IsNullOrEmpty() && a.Id != theData.Id) .Count(); if (unweightedCount > 0) { return Error("该车在该送货日期存在未过磅的信息,请过磅后再新增数据"); } _wTapplicationBusiness.UpdateData(theData); _wTapplicationBusiness.GetLogger().WriteOperateLog("WTapplication信息修改", "id:" + theData.Id, true); } ``` 主要的优化点如下: 1. 合并了两个 `if` 语句,使得代码更加简洁。 2. 重命名了 `UnWightCount` 变量为 `unweightedCount`,符合命名规范。 3. 将 `_wTapplicationBusiness.GetIQueryable()` 的查询条件提取出来,避免重复写代码。 4. 将新增和修改的代码分别写在了两个分支中,使得代码更加清晰易懂。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值