长话短说,抛砖引玉,举个很简单的例子,通过Session,验证用户是否已登陆。
环境:MyEclipse6.5+Mysql5+struts2.0.11.2
源码:拦截器类:
AuthInterceptor.java
<!---->package com.sy.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -3363926000416510686L;
@SuppressWarnings("unchecked")
@Override
public String intercept(ActionInvocation invocation) throws Exception {
@SuppressWarnings("unused")
Map map=invocation.getInvocationContext().getSession();//获取session映射
if(map.get("user")==null){//如果未登陆转到登陆页面
return Action.LOGIN;
}
else{
return invocation.invoke();//如果登陆继续执行
}
}
}
验证用户登陆后的Action类,负责向Session中插入值。
AdminAction.java
<!---->package com.sy.action;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sy.dao.AdminDao;
import com.sy.dao.NewsDao;
import com.sy.dao.impl.AdminDaoImpl;
import com.sy.dao.impl.NewsDaoImpl;
import com.sy.vo.Admin;
import com.sy.vo.News;
public class AdminAction extends ActionSupport {
private static final long serialVersionUID = -3699334709726488611L;
int i=1;//中间变量
private int k;//储存最大页面数
private int pageNow=1; //页码数,初始为1
private int pageSize = 8 ; //页面行数
private int intRowCount;//总行数
private int intPageCount;//总页数
private List<News> Newss;
private Admin admin;
public List<News> getNewss() {
return Newss;
}
public void setNewss(List<News> newss) {
Newss = newss;
}
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getK() {
return k;
}
public void setK(int k) {
this.k = k;
}
public int getIntRowCount() {
return intRowCount;
}
public void setIntRowCount(int intRowCount) {
this.intRowCount = intRowCount;
}
public int getIntPageCount() {
return intPageCount;
}
public void setIntPageCount(int intPageCount) {
this.intPageCount = intPageCount;
}
@SuppressWarnings("unchecked")
public String execute() throws Exception {
AdminDao adi=new AdminDaoImpl();
admin.getAname();
admin.getApassword();
if(adi.isLogin(admin)){
Map map=ActionContext.getContext().getSession();//插入Session的值
map.put("user", admin.getAname());
NewsDao npage=new NewsDaoImpl();
intRowCount=npage.count();
k=(intRowCount + pageSize - 1) / pageSize;
intPageCount = (intRowCount + pageSize - 1) / pageSize;//计算出总页数
if(pageNow<1){
pageNow=1;
}
if(pageNow > intPageCount)
pageNow=intPageCount;
i = (pageNow -1)*pageSize;
NewsDao nlist=new NewsDaoImpl();
if(null!=nlist.queryByPage(i,pageSize)){
Newss = nlist.queryByPage(i,pageSize);
return SUCCESS;
}else{
return "failure";
}
}else
return "failure";
}
}
struts.xml
<!----><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="News" extends="struts-default">
<interceptors>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="auth"></interceptor-ref>
</interceptor-stack>
<interceptor name="auth" class="com.sy.interceptor.AuthInterceptor"/>
</interceptors>
<global-results>
<result name="login" type="redirect">/admin/login.jsp</result>
</global-results>
<!-- 验证管理员登陆 -->
<action name="login" class="com.sy.action.AdminAction">
<result name="failure">/admin/failure.jsp</result>
<result>/admin/ManageNews.jsp</result>
<result name="input">/admin/login.jsp</result>
</action>
<!-- 管理员列表 -->
<action name="alist" class="com.sy.action.ListAction" method="adminList">
<result>/admin/deleteManager.jsp</result>
<result name="failure">/admin/Showfailure.jsp</result>
<interceptor-ref name="myStack"></interceptor-ref>
</action>
</package>
</struts>
配置完成!!!
施杨出品!!!转载注明出处www.cnblogs.com/shiyangxt
分享到:
相关推荐
struts2拦截器和验证框架
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
综合使用servlet filter与struts2 interceptor对权限进行验证。
Struts2拦截器实现权限控制demo,我感觉对初学者还是有一定帮助的
struts2.X 拦截器源码
Struts2的核心,拦截器的应用!!!
想学习Struts2拦截器 验证机制(比如Login拦截)的小伙伴可以参考一下,里面有最浅显易懂的案例,欢迎交流吐槽
Struts2拦截器.ppt Struts2拦截器.ppt Struts2拦截器.ppt
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序
关于struts2 拦截器的详细的解释及其在开发中对struts2的拦截器的应用和拦截器在框架中的作用.
1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 2. 拦截器栈(Interceptor Stack)。Struts2拦截器栈就是将拦截器按...
NULL 博文链接:https://cmexico.iteye.com/blog/621208
struts2拦截器应用小例子,与大家共分享
浪曦struts2拦截器源码浪曦struts2拦截器源码
基于SSH2框架Struts2拦截器的登录验证实现
struts2拦截器的使用说明文档,配置说明文档。可以看一下
struts2 拦截器_教程.zip
基于struts2的拦截器测试,实现了页面的跳转,中间过程的拦截
struts2拦截器实现权限控制。 内附代码及说明。 就在这里和大家分享下,希望能帮到哪位朋友。