您的位置:首页 > Web前端 > JavaScript

关于session过期返回主页解决方案之一以及js中的“和'使用问题

2013-08-06 10:56 639 查看
今天为了一个session超时能够返回主页的问题,搞得我蛋疼。原来别人写的老代码。使用spring的aop编程的原理在baseAction中使用

public UserSession getUserInfor() {
		UserSession userinfor = SpringSecurityUtils.getCurrentUser();
		if (null == userinfor) {
			throw new AppException(AppException.SESSION_TIME_OUT, "用户未登录或登录超时,请重新登录!");
		}
		return userinfor;
	}


这样在以后使用到userinfor的所有的使用到的方法中都throws Exception 在aop的类ExceptionAspect接收这个异常并处理。

package cn.comingnet.commons.aop;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;

import cn.comingnet.app.entity.system.AppLog;
import cn.comingnet.app.service.system.LogManager;
import cn.comingnet.commons.exception.AppException;
import cn.comingnet.commons.exception.Exceptioni18n;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 对action层进行aop,异常等处理
 * 
 * @author jile
 * 
 */
@Aspect
public class ExceptionAspect {
	private Logger log = Logger.getLogger(ExceptionAspect.class);
	@Autowired
	private LogManager logManager;

	/**
	 * 定义action层方法
	 */
	@Pointcut("execution(* cn.comingnet.app.web.action..*.*(..)) ")
	public void webServiceMethod() {
	}

	/**
	 * 为WebService入口方法加入TraceID控制.
	 */
	@SuppressWarnings("finally")
	@Around("webServiceMethod()")
	public Object traceAround(ProceedingJoinPoint pjp) {
		ActionSupport baseAction = (ActionSupport) pjp.getTarget();
		String signature = pjp.getSignature().toString();// 获取目标方法签名
		String methodName = signature.substring(signature.lastIndexOf(".") + 1, signature.indexOf("("));
		Object obj = "reload";
		Date startDate = new Date();
		try {
			// System.out.println("开始aop==>"+pjp.getSignature());
			obj = pjp.proceed();
		} catch (Throwable e) {
			Date endDate = new Date();
			// System.out.println("拦截异常信息:【" + e.getMessage() + "】");
			log.error(e.getMessage(), e);
			AppLog appLog = new AppLog();
			appLog.setType("异常信息");
			appLog.setModule(pjp.getTarget().getClass().toString());
			appLog.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startDate));
			appLog.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endDate));
			appLog.setTimes(endDate.getTime() - startDate.getTime());
			appLog.setContent(Exceptioni18n.processor(e) + "<br>" + Exceptioni18n.traceToString(e));
			appLog.setClazz(pjp.getTarget().getClass().toString());
			appLog.setMethod(methodName);
			appLog.setNote(methodName);
			appLog.setIsException(1);
			appLog.setExceptionName(e.getClass().toString());
			appLog.setIp(SpringSecurityUtils.getCurrentUserIp());
			appLog.setOperator(SpringSecurityUtils.getCurrentUserName());
			logManager.save(appLog);
			// 针对ajax请求的异常处理
			if (ActionContext.getContext().getValueStack().findString("op").equals("ajax")) {
				obj = "json";
				if (e instanceof AppException) {
					if (AppException.SESSION_TIME_OUT == ((AppException) e).getCode()) {
						ActionContext.getContext().getValueStack().set("jsonString", "eval(\"alert('" + e.getMessage() + "');window.top.location.href(window.location.protocol + '//' + window.location.host +'/cpms');\")");
					} else {
						ActionContext.getContext().getValueStack().set("jsonString", "{success:false,message:'" + e.getMessage() + "'}");
					}
				} else {
					ActionContext.getContext().getValueStack().set("jsonString", "{success:false,message:'" + Exceptioni18n.processor(e) + "'}");
				}
			}
			baseAction.addActionMessage("<font color=red><b>" + Exceptioni18n.processor(e) + "</b></font>");
		} finally {
			return obj;
		}
	}
}


ActionContext.getContext().getValueStack().set("jsonString", "eval(\"alert('" + e.getMessage() + "');window.top.location.href(window.location.protocol + '//' + window.location.host +'/cpms');\")");

返回一个json字符串使用eval让他作为js执行,这里就要说道这个url了。

这个url是使用js方法拼接出来的,因为,实际使用的项目中个可能经常要变化部署的服务器,你不可能将整个地址写死。

使用window.top.location.href的原因是因为很多项目页面都使用了类似frame框架的设计,这样控制的是整个页面,转向另一个url,而不会出现一个版块打开了首页的奇怪现象。

最后说道的是js中的单引号和双引号的问题的,js语言规则不是很强,单引号和双引号都可以用来括起一个字符串,你也可以根据需要恰当的使用这两种符号,

但我强调的是在返回的拼接url中使用单引号吧。当无知的我使用了双引号”//“,为了在java字符串中让他认为//后面的是注释我还特意的转译了下”\/\/“,很顺利的传到页面上的url变成了

http:“//” + window.location.host +”/cpms“

你没看错js他也不认识”//“ 了,不管什么原因一旦//或者/前面出现了”的话,后面的他就不认了。具体的我也搞不明白。

当我很是蛋疼时,我把所有的双引号换成单引号,终于得到了我想要的结果。在java端也不用什么转译的。真是方便啊!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐