长话短说,抛砖引玉,举个很简单的例子,通过Session,验证用户是否已登陆。
环境:MyEclipse6.5+Mysql5+struts2.0.11.2
源码:拦截器类:
AuthInterceptor.java
Code
验证用户登陆后的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 " ;
}
}
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 >
<! 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 >
配置完成!!!
本文转自施杨博客园博客,原文链接:http://www.cnblogs.com/shiyangxt/archive/2008/10/24/1318673.html,如需转载请自行联系原作者