Struts2透过自定义拦截器实现登录之后跳转到原页面
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透过自定义拦截器实现登录之后跳转到原页面的更多相关文章
- Struts2通过自己定义拦截器实现登录之后跳转到原页面
这个功能对用户体验来说是非常重要的.实现起来事实上非常easy. 拦截器的代码例如以下: package go.derek.advice; import go.derek.entity.User; i ...
- JavaWeb -- Struts 自定义拦截器, 登录权限拦截
1. 自定义拦截器, 登录权限拦截 login.jsp 登录JSP <%@ page language="java" contentType="text/html; ...
- struts2基础——自定义拦截器
一.自定义拦截器 默认的拦截器能实现的功能是有限的,Struts2 支持自定义拦截器. 二.拦截器类 1.实现 Interceptor 接口 2.继承 AbstractInterceptor 抽象类, ...
- 【struts2】自定义拦截器
1)什么是自定义的拦截器 所谓自定义的拦截器,就是由我们自己定义并实现的拦截器,而不是由Struts2定义好的拦截器.虽然Struts2的预定义拦截器已经满足了大多数情况的需要.但在有些时候,我们可能 ...
- struts2 的自定义 拦截器
Struts2的 拦截器: 对于拦截器,Struts2官方给出的 定义是: 拦截器是动态拦截Action调用的对象.它提供了一种机制,使开发者可以定义一段代码,在Action执行之前或者之后被调用执行 ...
- struts2基础---->自定义拦截器
这一章,我们开始struts2中拦截器的学习. 自定义拦截器
- struts2自定义拦截器 设置session并跳转
实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆. 为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟 修改web.xml view plainco ...
- 【Struts2】自定义拦截器interceptors
下面给一张图片表示Struts2拦截器的处理流程. 通过这个流程图,我们可以看出一个完整的请求大概的过程为: 请求 -->filter 控制器 --> 拦截器 1/ 拦截器 2--> ...
- Struts2笔记——自定义拦截器
需要两个步骤,自定义类实现拦截器接口和在struts.xml注册拦截器 =============================== 1.自定义类实现com.opensymphony.xwork2. ...
随机推荐
- 绿色版的mysql安装配置方式
解压下载好的压缩包 copy 一份my-default.ini改名字为my.ini为mysql的配置文件 打开my.ini 修改配置文件 默认的原版文件为 # For advice on how to ...
- [Q]自定义保存位置及文件名
以“DWG To PDF.pc3”打印为例: 说明:<DrawingDirectory> 当前图纸所在目录<DrawingFolderName> 当前图纸文件所在文件夹名称&l ...
- c#语言简介
语言的历史:c,c++,java,c#, c语言最早,c++语言复杂,而java把其中难的一部分去掉,也成为c--,而c#早期类似java. c#简单高效,并且与web结合. C#的运行环境是.net ...
- 原生JavaScript+CSS3实现移动端滑块效果
在做web页面时,无论PC端还是移动端,我们会遇到滑块这样的效果,可能我们往往会想着去网上找插件,其实这个效果非常的简单,插件代码的的代码往往过于臃肿,不如自己动手,自给自足.首先看一下效果图: 分析 ...
- linux学习心态建设
为啥我们要学习Linux 技术的价值不在于这个技术有多么高超,而在于技术本身给我们带来什么价值,所以很多时候我们学习一个技术,不能盲目学,是为了使用这个技术,知道这个技术的使用场景,知道这个技术带来的 ...
- WPF Image Binding Uri Source 失败解决办法
在ListView 的ListItem里动态绑定Image. 首先代码写的是没有问题的.但最后运行却无法显示图片.先看代码: 1. XAML部分 代码如下: <ListView x:Name=& ...
- python demo整理
1 变量作用域 #!/usr/bin/python # coding=utf-8 name = "whole global name" class Person: name = & ...
- intellij idea 12 搭建maven web项目 freemarker + spring mvc
配置spring mvc ,写这篇文章的时候spring已经出了4.0 这里还是用稳定的3.2.7.RELEASE,先把spring和freemarker配置好 1.spring mvc配置 在web ...
- C#入门经典中的SelectionFont属性为null
14.6.2.richtextbox控件: private void buttonbold_click(object sender, eventargs e) { ...
- zoj 2770 Burn the Linked Camp
今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #incl ...