struts2拦截器简单例子
来源:互联网 发布:smartdraw mac 破解版 编辑:程序博客网 时间:2024/05/21 17:05
自定义拦截器实现步骤:
1、实现Interceptor接口
在需要配置拦截器参数的情况下使用处理拦截的方法:public String intercept(ActionInvocation invocation) throws Exception {
}
invocation.invoke():
1-1、只要能获取这些代码,则说明拦截器验证通过,接着访问action类中对应的方法执行后续操作
1-2、没有执行这行代码:不会去调action中的方法,而是转向struts.xml文件去搜索拦截器中返回的字符标志
2、在struts.xml文件中对自定义拦截器进行配置
2-1、使用<interceptors> <interceptor name="自定义拦截器的名称"></interceptor> ... </interceptors>来加载自定义的拦截器
2-2、引用配置好的拦截到对应的action中 <interceptor-ref name="自定义拦截器的名称"/>
例子:
修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>struts11</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 过滤器 -->
<filter>
<!-- 过滤器核心类 -->
<filter-name>struts2</filter-name>
<!-- struts2的核心类 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 映射 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<!-- 过滤URL -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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="default" namespace="/" extends="struts-default">
<!-- 配置拦截器 -->
<interceptors>
<interceptor name="myIntercepter" class="com.caokaiyuan.intercepter.MyIntercepter"></interceptor>
</interceptors>
<!-- 登录 -->
<action name="loginAction" class="com.caokaiyuan.action.UserAction" method="login">
<result name="login">success.jsp</result>
</action>
<!-- 用户管理 -->
<action name="userAction" class="com.caokaiyuan.action.UserAction" method="user">
<!-- 引用拦截器 -->
<interceptor-ref name="myIntercepter"></interceptor-ref>
<result name="login">login.jsp</result>
<result name="user">user.jsp</result>
</action>
<!-- 客户管理 -->
<action name="clientAction" class="com.caokaiyuan.action.UserAction" method="client">
<!-- 引用拦截器 -->
<interceptor-ref name="myIntercepter"></interceptor-ref>
<!-- 配置拦截器不放行是返回字符串与 -->
<result name="login">login.jsp</result>
<result name="client">client.jsp</result>
</action>
</package>
</struts>
创建UserVO类
package com.caokaiyuan.vo;
import java.io.Serializable;
public class UserVO implements Serializable
{
private static final long serialVersionUID = 1L;
private String username;
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
创建action类UserAction
package com.caokaiyuan.action;
import org.apache.struts2.ServletActionContext;
import com.caokaiyuan.vo.UserVO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements ModelDriven<UserVO>
{
private static final long serialVersionUID = 1L;
private UserVO user = new UserVO();
//登录
public String login() throws Exception
{
System.out.println(user);
ServletActionContext.getRequest().getSession().setAttribute("user", user);
return "login";
}
//客户管理
public String client() throws Exception
{
return "client";
}
//用户管理
public String user() throws Exception
{
return "user";
}
public UserVO getUser()
{
return user;
}
public void setUser(UserVO user)
{
this.user = user;
}
@Override
public UserVO getModel()
{
return user;
}
}
创建拦截器类MyIntercepter
package com.caokaiyuan.intercepter;
import com.caokaiyuan.vo.UserVO;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyIntercepter implements Interceptor
{
private static final long serialVersionUID = 1L;
@Override
public void destroy()
{
}
@Override
public void init()
{
System.out.println("==========MyIntercepter==========");
}
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
UserVO user = (UserVO)invocation.getInvocationContext().getSession().get("user");
if (user != null)
{
System.out.println("拦截器放行!!");
return invocation.invoke();
}
else
{
System.out.println("拦截器不放行!!");
return Action.LOGIN;
}
}
}
创建index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p><a href="login.jsp">用户登录</a></p>
<p><a href="userAction">用户管理</a></p>
<p><a href="clientAction">客户管理</a></p>
</body>
</html>
创建login.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="loginAction" method="post">
<input type="text" name="usrename"/><br/>
<input type="password" name="password"/><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
创建client.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用户管理模块</h1>
</body>
</html>
创建user.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用户管理模块</h1>
</body>
</html>
创建success.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆成功!!
</body>
</html>
- struts2拦截器简单例子
- struts2 拦截器例子
- struts2中的拦截器概述和简单例子
- Struts2拦截器简单示例
- Struts2拦截器简单示例
- struts2实现简单拦截器
- Struts2拦截器简单模拟
- struts2拦截器 简单了解
- struts2拦截器简单介绍
- struts2 中的拦截器例子快速进阶
- spring拦截器的一个简单例子
- 使用拦截器的简单例子
- spring拦截器的一个简单例子
- spring mvc 拦截器简单例子
- struts拦截器的使用(简单例子)
- Struts2拦截器的一个简单应用
- 编写一个简单的Struts2 拦截器
- 最简单的拦截器写法 struts2
- STLA数据读入操作
- QQ通信原理分析
- 人才与成本
- Atitit JAVA p2p设计与总结 JXTA 2
- JVM的内存布局
- struts2拦截器简单例子
- org graphvize 编译 自动关闭*compilation* buffer
- java字符串与整数之间的互相转换
- 使用ashx文件生成验证码图片
- hadoop平台搭建(3)--hadoop安装、环境配置、单机运行
- 循序渐进之Spring AOP(5) - 创建切面
- 字符串排序
- 缓存策略
- activity启动模式