Struts2通过自定义拦截器实现登录之后跳转到原页面

这个功能对用户体验来说是非常重要的。实现起来其实很简单。

拦截器的代码如下:

package go.derek.advice;

import go.derek.entity.User;
import go.derek.util.CommonChecks;
import go.derek.util.Constant;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class JumpBeforeInterceptor extends AbstractInterceptor implements ApplicationContextAware{

	private static final long serialVersionUID = 7224871247223705569L;

	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		ActionContext actionContext = ai.getInvocationContext();
		Map<String, Object> session = actionContext.getSession();
		User currentUser = (User) session.get(Constant.USER_INFO);
		if (null != currentUser
				&& CommonChecks.isNotEmpty(currentUser.getUserId())) {
			return ai.invoke();//执行下一个拦截器,如果没有拦截器了,就执行action
		}
		else{
				HttpServletRequest request = (HttpServletRequest) actionContext
					.get(StrutsStatics.HTTP_REQUEST);
				String queryString = request.getQueryString();
			    // 预防空指针
			    if (queryString == null) {
					queryString = "";
			    }
			    // 拼凑得到登陆之前的地址
			    String realPath = request.getRequestURI() + "?" + queryString;
			    System.out.println(realPath+"	realPath");
				session.put(Constant.GOING_TO_URL, realPath);
				return ai.invoke();//放行
			}
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		// TODO Auto-generated method stub

	}
}

将上一页面的url放到session中,然后登录的时候再从session中取出来,再重定向就可以了。

loginAction中这样写

String url = (String) this.getSession().get(Constant.GOING_TO_URL);
		if (CommonChecks.isNotEmpty(url)) {
			this.getResponse().sendRedirect(url);
			return null;
		}

struts.xml配置文件中,要对上面的自定义拦截器做配置:

<interceptor name="jumpBefore"
				class="go.derek.advice.JumpBeforeInterceptor"/>
			<interceptor-stack name="jumpTo">
			<!-- 定义拦截器栈包含checkLogin拦截器 -->
			<interceptor-ref name="jumpBefore"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>

还要在之前的那个页面对应的action中,把拦截器配置上,比如:

<action name="problems" class="problemsAction">
			<result>/problems.jsp</result>
			<interceptor-ref name="jumpTo" />
		</action>

经过上面的过程,就可以实现登陆之后跳转回原页面了~

Struts2透过自定义拦截器实现登录之后跳转到原页面的更多相关文章

  1. Struts2通过自己定义拦截器实现登录之后跳转到原页面

    这个功能对用户体验来说是非常重要的.实现起来事实上非常easy. 拦截器的代码例如以下: package go.derek.advice; import go.derek.entity.User; i ...

  2. JavaWeb -- Struts 自定义拦截器, 登录权限拦截

    1. 自定义拦截器, 登录权限拦截 login.jsp 登录JSP <%@ page language="java" contentType="text/html; ...

  3. struts2基础——自定义拦截器

    一.自定义拦截器 默认的拦截器能实现的功能是有限的,Struts2 支持自定义拦截器. 二.拦截器类 1.实现 Interceptor 接口 2.继承 AbstractInterceptor 抽象类, ...

  4. 【struts2】自定义拦截器

    1)什么是自定义的拦截器 所谓自定义的拦截器,就是由我们自己定义并实现的拦截器,而不是由Struts2定义好的拦截器.虽然Struts2的预定义拦截器已经满足了大多数情况的需要.但在有些时候,我们可能 ...

  5. struts2 的自定义 拦截器

    Struts2的 拦截器: 对于拦截器,Struts2官方给出的 定义是: 拦截器是动态拦截Action调用的对象.它提供了一种机制,使开发者可以定义一段代码,在Action执行之前或者之后被调用执行 ...

  6. struts2基础---->自定义拦截器

    这一章,我们开始struts2中拦截器的学习. 自定义拦截器

  7. struts2自定义拦截器 设置session并跳转

    实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆. 为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟 修改web.xml view plainco ...

  8. 【Struts2】自定义拦截器interceptors

    下面给一张图片表示Struts2拦截器的处理流程. 通过这个流程图,我们可以看出一个完整的请求大概的过程为: 请求 -->filter 控制器 --> 拦截器 1/ 拦截器 2--> ...

  9. Struts2笔记——自定义拦截器

    需要两个步骤,自定义类实现拦截器接口和在struts.xml注册拦截器 =============================== 1.自定义类实现com.opensymphony.xwork2. ...

随机推荐

  1. Linux Tomcat 自启动

    使用chkconfig命令 修改tomcat/bin/startup.sh,在开头的地方添加如下内容 #chkconfig: #description:tomcat auto start #proce ...

  2. Visual Studio下使用jQuery的10个技巧

    广泛流行的jQuery是一个开源的,跨浏览器和兼容CSS 3的JavaScript库,你可以用它简化你的JavaScript编码任务和操作(添加,编辑和删除)HTML内容中的DOM元素,本文介绍10个 ...

  3. js将xml对象,xml文件解析成xml dom对象,来对对象进行操作

    由于ie与其他的浏览器对于xml文件的解析方式不同,所以有不同的解析方式 1.1 IE解析xml文件的方式 var xmlDoc=new ActiveXObject("Microsoft.X ...

  4. java中system.out.println()是什么意思

    在Java编程中,我们常常用System.out.println()方法来输出字符串,也许我们都已经猜到println()是方法名,但System是什么,out又是什么呢?这里就涉及用到一个stati ...

  5. 在java中json的使用案例

    import java.text.ParseException; import org.json.JSONArray; import org.json.JSONObject; public class ...

  6. iis服务器上面使用百度编辑器ueidtor提示“找不到临时文件”需要给window/temp修改权限

    iis服务器上面使用百度编辑器ueidtor提示“找不到临时文件”需要给window/temp修改权限,修改php上传临时文件window/temp 给user用户组读取和写入的权限

  7. ios 从前台返回到回台 从后台返回到前台 或者 支付宝支付订单后 对界面进行操作

    正常情况下,在AppDelegate中实现下面两个方法,能够监听从后台恢复到前台 - (void)applicationDidEnterBackground:(UIApplication *)appl ...

  8. linux jdk环境变量

    export JAVA_HOME=/usr/share/jdk8 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/ ...

  9. PAT 团体程序设计天梯赛-练习集 L1-008. 求整数段和

    给定两个整数A和B,输出从A到B的所有整数以及这些数的和. 输入格式: 输入在一行中给出2个整数A和B,其中-100<=A<=B<=100,其间以空格分隔. 输出格式: 首先顺序输出 ...

  10. spring ,mybatis多数据源

    同一个项目有时会涉及到多个数据库,也就是多数据源.多数据源又可以分为两种情况:   1)两个或多个数据库没有相关性,各自独立,其实这种可以作为两个项目来开发.比如在游戏开发中一个数据库是平台数据库,其 ...